can't find line of best fit from simple code

2 次查看(过去 30 天)
I can't get a line of best fit to my data using the code from the matlab polyfit page. Does anyone know what might be going wrong?
power=[70,56,42,28];
depth90_09 =[2.9000, 3.0000, 4.4000, 1.7000];
Fit = polyfit(power,depth90_09,2);
plot(polyval(Fit,power))
hold on
scatter(power,depth90_09)
Cheers!

采纳的回答

Star Strider
Star Strider 2022-1-20
The first argument to plot must be the independent variable vector. When I added that, it works!
(I broke out the polyval call as a separate assignment, for clarity.)
power=[70,56,42,28];
depth90_09 =[2.9000, 3.0000, 4.4000, 1.7000];
Fit = polyfit(power,depth90_09,2);
Val = polyval(Fit, power);
figure
plot(power, Val)
hold on
scatter(power,depth90_09, 'filled')
.
  5 个评论
Em
Em 2022-1-28
编辑:Image Analyst 2022-1-28
Thank you for all your help Star Strider! For some reason my lines of best fit don't seem to be accurate for some of the data sets. Do you know why that might be?
Here is my code:
power=[70,56,42,28];
depth90_09 =[2.9000, 3.0000, 4.4000, 1.7000];
depth50_09 =[4.3,3.4,1.8,1.4];
depth50_08 =[3,2.3,1.2,1.5];
depth50_07 =[3.3,3.3,1.2,1.6];
depth50_06 =[4,2.6,2.5,0.5];
depth50_05 =[2.7,2.7,1.8,1];
depth50_04 =[3.5,2.3,1.1,0.7];
depth50_03 =[2.8,1.4,0.7,1];
depth50_02 =[0.6,1.1,0.7,0];
power_v = linspace(min(power), max(power), 150);
Fit = polyfit(power,depth90_09,2);
Valdepth90_09 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth90_09)
hold on
scatter(power,depth90_09, 'filled')
%%%%%%
Fit = polyfit(power,depth50mms_09,2);
Valdepth50mms_09 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth50mms_09)
hold on
scatter(power,depth50_09, 'filled')
%%
Fit = polyfit(power,depth50mms_08,2);
Valdepth50mms_08 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth50mms_08)
hold on
scatter(power,depth50_08, 'filled')
%%
Fit = polyfit(power,depth50mms_07,2);
Valdepth50mms_07 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth50mms_07)
hold on
scatter(power,depth50_07, 'filled')
%%
Fit = polyfit(power,depth50mms_06,2);
Valdepth50mms_06 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth50mms_06)
hold on
scatter(power,depth50_06, 'filled')
%%
Fit = polyfit(power,depth50mms_05,2);
Valdepth50mms_05 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth50mms_05)
hold on
scatter(power,depth50_05, 'filled')
%%
Fit = polyfit(power,depth50mms_04,2);
Valdepth50mms_04 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth50mms_04)
hold on
scatter(power,depth50_04, 'filled')
%%
Fit = polyfit(power,depth50mms_03,2);
Valdepth50mms_03 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth50mms_03)
hold on
scatter(power,depth50_03, 'filled')
%%
Fit = polyfit(power,depth50mms_02,2);
Valdepth50mms_02 = polyval(Fit, power_v);
figure
plot(power_v, Valdepth50mms_02)
hold on
scatter(power,depth50_02, 'filled')
Cheers
Image Analyst
Image Analyst 2022-1-28
@Em your code throws an error. What is the value of
depth50mms_09
Are you sure you want a fit and not an interpolation? The quadratic fit looks reasonable to me.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by