Question

# N MATLAB: This is an easy problem to demonstrate how polyfit and polyval work! The values...

N MATLAB:

This is an easy problem to demonstrate how polyfit and polyval work!

The values in y are basically a sine function on x. You can validate by checking in MATLAB

>> x = 0:9

x =

0 1 2 3 4 5 6 7 8 9

>> y = sin(x)

y =

0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121

Now, let us try to see what happens if we use polyfit on the values of y and see whether 1st order, 2nd order, 3rd or 7th order polynomials can approximate the sine function

If you implement this correctly, you will see that polyfit works very well for interpolation, but not for extrapolation

SCRIPT:

x = [0 1 2 3 4 5 6 7 8 9];

y = [0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121];

%use polyfit to find the coefficients of 1st order polynomial using x and y, and store them in p1

p1 =

%use polyfit to find the coefficients of 2nd order polynomial using x and y, and store them in p2

p2 =

%Similarly do it for order 3, 7 and store it in p3, p7

p3 =

p7 =

% Now visualize the polynomials estimated

x1 = linspace(0,10);

y1 = polyval(p1, x1);

y2 = polyval(p2, x1);

y3 = polyval(p3, x1);

y7 = polyval(p7, x1);

subplot(2,2,1)

plot(x,y,'o')

hold on

plot(x1,y1)

hold on

plot(x1,sin(x1),'g--')

title(' p = 1')

hold off

subplot(2,2,2)

plot(x,y,'o')

hold on

plot(x1,y2)

hold on

plot(x1,sin(x1),'g--')

title(' p = 2')

hold off

subplot(2,2,3)

plot(x,y,'o')

hold on

plot(x1,y3)

hold on

plot(x1,sin(x1),'g--')

title(' p = 3')

hold off

subplot(2,2,4)

plot(x,y,'o')

hold on

plot(x1,y7)

hold on

plot(x1,sin(x1),'g--')

title(' p = 7')

hold off

Code:

clc
clear all

x = [0 1 2 3 4 5 6 7 8 9];

y = [0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121];

%use polyfit to find the coefficients of 1st order polynomial using x and y, and store them in p1

p1 = polyfit(x, y, 1)

%use polyfit to find the coefficients of 2nd order polynomial using x and y, and store them in p2

p2 = polyfit(x, y, 2)

%Similarly do it for order 3, 7 and store it in p3, p7

p3 =polyfit(x, y, 3)

p7 = polyfit(x, y, 7)

% Now visualize the polynomials estimated

x1 = linspace(0,10);

y1 = polyval(p1, x1);

y2 = polyval(p2, x1);

y3 = polyval(p3, x1);

y7 = polyval(p7, x1);

subplot(2,2,1)

plot(x,y,'o')

hold on

plot(x1,y1)

hold on

plot(x1,sin(x1),'g--')

title(' p = 1')

hold off

subplot(2,2,2)

plot(x,y,'o')

hold on

plot(x1,y2)

hold on

plot(x1,sin(x1),'g--')

title(' p = 2')

hold off

subplot(2,2,3)

plot(x,y,'o')

hold on

plot(x1,y3)

hold on

plot(x1,sin(x1),'g--')

title(' p = 3')

hold off

subplot(2,2,4)

plot(x,y,'o')

hold on

plot(x1,y7)

hold on

plot(x1,sin(x1),'g--')

title(' p = 7')

hold off

Output:

p1 =

0.0122 0.1405

p2 =

0.0449 -0.3916 0.6789

p3 =

0.0077 -0.0588 -0.0376 0.4854

p7 =

0.0000 -0.0001 -0.0096 0.1295 -0.5553 0.5249 0.7487 0.0004

#### Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
• ### please help me with this MATLAB CODE and explain to me what each line does and...

please help me with this MATLAB CODE and explain to me what each line does and what is used for? leave your comments as words, not as pictures. ..................................................................................................................................................................... clear all; close all; % For a script file, you better start with clear all and close all                        % However, for a fucntion, you better NOT to start                        % with them %% End of cell mode example %% Plot function t = 0:0.1:5; x1 = sin(2*5*t); x2 = cos(3*7*t);...

• ### Please help me with this MATLAB programming problem! Im coding iin MATLAB2018 if that makes any d...

Please help me with this MATLAB programming problem! Im coding iin MATLAB2018 if that makes any difference! The first picture is the question i need to answer. The picture below is used as reference to answer the question. The last picture (below) is the into to the problem, and is used as reference. 1. Use Matlab to create the following single plot with three subplots. All titles, gridlines, and axis labels should be as shown. Arc System Response 15 E...

• ### A group of physics students collected data from a test of the projectile motion problem that...

A group of physics students collected data from a test of the projectile motion problem that was analyzed in a previous lab exercise (L5). In their test, the students varied the angle and initial velocity Vo at which the projectile was launched, and then measured the resulting time of flight (tright). Note that tright was the dependent variable, while and Vo were independent variables. The results are listed below. (degrees) Time of Flight (s) Initial Velocity V. (m/s) 15 20...