Plotting the third and firth order polynomial of a function?

Hello, I have some trouble with my code. The objective of it is to fit the following data using a third and fifth order polynomial and plot the fits over the range 0<=t<=10
Data: >>t = 0:10;
>> y = [0 0.5104 0.3345 0.0315 -0.1024 -0.0787 ...
-0.0139 0.0198 0.0181 1.0046 -0.0037];
All of this data comes from the function: y(t) = e^(-0.5*t) * sin(t)
Here is my code so far:
clc
clear
t = 0:10;
y = exp(-0.5.*t)*sin(t);
p1 = polyfit(t,y,3);
p2 = polyfit(t,y,5);
plot(p1)
hold on
plot(p2)
grid on
I'm getting an error saying that the inner matrix dimensions must agree. Can anyone help out?

1 Commento

Please do not write a shortened rephrased error message, but a complete copy.

Accedi per commentare.

Risposte (1)

t = 0:10;
y = exp(-0.5 .* t) .* sin(t); % Elementwise .* instead of matrix multiplication *
Read the documentation of polyfit. You will find out, that the coefficients of the polynomial are replied. It is not meaningful to plot them. But polyval let you obtain the curves again. Use a finer grid then, e.g. tt = 0:0.1:10.

5 Commenti

clc
clear
t = 0:1:10;
y = exp(-0.5.*t).*sin(t);
p1 = polyfit(t,y,3);
p2 = polyfit(t,y,5);
plot(p1, 'b-+')
hold on
plot(p2, 'r-o')
grid on
This is what I got, but i'm not so sure if this is right..
No, these are the coefficients of the polynomial. It is not meaningful to draw them as a line. Come on, please follow my advice and read the documentation of polyfit:
doc polyfit
Or use the online doc: https://www.mathworks.com/help/matlab/ref/polyfit.html. It contains an example of how to draw a fit using polyval. You need something like this:
You forgot to use polyfit. Here's a start. It should be easy to add the 5th order polynomial fit.
t = 0 : 10;
y = exp(-0.5 .* t) .* sin(t);
p1 = polyfit(t,y,3)
p2 = polyfit(t,y,5)
tFit = linspace(0,10, 1000);
yFit1 = polyval(p1, tFit);
plot(t, y, 'bd', 'LineWidth', 2)
hold on;
plot(tFit, yFit1, 'r-', 'LineWidth', 2)
hold on
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
legend('Training data', 'Order 3 fit');
@Image Analyst: You mean "polyval", not "polyfit".
@Matt Amador: I assumed, that this is a homework and did not post a working solution. If this assumption was wrong: Sorry, I did not want to conceal the solution, but to let you the chance to solve it by your own.
Jan, right - polyval (right in the code, wrong in the description - sorry).
Matt, if someone posts homework, the expectation is that they label it as homework so people don't give a complete solution, lest they get caught for plagiarism and get in trouble. However, I gave you partial code based on your original code (plus a few other fancy things) so it might be okay. But if it's homework, the safest thing to do is to label it as homework. We wouldn't want you to get in trouble.

Accedi per commentare.

Categorie

Richiesto:

il 20 Ott 2017

Commentato:

il 21 Ott 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by