I'm trying to fit some data using lsqcurvefit (Please Help!)

8 views (last 30 days)
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 Comments
Matt J
Matt J on 28 Feb 2013
Edited: Matt J on 28 Feb 2013
Incidentally, your current code doesn't guarantee that T will be included in the range to be integrated, e.g.,
>> T=51; 50:.3:T
ans =
50.0000 50.3000 50.6000 50.9000
Better to do something like this.
function rang = range(T)
rang = [50:0.01:T, T];
end

Sign in to comment.

Answers (2)

Matt J
Matt J on 28 Feb 2013
Edited: Matt J on 28 Feb 2013
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 Comments
Russell
Russell on 28 Feb 2013
T and J aren't constants, they are both arrays.
And no you can't solve the integral analytically. int(exp(-c/x))dx is unsolvable. Can't be solved by parts.
Matt J
Matt J on 28 Feb 2013
Edited: Matt J on 28 Feb 2013
And no you can't solve the integral analytically. int(exp(-c/x))dx is unsolvable.
yeah, forget that part (but only that part!!).

Sign in to comment.


Matt J
Matt J on 28 Feb 2013
Edited: Matt J on 28 Feb 2013
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.

Community Treasure Hunt

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

Start Hunting!