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 thisproblem.
A common problem in experimental work is to find a curve y =f(x) of a specified form corresponding to experimentally determinedvalues of x and y, say
(x1, y1), (x2, y2), · · · , (xn, yn). The followings are thefour 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 tothe proper mathematical model using least squares method. Thefunction 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 (SeeFigure 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 completethe missing parts.
(b) Use LS_solver.m to fit an exponential model to the followingdata (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 followingdata (Table 2), and graph the curve and data points in the samefigure.
Table 2: Data points of Problem 1-iii (logarithmic model)
You may use the backslash operator in MATLAB (syntax : A \ b fora linear system Ax = b) and refer to the T5 and T7 in Section 7.8of 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 (pdfformat is recom-
mended) on the Homework Box for MATLAB Submission Problem 3.
Print out your hardcopy solution and submit it to yourrecitation 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 |