![untitled.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207561/untitled.jpeg)
MATLAB polyfit, palyval not working correctly
조회 수: 13 (최근 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!
댓글 수: 0
채택된 답변
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207561/untitled.jpeg)
What you did, as you did it, makes no sense in context of the model you built..
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!