Question

In: Electrical Engineering

MATLAB Assignment 8 Introduction to Linear Algebra (Weeks 11 and 12) Spring, 2018 1. MATLAB Submission...

MATLAB Assignment 8

Introduction to Linear Algebra (Weeks 11 and 12) Spring, 2018

1. MATLAB Submission Problem 3 ( Due Date : May 24 (Thu) ) Referring to the instruction below, you are required to submit this problem.

A common problem in experimental work is to find a curve y = f(x) of a specified form corresponding to experimentally determined values of x and y, say

(x1, y1), (x2, y2), · · · , (xn, yn). The followings are the four important models in applications.

• Linear line model (y = ax + b)
• Exponential model (y = aebx)
• Logarithmic model (y = a + b ln x)

A function file LS_solver.m is to fit given experimental data to the proper mathematical model using least squares method. The function file given as follows:

     % --------- function file "LS_solver.m" --------- %
     % input data: x, y and opt
     %    if opt=1, linear model (y=a*x+b)
     %    if opt=2, exponential model (y=a*exp(b*x))
     %    if opt=3, logarithmic model (y=a+b*ln(x))
     function [a, b]=LS_solver(x, y, opt)
         [m1, n1]=size(x);   [m2, n2]=size(y); % Size of the input data.
         xx=linspace(min(x), max(x), 100); % xx will be used to plot the fitting curve.
         if (m1~=1)||(m2~=1)||(n1~=n2)   % If the input data size is not proper,
             fprintf(’Error: Improper input data.\n’);   % error message.
         elseif (opt==1)||(opt==2)||(opt==3)  % option = 1, 2, 3.
             figure; plot(x, y, ’o’); % Plot the given data points.
             hold on;  % Ready to draw the next graph.
             switch opt
                 case 1  % Linear model
                     fprintf(’Linear model\n’);
                     % ---- Complete here ---- %
                     a=sol(1); b=sol(2); % Fitting constants a and b.
                     plot(xx, a*xx+b); % Plot the fitting curve with a and b.
                     title(’Linear model (y=a*x+b)’);
                 case 2  % Exponential model
                     fprintf(’Exponential model\n’);
                     % ---- Complete here ---- %
            case 3  % Logarithmic model
                fprintf(’Logarithmic models\n’);
                % ---- Complete here ---- %
        end
        hold off; % no more graph.
    else % for invalid [opt]
        fprintf(’Error: Improper option value.\n’); % error message.
        return; % Return the process.

end end

If you execute the following MATLAB commands:

>> x=[2 3 4 5 6 7 8 9]; y=[1.75 1.91 2.03 2.13 2.22 2.30 2.37 2.43];
>> [a b]=LS_solver(x, y, 1)

Then, you may obtain the following results with the figure (See Figure 1 below):

Linear model a=

0.0948

b=
1.6213

Figure 1: Execution result

(a) Download the function file LS_solver.m in KLMS and complete the missing parts.

(b) Use LS_solver.m to fit an exponential model to the following data (Table 1), and

graph the curve and data points in the same figure.
Table 1: Data points of Problem 1-ii (exponential model)

(c) Use LS_solver.m to fit a logarithmic model to the following data (Table 2), and graph the curve and data points in the same figure.

Table 2: Data points of Problem 1-iii (logarithmic model)

You may use the backslash operator in MATLAB (syntax : A \ b for a linear system Ax = b) and refer to the T5 and T7 in Section 7.8 of the textbook.

Submission Guidelines.

Save your resulting images as Id_b.fig and Id_c.fig, respectively.

Upload your m-file, resulting images and hardcopy solution (pdf format is recom-

mended) on the Homework Box for MATLAB Submission Problem 3.

Print out your hardcopy solution and submit it to your recitation TA at the beginning

of your recitation class.

Add comments in your m-file using %.

Late submission will not be allowed.

2. Read the attachments “MATLAB Week11.pdf”, “MATLAB Week12.pdf” and practice by yourself.

x

0

1

2

3

4

5

6

7

y

3.9

5.3

7.2

9.6

12

17

23

31

x

2

3

4

5

6

7

8

9

y

4.07

5.30

6.21

6.79

7.32

7.91

8.23

8.51

Solutions

Expert Solution


% --------- function file "LS_solver.m" --------- %
% input data: x, y and opt
% if opt=1, linear model (y=a*x+b)
% if opt=2, exponential model (y=a*exp(b*x))
% if opt=3, logarithmic model (y=a+b*ln(x))
function [a, b]=LS_solver(x, y, opt)
format
[m1, n1]=size(x); [m2, n2]=size(y); % Size of the input data.
xx=linspace(min(x), max(x), 100); % xx will be used to plot the fitting curve.
if (n1~=1)||(n2~=1)||(m1~=m2) % If the input data size is not proper,
fprintf('Error: Improper input data.\n'); % error message.
elseif (opt==1)||(opt==2)||(opt==3) % option = 1, 2, 3.
figure; plot(x, y,'o'); % Plot the given data points.
hold on; % Ready to draw the next graph.
switch opt
case 1 % Linear model
fprintf('Linear model\n');
ft=fittype('a*x+b'); % creating a fit type with linear equation
sol=fit(x,y,ft); % fitting the curve
a=sol.a;
b=sol.b; % Fitting constants a and b.
plot(xx, a*xx+b); % Plot the fitting curve with a and b.
title('Linear model (y=a*x+b)');
case 2 % Exponential model
fprintf('Exponential model\n');
ft=fittype('a*exp(b*x)'); % creating a fit type with exponential equation
sol=fit(x,y,ft); % fitting the curve
a=sol.a;
b=sol.b; % Fitting constants a and b.
plot(xx, a*exp(b*xx)); % Plot the fitting curve with a and b.
title('Exponential model (y=a*exp(b*x))');
case 3 % Logarithmic model
fprintf('Logarithmic models\n');
ft=fittype('a+b*log10(x)'); % creating a fit type with logarithmic equation
sol=fit(x,y,ft); % fitting the curve
a=sol.a;
b=sol.b; % Fitting constants a and b.
plot(xx, a+b*log10(xx)); % Plot the fitting curve with a and b.
title('logarithmic model (y=a+b*ln(x))');
end
xlabel('x');ylabel('y');grid;grid minor;
legend('y','y_{fit}');
hold off; % no more graph.
else % for invalid [opt]
fprintf('Error: Improper option value.\n'); % error message.
return; % Return the process.
end
end
%%
%a).
x=[2 3 4 5 6 7 8 9]';
y=[1.75 1.91 2.03 2.13 2.22 2.30 2.37 2.43]';
[a,b]=LS_solver(x,y,1)
%%
% b).
x=[0 1 2 3 4 5 6 7]';
y=[3.9 5.3 7.2 9.6 12 17 23 31]';
[a,b]=LS_solver(x,y,2)
%%
% c).
x=[2 3 4 5 6 7 8 9]';
y=[4.07 5.30 6.21 6.79 7.32 7.91 8.23 8.51]';
[a,b]=LS_solver(x,y,3)


Related Solutions

