[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: fminsearch
From: |
siko1056 |
Subject: |
Re: fminsearch |
Date: |
Tue, 20 Dec 2016 17:02:42 -0800 (PST) |
Tweetman wrote
> Hi,
>
> I have measured data (attached) to which I want to fit the sum of two
> exp-functions. Here is my code so far:
>
> A = dlmread('output.txt');
> alpha60 = A(:,1);
> cv60 = A(:,2);
> function f = exponential(coeff,alpha60,cv60)
> a = coeff(1);
> b = coeff(2);
> c = coeff(3);
> d = coeff(4);
> g = coeff(5);
> h = coeff(6);
> i = coeff(7);
> Y_fun60 = a + b.*e.^(-alpha60./c).^d + g.*e.^(-alpha60./h).^i;
> f = sum ((Y_fun60 - cv60).^2);
> end
> options = optimset('TolFun',1e1, 'TolX', 1e1);
> [cc fval] = fminsearch(@(c) exponential(c,alpha60,cv60),[10 625 30 -270
> -65 60 -13],options)
> fitted60 = cc(1) + cc(2).*e.^(-alpha60./cc(3)).^cc(4) +
> cc(5).*e.^(-alpha60./cc(6)).^cc(7);
> plot(alpha60,cv60, alpha60,fitted60)
>
> When I plot this, I find that the fit does not match the data nicely and
> I am asking myself why. Is it the options that I picked? I played around
> with a few different starting values and options such as MaxIter, but
> could not see major improvements. Is it the code, the function to be
> minimized?
>
> I am running Octave 3.8.1.
>
> BTW: I also ran into an example how to use nlinfit so I thought maybe I
> could use that instead. Octave tells me it is part of statistics
> package, but I finally found nlinfit in the function reference of optim
> package. Is that some kind of version mismatch? How can I install an
> older version of statistics (e. g. from forge), which one would I need?
>
> Thanks for your help,
> JP
Hello JP,
I made some modifications with your code to get a better understanding:
A = dlmread('output.txt');
alpha60 = A(:,1);
cv60 = A(:,2);
scatter (alpha60, cv60)
hold on;
myfun = @(a,b,c,d,g,h,i) a + b.*e.^(-alpha60./c).^d +
g.*e.^(-alpha60./h).^i;
exponential = @(coeff,cv60) ...
sumsq (myfun(coeff(1), coeff(2), coeff(3), coeff(4), coeff(5), coeff(6),
coeff(7)) - cv60);
options = optimset('Display', 'iter');%, 'TolFun', 1e1, 'TolX', 1e1);
cc0 = [10, 625, 30, -270, -65, 60, -130];
[cc, fval] = fminsearch(@(c) exponential(c,cv60), cc0, options)
%[cc, fval, info] = fminunc(@(c) exponential(c,cv60), cc0, options)
fitted60 = myfun(cc(1), cc(2), cc(3), cc(4), cc(5), cc(6), cc(7));
plot (alpha60, cv60, alpha60, fitted60)
Essentially it should behave the same way as yours did. When you add
"'Display', 'iter'" to optimset, you can see if your solver actually does
anything. In your original case the solver more or less just returned you
initial coefficient guess after one step. I got the solver to "work more" by
removing your restrictions on 'TolFun' and 'TolX' using the default values.
Like seen above, I also tried fminunc as alternative to the derivate free
fminsearch. The results are "better" but for both solvers not overwhelming.
What good or bad means depends strongly on your application and some
understanding of your model/data/measurement. To me your data seems scaled
badly (ranging von 10^0 to 10^7).
HTH, Kai
--
View this message in context:
http://octave.1599824.n4.nabble.com/fminsearch-tp4681098p4681116.html
Sent from the Octave - General mailing list archive at Nabble.com.
- fminsearch, Tweety, 2016/12/18
- Re: fminsearch,
siko1056 <=