MATLAB polyfit, palyval not working correctly
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Christina Kersten
el 8 de Mzo. de 2019
Comentada: Christina Kersten
el 8 de Mzo. de 2019
Basically I'm just trying to curve fit some data but the line of best fit is not correct. Here is the code I have so far
clear all; close all
c = xlsread('Elastic Modulus.xlsx','Sheet1');
temp = c(:,1);
carbonSteel = c(:,2);
plot(temp,carbonSteel,'*')
hold on
p = polyfit(temp,carbonSteel,3);
y = polyval(p,carbonSteel);
plot(temp,y)
The temp and carbonsteel columns contain the values:
temp =
-200
-129
-73
21
93
149
204
260
316
371
427
482
538
593
carbonSteel =
31.4000
30.8000
30.2000
29.5000
28.8000
28.3000
27.7000
27.3000
26.7000
25.5000
24.2000
22.4000
20.4000
18.0000
When I plot it just shows a straight line. How do I fix this? Thank you!
0 comentarios
Respuesta aceptada
John D'Errico
el 8 de Mzo. de 2019
p = polyfit(temp,carbonSteel,3);
y = polyval(p,carbonSteel);
What did you build? A model, that predicts carbonsteel, as a function of temp. So temp is the INDEPENDENT variable.
Now, you want to predict the dependent variable, as as a function of temp. So you need to do this:
y = polyval(p,temp);
plot(temp,carbonsteel,'bo',temp,y,'r-')
What you did, as you did it, makes no sense in context of the model you built..
Más respuestas (0)
Ver también
Categorías
Más información sobre Get Started with Curve Fitting Toolbox 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!