By default MATLABs polyfit will always calculate the full po

By default, MATLAB\'s polyfit will always calculate the full polynomial fit. (i.e., Ax + B, Ax^2 + Bx + C, Ax^3 + Bx^2 + C x + D, etc.) Write a function called \"polyfit2\" that will allow the user to specify what terms to activate. For example... [Coefficients] = polyfit2(x, y, [1 0]) % Fit Ax^1 + Bx^0 [Coefficients] = polyfit2(x, y, 12 1]) % Fit Ax^2 + Bx^1 [Coefficients] = polyfit2(x, y, [3 1 0]) % Fit Ax^3 + Cx^1 + Dx^0 Use your function in a script file with the same data from the in-class example in the notes. Recreate the plot as well.

Solution

x = linspace(0,4*pi,10);

y = sin(x);

coef=polyfit2(x,y,[5 1]);

disp(coef)

function [Coefficients] =polyfit2(x,y,coeff_mat)

n=max(coeff_mat);

p = polyfit(x,y,n)

Coefficients=zeros(1,n+1);

k=1;

for i=n:-1:1

if (i==coeff_mat(k))

Coefficients(i) =p(i);

k=k+1;

else

Coefficients(i) =0;

  

end

end

Output :

Rum_polyfit2
0.0013 0 0 0 2.8327 0

 By default, MATLAB\'s polyfit will always calculate the full polynomial fit. (i.e., Ax + B, Ax^2 + Bx + C, Ax^3 + Bx^2 + C x + D, etc.) Write a function called

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site