polyfit() 不遵循数据
polyfit() not following the data
这是我的代码:
clear all
close all
clc
%% abdomen
apAbdWat = [5.7 7.4 11.2 14.9 18.6 22.4 26.1 29.8 33.6];
latAbdWat = [7.7 10 15 20 25 30 35 40 45];
apTisAbd = [8.9 11.4 13.9 15.9 18.4 22 24.9 30.7];
latTisAbd = [10.6 14 18 20.6 24 30 32.4 38.9];
apAnthAbd = [20.8];
latAnthAbd = [28.3];
figure
plot(apAbdWat, latAbdWat, '-+'); hold on;
plot(apTisAbd, latTisAbd, 'r-o'); hold on;
plot(apAnthAbd, latAnthAbd, '-k*')
xlabel('AP diameter (cm)')
ylabel('LAT diameter (cm)')
title('AP diameter vs. LAT diameter (abdomen phantom) ')
legend('water abdomen', 'tissue abdomen', 'anthrop abdomen');
x = [apAbdWat apTisAbd apAnthAbd];
y = [latAbdWat latTisAbd latAnthAbd];
[p,S,mu] = polyfit(y,x,1);
hold on; t = 1:40;
plot(t,p(1)*t+p(2), 'g-')
这是我得到的:
绿线不应该很好地跟随所有点的其余部分吗?我不知道我哪里做错了。谢谢。
您有两个问题:
来自文档(添加格式以突出上述两个问题):
POLYFIT Fit polynomial to data.
P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) [...]
[P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a
structure S [...]
[P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in
XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X) [...]
所以,改变你的线路
[p,S,mu] = polyfit(y,x,1);
到
[p,S] = polyfit(x,y,1);
结果图像:
你得到的变量有误,你可以按如下方式编辑你的代码:
p= polyfit(y,x,1);
t=1:1:40;
hold on;plot(x,polyval(p,x),'r');
这是我的代码:
clear all
close all
clc
%% abdomen
apAbdWat = [5.7 7.4 11.2 14.9 18.6 22.4 26.1 29.8 33.6];
latAbdWat = [7.7 10 15 20 25 30 35 40 45];
apTisAbd = [8.9 11.4 13.9 15.9 18.4 22 24.9 30.7];
latTisAbd = [10.6 14 18 20.6 24 30 32.4 38.9];
apAnthAbd = [20.8];
latAnthAbd = [28.3];
figure
plot(apAbdWat, latAbdWat, '-+'); hold on;
plot(apTisAbd, latTisAbd, 'r-o'); hold on;
plot(apAnthAbd, latAnthAbd, '-k*')
xlabel('AP diameter (cm)')
ylabel('LAT diameter (cm)')
title('AP diameter vs. LAT diameter (abdomen phantom) ')
legend('water abdomen', 'tissue abdomen', 'anthrop abdomen');
x = [apAbdWat apTisAbd apAnthAbd];
y = [latAbdWat latTisAbd latAnthAbd];
[p,S,mu] = polyfit(y,x,1);
hold on; t = 1:40;
plot(t,p(1)*t+p(2), 'g-')
这是我得到的:
绿线不应该很好地跟随所有点的其余部分吗?我不知道我哪里做错了。谢谢。
您有两个问题:
来自文档(添加格式以突出上述两个问题):
POLYFIT Fit polynomial to data.
P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) [...]
[P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a structure S [...]
[P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X) [...]
所以,改变你的线路
[p,S,mu] = polyfit(y,x,1);
到
[p,S] = polyfit(x,y,1);
结果图像:
你得到的变量有误,你可以按如下方式编辑你的代码:
p= polyfit(y,x,1);
t=1:1:40;
hold on;plot(x,polyval(p,x),'r');