Develop a program that gets data again from the mat file and then Plots the indi
ID: 3864730 • Letter: D
Question
Develop a program that gets data again from the mat file and then Plots the individual points with 'o' circle marks Fits a cubic spline to the data, use "star" marks Fits a linear least square curve, use "diamond" marks Fits a 7th order polynomial, use "x" marks Plot all curves simultaneously using different symbols for the plots. Be sure to axis to expand the range for better visibility of the final graph. See also help plot Do this in an m-file and attach the final graph. Answer this question. Which curve fits the data the best and why?Explanation / Answer
% Since you have not attached .mat file, i am assuming default names for the file and variables
% change the file name and variables names according to your data
filename = 'example.mat';
variables = {'x','y'};
structuredArr = load(filename, variables{:})
x = structuredArr.x;
y = structuredArr.y;
%can also use scatter(x,y,25,'filled','o') to plot
plot(x, y, 'or');
hold on; % will plot multiple plots in a single graph
% fitting cubic spline to the data
xx = linspace(min(x), max(x));
yy = spline(x,y,xx);
plot(x, y,':*b', xx, yy);
hold on;
% linear least square fitting
plot(x, y, '-dg');
hold on;
% fitting 7th degree polynomial
p = polyfit(x,y,7);
x1 = linspace(min(x), max(x));
y1 = polyval(p,x1);
plot(x1, y1, '--xk');
hold off;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.