Curve-Fit Function USING MATLAB
Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features:
The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default).
The data files will be text files in the following format:
x1 y1
x2 y2
x3 y3
::
xn yn
The output shall be a formatted plot of the data points (markers) and the best fit polynomial curves (color-keyed lines). The plots shall include a legend that displays the formatted polynomial(s) expressions. The plot shall be complete with titles, labels and appropriate formatting.
The first line of the plot title shall be “Assignment 8-2, your name, date and time stamp.” The second line shall include the name of the data file read. For the multiple plot case where subplot is used, the title shall only be added to the top plot.
The function shall complete the plots. No user interactive formatting using the Property Editor shall be needed.
The function shall be robust in that it includes error-checking and verification of user inputs (existence of data file, valid input arguments, etc.)
a) Before attempting to open the data file, check to verify it exists:
if exist(myfile,’file’) == 0;
% display an error message and halt execution of the program
error('File Does Not Exist'); end
b) Use fscanf to read the data file.
c) Use the length of the vector of polynomial degrees to be fitted to determine the number of plots required in addition to plotting of the data points.
d) Use a switch construct to format the plots as a single plot or multiple plots (subplots). Use linspace to create a large array of x-values for smooth plotting:
e) Use an array of characters to specify the colors to be used for each fitted curve:
x = linspace(min(xdata),max(xdata),1000);
f) Use an array of characters to specify the colors to be used for each fitted curve:
mycolor = [ 'r';'g';'b';'c';'m';'y';'k'];
then use mycolor(i) in the plot command inside the plotting loop to specify the color to be used for each curve.
g) For the case with all curves on a single plot, generate an array of strings inside the loop to be used after the loop to create the legend:
legendtext(i+1,:) = ['Polynomial of Degree ' num2str(n(i))];
h) Upon completion of the loop that plots the curves, use the legend command:
legend(legendtext,'location','best');
i) For the case with multiple plots (subplots), the legend command can be used inside
the loop since there is only one curve in each plot.
j) To create the two-line title, consider using a statement similar to:
title({'Assignment 8-3 – your name, ' datestr(now); titletext});
`Hey,
Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.
clc
clear all
close all
format short
function func(myfile,v,ch)
mycolor = [ 'r';'g';'b';'c';'m';'y';'k'];
if exist(myfile,'file') == 0;
% display an error message and halt execution of the program
error('File Does Not Exist');
end
A=load(myfile);
x=A(:,1);
y=A(:,2);
switch(ch)
case 's'
hold on;
for i=1:length(v)
C=polyfit(x,y,v(i));
xx=linspace(min(x),max(x));
yy=polyval(C,xx);
plot(xx,yy,mycolor(randi([1,7],1,1)));
legendtext(i,:) = ['Polynomial of Degree ' num2str(n(i))];
end
legend(legendtext);
case 'm'
for i=1:length(v)
C=polyfit(x,y,v(i));
xx=linspace(min(x),max(x));
yy=polyval(C,xx);
subplot(ceil(length(v)/2),2,i)
plot(xx,yy,mycolor(randi([1,7],1,1)));
title({'Assignment 8-3 – your name, ' datestr(now); ['Polynomial of Degree ' num2str(v(i))]});
end
end
end
Kindly revert for any queries
Thanks.
Get Answers For Free
Most questions answered within 1 hours.