MATLAB polyfit, palyval not working correctly

28 views (last 30 days)
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!

Accepted Answer

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

More Answers (0)

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!