MATLAB polyfit, palyval not working correctly

28 ビュー (過去 30 日間)
Christina Kersten
Christina Kersten 2019 年 3 月 8 日
コメント済み: Christina Kersten 2019 年 3 月 8 日
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..
  1 件のコメント
Christina Kersten
Christina Kersten 2019 年 3 月 8 日
Oh my bad that was a dumb mistake. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet 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