How to fit an exponential curve with known error bars

66 vues (au cours des 30 derniers jours)
Avichay Ashur
Avichay Ashur le 23 Mai 2021
Commenté : Scott MacKenzie le 27 Mai 2021
Hello,
i have X,Y data that i want to exponential curve the data with errorbars though im strugeling to find the command to do so, whenever i try the basic errorbar command it gives me a plot but not a curved plot, is there any 1 to do the command i need?
thx in advance!
  2 commentaires
Scott MacKenzie
Scott MacKenzie le 23 Mai 2021
It would help if you showed your code, data, and the plot that your code currently generates.
Avichay Ashur
Avichay Ashur le 23 Mai 2021
X=[-504,-494,-490,-484,-478,-456,-436,-402,-364,-322]*10^-6
Y=[4.85,4.37,4.07,3.65,3.24,2.16,1.52,0.8,0.4,0.18] %[V] +-0.002[V]
errX=[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01]

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 23 Mai 2021
Some thoughts ...
Here is your data:
X=[-504,-494,-490,-484,-478,-456,-436,-402,-364,-322]*10^-6;
Y=[4.85,4.37,4.07,3.65,3.24,2.16,1.52,0.8,0.4,0.18]; %[V] +-0.002[V]
errX=[0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01];
First, those error bars are constant across X, but they are relatively HUGE, compared to the size of what is in X. As such, they would have almost no impact on the fit.
Next, you want to use the model
Y = a*exp(b*X)
If that model does fit, then suppose we log the model? We can see the impact of doing so by a semilogy plot. If that is a straight line, then your model is appropriate.
semilogy(X,Y,'o-')
grid on
xlabel 'X'
ylabel 'Y'
I see some tiny deviation from linearity at the left end, but a linear fit would do just fine in that domain. I'm actually surprised it was that close to a straight line.
We can do the fit on the logged model using a simple linear least squares (i.e., polyfit.) But since you are using the curve fitting toolbox, we can use that. First, I'll do the fit on the logged model. As a logged model, we would see:
log(Y) = log(a) + b*x
loggedmodel = fit(X',log(Y'),'poly1')
loggedmodel =
Linear model Poly1: loggedmodel(x) = p1*x + p2 Coefficients (with 95% confidence bounds): p1 = -1.835e+04 (-1.869e+04, -1.8e+04) p2 = -7.602 (-7.756, -7.448)
aest = exp(loggedmodel.p2)
aest = 4.9945e-04
best = loggedmodel.p1
best = -1.8348e+04
Again, this totally ignores those error bars. But your error bars are IMMENSE, compared to the size of the numbers in X.
As well, the above model presumes that the noise is in log(Y), NOT in X. Is this important here? Possibly not, since your data is so nicely linear in this domain.
Can we estimate the model in a nonlinear form? Of course. But still, I'll be ignoring those error bars for now, both since they are so huge, and they are constant for all X.
ft = fittype('exp1')
ft =
General model Exp1: ft(a,b,x) = a*exp(b*x)
mdl = fit(X',Y',ft,'start',[aest,best])
mdl =
General model Exp1: mdl(x) = a*exp(b*x) Coefficients (with 95% confidence bounds): a = 0.0008243 (0.0002759, 0.001373) b = -1.73e+04 (-1.866e+04, -1.594e+04)
plot(X,Y,'o')
hold on
plot(mdl)
grid on
As you can see, the linearized model estimates do lie within the confidence bounds we found for the nonlinear fit, however, they are not identical. A problem is, I don't know if your data really has errors in Y or in X. It looks like you have measured values on both axes. So I don't know how to best treat the estimation process.
  9 commentaires
Adam Danz
Adam Danz le 25 Mai 2021
The hard-problem is that Matlab has no way of knowing the intentions of the programmer so it chooses which function to run based on the Function Precedence Order (pardon the anthropomorphisms).
@gmdistribution/fit.m is designed to be run by fitgmdist() which calls the file explicitly using gm=gmdistribution.fit(X,k,varargin{:}) which is possible because @gmdistribution/fit.m is defined as a method in the class constructor gmdistribution.m. So fitgmdist() cannot accidentally choose another fit() function.
However, @gmdistribution/fit.m still resides on the matlab path and when no function named "fit" exists on the path and the user calls the fit() function, Matlab chooses the only one it knows, @gmdistribution/fit.m, unless the user has another function named fit.
When the Curve Fitting Toolbox is added to the Matlab path and the user directly calls fit(), Matlab chooses curvefit/fit.m since @gmdistribution/fit.m is buried in a method directory and is designed to be called explicitly.
I suppose the @gmdistribution/fit.m function could detect if input #2 is a vector of length size(x,1) in which case its error message could suggest that the curve fitting toolbox may be required since @gmdistribution/fit.m expects input #2 to be a positive integer. But too much defensive programming comes at the expense of run-time and maintenance costs so ultimately the user is responsible for knowing function precedences.
Fortunately, calling the wrong function usually produces an error message and after confirming that the inputs are all OK, I usually go directly to which <name> -all. It's a very frequent solution to many questions asked in the forum, primarily because users name custom functions things like sum or plot which is a whole other can of worms 😃.
Scott MacKenzie
Scott MacKenzie le 27 Mai 2021
Thanks, Adam. Excellent narative on this issue.

Connectez-vous pour commenter.

Plus de réponses (1)

Scott MacKenzie
Scott MacKenzie le 23 Mai 2021
The plot looks curved to me.
The error bars in your chart appear as horizontal lines because they are really small. Here's a tweak to your data to increase the size of the error bars:
X = [-504,-494,-490,-484,-478,-456,-436,-402,-364,-322] * 10^-6 ;
Y = [4.85,4.37,4.07,3.65,3.24,2.16,1.52,0.8,0.4,0.18];
errX = [0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01];
errX = 15 * errX; % so the error bars are visible
errorbar(X, Y, errX);
  7 commentaires
Avichay Ashur
Avichay Ashur le 23 Mai 2021
Modifié(e) : Avichay Ashur le 23 Mai 2021
@Scott MacKenzie Hey scott, the code u gave me earlier actualy worked and helped me alot, i figured how to export the curve fitting code and using the error bars as u said, 1 last thing i could realy use ur help is how i keep the points on the graph, right now there is only the curve that i exported, and the errorbars, could u explain me what i need to add in order to make the data points larger?
Scott MacKenzie
Scott MacKenzie le 23 Mai 2021
I not familiar with the curve fitting tool. There's likely a property named MarkerSize which you can change to control the size of the markers (i.e., data points). See if you can find it in the tool. Good luck.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Fit Postprocessing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by