Borrar filtros
Borrar filtros

Plotting the third and firth order polynomial of a function?

6 visualizaciones (últimos 30 días)
Matt Amador
Matt Amador el 20 de Oct. de 2017
Comentada: Image Analyst el 21 de Oct. de 2017
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 comentario
Jan
Jan el 20 de Oct. de 2017
Please do not write a shortened rephrased error message, but a complete copy.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 20 de Oct. de 2017
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 comentarios
Jan
Jan el 21 de Oct. de 2017
@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.
Image Analyst
Image Analyst el 21 de Oct. de 2017
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Historical Contests en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by