Q:

Write a function that will receive data points in the form of x and y vectors. If the lengths of the vectors are not the same, then they can’t represent data points so an error message

0

 Write a function that will receive data points in the form of x and y vectors. If the lengths of the vectors are not the same, then they can’t represent data points so an error message should be printed. Otherwise, the function will fit a polynomial of a random degree through the points, and will plot the points and the resulting curve with a title specifying the degree of the polynomial. The degree of the polynomial must be less than the number of data points, n, so the function must generate a random integer in the range from 1 to n-1 for the polynomial degree. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

ranCurveFit.m

function ranCurveFit(x,y)

% Uses x,y input data vectors, performs a curve fit with a random

% polynomial degree. Function terminates if input vectors

% have different lengths.

% Format of call: ranCurveFit(x,y)

% Does not return any values

if length(x) ~= length(y)

 disp('Error! x and y must have the same length')

else

 n = randi([1,length(x)-1]);

 coefs = polyfit(x,y,n);

 curve = polyval(coefs,x);

 plot(x,y,'ko',x,curve)

 title(sprintf('Polynomial degree: %d',n))

end

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now