In: Electrical Engineering
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 |
% --------- 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)