Question: Write a MATLAB program to fine the line of best fit. Your program consists of a LS . m to find the the line of
Write a MATLAB program to fine the line of best fit. Your program consists of a LSm to find the the line of best fit. In this function, the input parameters include
the data points xp yp and n the power of the line of best fit. A skeleton code for this problem will be below The use of the skeleton code is not mandatory.
The outputs printouts consist of the coefficient an of the polynomial.
program by the work you done above. Plot the first polynomials in the same plot together with your data set. Titleand labels are required.
function an myLSxp yp n noplot
leastsquaresx y m fits a leastsquares polynomial of degree m through
a set of data where x and y are the coordinates.
the set of coefficients c ccm
for the leastsquare polynomial Pmx c cx cx cmxm
Input:
xp x coordinates
yp y coordinates
n maximum power of the polynomial
Example:
an myLeastSquaresxp yp n
Date:
Author: Dr Hien Nguyen
RMIT University
m sizexp; checking the size of dataset
if m
m sizexp;
end
disp;
disp LEASTSQUARES METHOD';
disp;
fill in display Number of sample points is m
fill in display Number of polynomial degree is n
if n m
what happen if n m
end
compute coefficients.
b zerosn; righthand side column vector
for i :m
for j :n
fill in your code
end
end
V zerosmn; V matrix
for i :m
for j :n
fill in your code
end
end
fill in your code S matrix
an Sb; solve for coefficient
Using symbol tool box to display the resulted polynomial
syms x
fill in your code
format short
Display output
disp;
fprintf anx:
dispan;
disp;
dispy;
creating an xaxis and evaluating the leastssquare polynomial
if noplot
fill in your code to plot the polynomial
end
end
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