EENG 1910: Project I – Introduction to Electrical Engineering Assignment-8 1. Create a MATLAB function that...
EENG 1910: Project I – Introduction to Electrical Engineering Assignment-8 1. Create a MATLAB function that will take as inputs the radius (r) and height (h) of a cone and calculate its volume. The formula to compute the volume of a cone is as follows. 1 ? = 3 ??2ℎ Write a descriptive comment on the use of the function so that the user by typing help nameofyourfunction has all the necessary information to use your function. In each of...
Chapter 3.6, Problem 20E in Introduction to Linear Algebra (5th Edition) Find the basis for the...
Chapter 3.6, Problem 20E in Introduction to Linear Algebra (5th Edition) Find the basis for the null space and the range of the given matrix. Then use Gram-Schmidt to obtain the orthagonal bases. 1 3 10 11 9 -1 2 5 4 1 2 -1 -1 1 4
Chapter 8 Case Please submit this assignment as a Text Submission using the "Write Submission" button....
Chapter 8 Case Please submit this assignment as a Text Submission using the "Write Submission" button. Submissions attached as a separate file will not be graded! Englewood Company has an opportunity to produce and sell a revolutionary new smoke detector for homes. To determine whether this would be a profitable venture, the company has gathered the following data on probable costs and market potential: New equipment would have to be acquired to produce the smoke detector. The equipment would cost...
ACCOUNTING 3220 CORPORATE FINANCIAL REPORTING 1 Spring 2018 Assignment #9 – Noncurrent Liabilities This assignment is...
ACCOUNTING 3220 CORPORATE FINANCIAL REPORTING 1 Spring 2018 Assignment #9 – Noncurrent Liabilities This assignment is due at 9:00am Monday, April 30, 2018 regardless of your section. The assignment must be submitted electronically via D2L. DO NOT EMAIL ME YOUR HOMEWORK. You may submit your homework in Excel, Word, or .pdf style (not .zip or other). Please format the pages for printing and clearly state all group members’ names and section numbers on the first page. The assignment may be...
The following sample was taken: 1, 9, 11, 3, 12, 15, 16, 8, 7, 8, 12,...
The following sample was taken: 1, 9, 11, 3, 12, 15, 16, 8, 7, 8, 12, 3, 2, 6, 6, 7, 8, 10, 9, 5, 6, 7, 8, 6, 8, 7 a. Calculate the average b. Estimate the variance c. Build a 95% confidence interval for the standard deviation. Please, give your answer by hand. Thanks in advance.
FIN 390 Assignment – Spring 2018 Capital Budgeting Mini Case Instructions: The assignment is based on...
FIN 390 Assignment – Spring 2018 Capital Budgeting Mini Case Instructions: The assignment is based on the mini case below. The instructions relating to the assignment are at the end of the case. Samantha Groves and Harry Finch are facing an important decision. After having discussed different financial scenarios into the wee hours of the morning, the two computer engineers felt it was time to finalize their cash flow projections and move to the next stage – decide which of...
Assignment 1 (assessment worth 10%) Due Date Monday 8th May by 5pm GMT+8 [Submission will be...
Assignment 1 (assessment worth 10%) Due Date Monday 8th May by 5pm GMT+8 [Submission will be strictly observed. Make submission via Turnitin] Question 1 An Australian investor holds a one month long forward position on USD. The contract calls for the investor to buy USD 2 million in one month’s time at a delivery price of $1.4510 per USD. The current forward price for delivery in one month is F= $1.5225 per USD. Suppose the current interest rate interest is...
Shoe Size 12 6 11 13 8 9 8 8 9 9 11 5 10 8...
Shoe Size 12 6 11 13 8 9 8 8 9 9 11 5 10 8 7 7 11 9 9 9 12 8 8 8 12 9 11 8 11 8 13 5 9 8 11 We need to find the confidence interval for the SHOE SIZE variable. To do this, we need to find the mean and standard deviation with the Week 1 spreadsheet. Then we can the Week 5 spreadsheet to find the confidence interval. This does...
ACCY 207 EXCEL ASSIGNMENT #2 Spring 2018 CHECK FIGURES: What If #1: NOI $630,000                             &nbsp
ACCY 207 EXCEL ASSIGNMENT #2 Spring 2018 CHECK FIGURES: What If #1: NOI $630,000                                                            Increase in NOI $180,000 REQUIREMENTS: Use the data in the posted problem for this Excel assignment. For the Year Ended December 31, 2017 prepare a Contribution Income Statement for CedarWorks using the following format for your data block page: CedarWorks For the Year Ended December 31, 2017 Operating results: Original What If #1 What If #2 What If #3 Unit selling price $3,000.00 $ $...
Assignment #10 Chapters 11 & 12 1. The mother of a child who was just diagnosed...
Assignment #10 Chapters 11 & 12 1. The mother of a child who was just diagnosed with type 1 diabetes turns to the child’s father and says, “This is all your fault. Your father has type 2 diabetes.” Is she correct in her assumption that the child inherited diabetes from his father’s family? Explain your response 2. Why sickle cell disease/trait and cystic fibrosis are considered probable genetic evolutionary disorders? 3. Why is carrier status for Duchenne muscular dystrophy (DMD)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT