In this question you are asked to write a Matlab code that can do polynomial int
ID: 3823257 • Letter: I
Question
In this question you are asked to write a Matlab code that can do polynomial interpolation between the data points with given x & y coordinates. a) Use your program to draw a 3^rd order polynomial through the specified points. As discussed, a 3rd order polynomial requires 4 different data points. Generate the coordinates of the points in the manner shown in the table below such that x-coordinates are uniformly distributed between 0 and 1 and the y-coordinates are selected randomly. Find the coefficients of the 3^rd order polynomial and plot it. y = a_0 + a_1 x + a_2 x^2 + a_3 x^3 Your plot should look like as the one below. b) Run the same program with 6 points and calculate the coefficients of the 5th order polynomial going exactly through the 6 data points with the green coordinates. c) Fit a linear function through data points generated in b). The linear function will not go exactly through the data points but it will be pass "nearby" the points in a sense that the error is minimized. To generate it use the "polyfit" function available in Matlab library Information about the usage of this function can be found in Matlab's help.Explanation / Answer
%%%%%%%%%%%%% MATLAB CODE %%%%%%%%%%%
%%%%%%%%%%%%%% (a) %%%%%%%%%%%%%%%%
x_coord =[0 0.33 0.66 1];
y_coord = (1).*rand(1,4); %randomly generated y b/w 0 and 1
polynomial = polyfit(x_coord,y_coord,3); % 3rd order polynomial fitting
figure('Name','3rd order polynomial','NumberTitle','off');
X = 0:.01:1;
Y = polyval(polynomial,X);
plot(X,Y,x_coord,y_coord,'o'); % Polynomial graph
legend('3rd order Polynomial','Data Points','Location','best');
xlabel('X') % x-axis label
ylabel('Y') % y-axis label
%%%%%%%%%%%%%% (b) %%%%%%%%%%%%%%%%
x_coord =[0 0.1 0.2 0.3 0.4 0.5];
y_coord = (0.5).*rand(1,6); %randomly generated y b/w 0 and 1
polynomial_5 = polyfit(x_coord,y_coord,5); % 5th order polynomial fitting
figure('Name','5th order polynomial','NumberTitle','off');
X = 0:.01:0.5;
Y = polyval(polynomial_5,X);
plot(X,Y,x_coord,y_coord,'o'); % Polynomial graph
legend('5th order Polynomial','Data Points','Location','best');
xlabel('X') % x-axis label
ylabel('Y') % y-axis label
%%%%%%%%%%%%%% (c) %%%%%%%%%%%%%%%%
%% data taken from above part(b) %%%%%%
Linear_fit = polyfit(x_coord,y_coord,1); % Linear fit
figure('Name','Linear function','NumberTitle','off');
X = 0:.01:0.5;
Y = polyval(Linear_fit,X);
plot(X,Y,x_coord,y_coord,'o'); % Polynomial graph
legend('Linear fit','Data Points','Location','best');
xlabel('X') % x-axis label
ylabel('Y') % y-axis label
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.