Reputation: 1357
Here is my code:
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-')
Here is what I have got:
Shouldn't the green line follow the rest of all the points pretty well? I cannot figure out where I did wrong. thanks.
Upvotes: 1
Views: 1105
Reputation: 6424
your getting the wrong variable, you may edit your code as follows:
p= polyfit(y,x,1);
t=1:1:40;
hold on;plot(x,polyval(p,x),'r');
Upvotes: 1
Reputation: 112659
You have two issues:
x
and y
are interchanged in your line containing polyfit
.polyfit
doesn't give a polynomial in x
. To obtain a polynomial in x
you use the two-output version.From the documentation (format added to highlight the two above issues):
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) [...]
So, change your line
[p,S,mu] = polyfit(y,x,1);
to
[p,S] = polyfit(x,y,1);
Resulting image:
Upvotes: 1