Why am I getting infinite value from my user defined function?

조회 수: 4 (최근 30일)
Ayesha Zaman
Ayesha Zaman 2016년 8월 16일
댓글: John D'Errico 2016년 8월 18일
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

채택된 답변

Walter Roberson
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.

추가 답변 (1개)

John D'Errico
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
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
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 CenterFile 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!

Translated by