Why am I getting infinite value from my user defined function?
4 ビュー (過去 30 日間)
古いコメントを表示
Hello
I am working on curve fit . My codes are like....
X=...
Y=...
X1=(X./(42*(10^-9)));
Y1=(Y./(pi*((5*10^-6)*(5*10^-6))));
Q=real(sqrt(X1));
plot(X1,Y1,'ro')
hold on
A=1.5*(10^(-7)); B=-356;
C=396*(10^(-3));
coeff=fminsearch('exp_fit',[A B C]);
A=coeff(1)
B=coeff(2)
C=coeff(3)
y_se=A*(exp(-B+C.*Q));
plot(x3,y_se,'b--')
I have my defined func as...
function E=exp_fit(x0)
A=x0(1); B=x0(2); C=x0(3);
X=..
Y=...
X1=real(X./(42*(10^(-9))));
Y1=real(Y./(pi*((5*10^(-6))*(5*10^(-6)))));
E=sum(A.*(exp(-B+C.*sqrt(X1))-Y1).^2);
end
0 件のコメント
採用された回答
Walter Roberson
2016 年 8 月 17 日
fminsearch does not implement constraints. Your C can get large relative to B, leading to exp() of a large number, which is infinity. A sum that includes infinity (but no NaN and no negative infinity) is infinity.
0 件のコメント
その他の回答 (1 件)
John D'Errico
2016 年 8 月 17 日
We don't see your data. So there is no way of knowing what the iterations will do. We also don't see your starting values, or what any of the numbers are like.
But ANYTIME exponentials are involved, you are likely to see overflows if you are not careful. That causes inf.
Given that you don't even know why you are seeing inf results, that guarantees that you don't know how to be careful here in the curve fit.
Use the debugger to investigate when inf occurs. Or provide more information, like the data itself, so we have a chance to be more helpful.
3 件のコメント
John D'Errico
2016 年 8 月 18 日
I think you do not understand how to estimate a model, or how to construct one.
Now that you have posted sufficient data to see it, look at the expression you are trying to minimize.
E=sum(A.*(exp(-B+C.*sqrt(X1))-Y1).^2);
What is the purpose of the variable A there? The minimum of this expression occurs at A = -inf, irrespective of the value of B and C.
Anyway, I have no idea why you feel the need to use real on all sort of expressions, where the result is always a purely real number already, and will never be complex. Lets look more closely at the expression E.
E=sum(A.*(exp(-B+C.*sqrt(X1))-Y1).^2);
In the middle there, we have
exp(-B+C.*sqrt(X1))
So it appears that you want to use a two term exponential model.
Given your starting values,
U = -B + C*sqrt(X1);
>> min(U)
ans =
473.33
>> max(U)
ans =
492.5
But then you use exp.
exp(min(U))
ans =
3.6676e+205
How does that compare to Y1?
>> min(Y1)
ans =
3349.3
>> max(Y1)
ans =
6841.4
Anyway, do you seriously expect a curve that looks like this:
plot(sqrt(X1),Y1,'ro')

Your data does NOT look like an exponential function of sqrt(X1). It NEVER will be fit by a curve of the form you seem to be trying to fit. NEVER.
Again, the parameter A is completely irrelevant.
So you need to decide what the model is that you WANT to fit. There are no magic set of parameters for the model you seem to be using that will fit your data.
If you forve me to choose a model that makes SOME sense for a curve of the typ you show, I might pick:
y = a + b*exp(-c*sqrt(X1))
With a carefully chosen set of choices for a,b,c, this model MIGHT fit your data.
John D'Errico
2016 年 8 月 18 日
If you want a reasonable choice of parameters for that model, you MIGHT start with this:
6670.5 + -1.3959e+12*exp(0.067286*sqrt(X1))
So:
plot(X1,Y1,'ro',X1,6670.5 + -1.3959e+12*exp(-0.067286*sqrt(X1)),'b-')

参考
カテゴリ
Help Center および File Exchange で Get Started with Curve Fitting Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!