MATLAB polyfit, palyval not working correctly

28 次查看(过去 30 天)
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!

采纳的回答

John D'Errico
John D'Errico 2019-3-8
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-')
untitled.jpg
What you did, as you did it, makes no sense in context of the model you built..

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by