Question

Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...

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:

  1. 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).

  2. The data files will be text files in the following format:

    1. x1 y1

    2. x2 y2

    3. x3 y3

    ::

xn yn

  1. 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.

  2. 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.

  3. The function shall complete the plots. No user interactive formatting using the Property Editor shall be needed.

  4. 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});

Homework Answers

Answer #1

`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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please use Mat lab only. Thanks (b) Plotting Sunspots Load the Matlab data file, sunspot.dat, and...
Please use Mat lab only. Thanks (b) Plotting Sunspots Load the Matlab data file, sunspot.dat, and experiment with making plots and subplots. The file is a simple 288 row by 2 column matrix where the first column has consecutive years from 1700 until 1987 and the second column has the mean sunspot number for that year. Then we will construct year and spots vectors from the two columns and plot spots as a function of year. load sunspot.dat; year =...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
3.   Consider the following data set for an office structure built by Anderson Construction Co. The...
3.   Consider the following data set for an office structure built by Anderson Construction Co. The completed building is nine stories. However, construction was interrupted by a fire after 5.3357 floors were completed. At the time of the fire, Anderson had used 54,067 hours of labor to construct the first 5.3357 stories of the building. It then took Anderson an additional 40,750 labor hours to complete this nine-story building. In this problem, FLRCOM is the number of floors completed, and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT