Question

Example 1: Least Squares Fit to a Data Set by a Linear Function. Compute the coefficients of the best linear least-squares fi

Example 1: Least Squares Fit to a Data Set by a Linear Function. Compute the coefficients of the best linear least-squares fit to the following data. x2.4 3.6 3.64 4.7 5.3 y| 33.8 34.7 35.5 36.0 37.5 38.1 Plot both the linear function and the data points on the same axis system Solution We can solve the problem with the following MATLAB commands x[2.4;3.6; 3.6;4.1;4.7;5.3]; y-L33.8;34.7;35.5;36.0;37.5;38.1 X [ones ( size (x)),x); % build the matrix X for linear model % build vector of x -values % build vector of y -values % right hand side of the Normal Equations % Left hand side of the Normal Equations % Cholesky decomposition Uchol (S); % solve the normal equations using Cholesky decomposition plot(x,y,'o') q2:0.1:6; fit-c(1)+c (2)#4; hold on plot (q,fit, 'r'); % plot the data points % define a vector for plotting the linear fit % define the linear fit % plot the linear fit together with the data points Running the script file produces the output 29.6821 1.5826 and the following figure.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

clear all
clc
format short e
dat=load('gco2.dat');
year=dat(:,1);
conc=dat(:,2);
A=[ones(length(year),1) year];
b=conc;
x=A\b;
disp('For linear fit')
c1=x(1)
c2=x(2)
q=min(year):max(year);
yy=polyval(x(end:-1:1),q);
plot(year,conc,'o',q,yy,'LineWidth',2);
A=[ones(length(year),1) year year.^2];
b=conc;
x=A\b;
disp('For quadratic fit')
c1=x(1)
c2=x(2)
c3=x(3)
yy=polyval(x(end:-1:1),q);
hold on;
plot(q,yy,'LineWidth',2);
legend('data points','Linear fit','quadratic fit','location','northwest');

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Example 1: Least Squares Fit to a Data Set by a Linear Function. Compute the coefficients of the ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT