I'm trying to fit some data using lsqcurvefit (Please Help!)
6 次查看(过去 30 天)
显示 更早的评论
Please Help!!! I know I'm making a really dumb mistake somewhere. I never have problems fitting data with lsqcurvefit except for this function.
I'm trying to fit the function: Jcur = x(3).*exp(-x(2)/T).*exp(-(1/x(1)).*integral(exp(-x(2)/T'),dT', from some constant to T)
so my limits of integration depend on the variable T
I use lsqcurvefit(@tsdcfit,x0,T,J,[],[],options)
and have a separate function file tsdcfit with my fitting variables and T:
function Jcur = tsdcfit(x,T); global k e r;
function fux = fun(x,T) range = 50:0.01:T; ex = exp(-x(2)/(range)); fux = trapz(range,ex); end
Jcur = x(3).*exp(-x(2)/T).*exp(-(1/x(1)).*fun(x,T)); end
Any help would be greatly appreciated! I know there's something wrong with how i call the function fun(x,T), I've played around with it a lot and it's basically only spitting out one value regardless of the range of T
4 个评论
Walter Roberson
2013-2-28
回答(2 个)
Matt J
2013-2-28
编辑:Matt J
2013-2-28
lsqcurvefit(@tsdcfit,x0,T,J,[],[],options)
does not make much sense if T and presumably also J are scalar constants. It means you are curve fitting based on 1 data point.
Also, you are not passing T to tsdcfit and should be getting error messages. I question whether we are seeing your actual code.
Also, the sensitivity of fun(x,T) would depend on x. If x(2) is set to zero, for example, it would make sense that fun(x,T) is independent of T.
Finally, the integral of exp(-x(2)/T) has a closed form solution as a function of T and doesn't need to be numerically approximated using TRAPZ. Note that after making the change of variables y=-x(2)/T, the integral becomes
integral(y^2 * exp(y),dy)
which can be computed symbolically by integration by parts. It would be better to use the closed form formula.
2 个评论
Matt J
2013-2-28
编辑:Matt J
2013-2-28
If I put in an array of T values, say T=201:1:210, and then ask for xint(T),I get one value back, but I'm looking for 10 values...
You have to for-loop over T(i). range(T) will always return a vector to be integrated, regardless of whether or not T is a scalar, and the integral over that range will of course be a scalar as well.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Expansion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!