MATLAB: Fitting gaussian exponential to logscale

curve fittingexponentialMATLAB

Hello!
I have been trying to fit a log scale plot to an exponential function given below:
fun=@(t,x)2*(t(1))^2.*(1-exp(-((x./t(2)).^(2*t(3)))));
But I am unable to get a fit that is even close to the data plot. this is what I tried to do:
function fun = myfun(t,x,y)
t(1) = t(1)*1e-9;
t(2) = t(2)*1e-9;
t(3) = t(3)*1e-9;
xx=log(x)
yy=log(y)
fun=@(t,x)2*(t(1))^2.*(1-exp(-((x./t(2)).^(2*t(3)))));;
t=lsqcurvefit(fun,t,x,y)
plot(xx,yy,'ko',xx,fun(t,xx),'b-')
end
and then caling the function as:
t=[7.25553e-10 2.2790e-9 2.27908e-9]
myfun(t,x,y)
But this is what I get instead:
The x and y data is attached as .txt
I tried playing with different inital values but none of them gives me close to a good fit. I dont understand what is wrong here.
Could someone please help me out ?
I also do not have access to the curve fitting tool or any other tool in matlab just so you know. Any help would be really appreciated!
Thank you!

Best Answer

The logs of the y values are negative, whereas your function will return positive values only! You are probably better off trying the following
% Scale data
xx = 10^7*x;
yy = 10^18*y;
fun=@(t,x) 2*(t(1))^2.*(1-exp(-((x./t(2)).^(2*t(3)))));
t0 = [1 1 0.5];
t = fminsearch(@(t) fcn(t,xx,yy,fun),t0);
disp(t)
Y = fun(t,xx);
% Rescale curve fit
yfit = Y*10^-18;
plot(x,y,'o',x,yfit),grid
xlabel('x [m]'),ylabel('y [m^2]')
legend('data','curve fit')
function F = fcn(t,xx,yy,fun)
Y = fun(t,xx);
F = norm(Y-yy);
end
which results in the following fit