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 LS.m 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 3 polynomials in the same plot together with your data set. Titleand labels are required.
function [an]= myLS2136(xp, yp, n, noplot)
%% least_squares(x, y, m) fits a least-squares polynomial of degree m through
% a set of data where x and y are the coordinates.
% the set of coefficients c0, c1,...,cm
% for the least-square polynomial Pm(x)= c0+ c1*x + c2*x^2+...+ cm*x^m
% Input:
% xp - x coordinates
% yp - y coordinates
% n - maximum power of the polynomial
% Example:
%[an]= myLeastSquares(xp, yp, n)
%
% Date: 30/05/2019
% Author: Dr. Hien Nguyen
% RMIT University
%--------------------------------------------------------------------------
m = size(xp,1); % checking the size of dataset
if m ==1
m = size(xp,2);
end
disp('');
disp(' LEAST-SQUARES METHOD');
disp('---------------------------');
% fill in display Number of sample points is m+1
% fill in display Number of polynomial degree is n
if (n>= m)
% what happen if n>= m?
end
%% compute coefficients.
b = zeros(n+1,1); % right-hand side column vector
for i =1:m
for j =1:n+1
% fill in your code
end
end
V = zeros(m,n+1); % V matrix
for i =1:m
for j =1:n+1
% fill in your code
end
end
% fill in your code % S matrix
an = S\b; % solve for coefficient
% Using symbol tool box to display the resulted polynomial
syms x
% fill in your code
% format short
%% Display output
disp('------------------------');
fprintf(' a_n(x):
')
disp(an);
disp('------------------------');
disp(y);
%% creating an x-axis and evaluating the least-ssquare polynomial
if (noplot ==1)
% 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!