Frage

I am using Matlab to fit some data in 2 coordinates (x,y) with a poly1 curve.

The problem is that I can't find a way to make the fitting line longer.

I need it from (180, 930) to (191, 944), but instead Matlab just draw the fitting line near the data, which is between those two coordinates.

Is there some argument to the fit command (or some preferences in the cftool) that can help me out?

Moreover, I've tried the "Adjust axes limits" option in the cftool, but it didn't help at all.

I've searched through the already asked questions, but I haven't found anything related to this. I'm new to this program, therefore I'm sorry if this is a stupid question Thanks in advance, Giovanni

EDIT: The code for the first image is:

[FitUp,goodnessUP] = fit(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,2),'poly1')
[FitDown,goodnessDOWN] = fit(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,3),'poly1')
plot(FitUp,'b')
hold on
plot(FitDown,'b')
hold on
errorbar(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,2),AKaterMatrix1msDX(:,4),'--r')
hold on
errorbar(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,3),AKaterMatrix1msDX(:,4),'--r')

The code for the second is:

[FitUp,goodnessUP] = fit(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,2),'poly1')
[FitDown,goodnessDOWN] = fit(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,3),'poly1')
plot(FitDown,'b')
hold on
plot(FitUp,'b')
hold on
errorbar(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,2),AKaterMatrix1msDX(:,4),'--r')
hold on
errorbar(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,3),AKaterMatrix1msDX(:,4),'--r')

Here you can find the two fits, it appears that the first fit is not cropped, while the second after the hold on is: https://docs.google.com/file/d/0B749BCu7mnZHaEhITUZ1YzdfVDA/edit?usp=sharing https://docs.google.com/file/d/0B749BCu7mnZHeDVTOGRuSkktUmc/edit?usp=sharing

War es hilfreich?

Lösung

You just need to be careful when and how you set the hold. First make some dummy data

AKaterMatrix1msDX(:, 1) = 185:189;
AKaterMatrix1msDX(:, 2) = 2*rand(5, 1)+933;
AKaterMatrix1msDX(:, 3) = 2*rand(5, 1)+940;
AKaterMatrix1msDX(:, 4) = 2*rand(5, 1);

Next, and this is the key part, set the axis to be what you want and turn hold on

figure
axis([180, 191, 930, 944]);
hold on

This do exactly what you did

[FitUp,goodnessUP] = fit(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,2),'poly1')
[FitDown,goodnessDOWN] = fit(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,3),'poly1')
plot(FitUp,'b')
hold on
plot(FitDown,'b')
hold on
errorbar(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,2),AKaterMatrix1msDX(:,4),'--r')
hold on
errorbar(AKaterMatrix1msDX(:,1),AKaterMatrix1msDX(:,3),AKaterMatrix1msDX(:,4),'--r')

enter image description here

Andere Tipps

If you don't need a lot of fit statistics, polyfit followed by polyval can give you your fit:

X = AKaterMatrix1msDX(:,1);
Y = AKaterMatrix1msDX(:,2);
dY = AKaterMatrix1msDX(:,4);
[a,S] = polyfit(X,Y)
extraPlotRange = 10;
newX = linspace(min(X)-extraPlotRange,max(X)+extraPlotRange,100);

[fitY,delta] = polyval(a,newX);

plot(X,Y)
hold on
plot(newX,fitY)
plot(newX,fitY+delta,':b')
plot(newX,fitY-delta,':b')
errorbar(X,Y,dY,'--r')
hold off

This will not, unfortunately, give you the same goodness of fit statistics that you might need, only the confidence intervals of the fit.

The other option, if you want to stay with fit, would be to get the fit coefficients using coeffvalues. Those fit coefficients would be the same as you get from polyfit.

aUp = coeffvalues(FitUp);
aDown = coeffValues(FitDown);

fitYup = polyval(aUp,newX);
fitYdown = polyval(aDown,newX);

etc.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top