[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: error: `fminsearch' undefined - Octave 3.2.4 Win7 64bit
From: |
Przemek Klosowski |
Subject: |
Re: error: `fminsearch' undefined - Octave 3.2.4 Win7 64bit |
Date: |
Mon, 14 Nov 2011 09:20:31 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20110930 Thunderbird/7.0.1 |
On 11/13/2011 06:15 AM, jammydav93 wrote:
Thank you that's great!
I now have another problem, i'm trying to minimise a function of
f(x) = a * cos(b*x + c) .* exp (x/d)
where my values of x and f(x) are known, how can I find the values of
a,b,c,d using fminsearch?
I tried the following however had no success as I don't know how to
format the function (fmp.m)
function y=fmp(x,a,b,c,d)
y=a*cos(b*x + c) .* exp (x/d)
The optim package version I have (Octave 3.4.2 and optim 1.0.16) does
not have fminsearch; it does have a similar function fminunc.
You have to be careful about what are the values returned from the
function you minimize. There is no uniquely-defined mathematical
ordering in two or more dimensions, so the minimized functions normally
have to be scalars. Your function fmp() is written with the .* operator,
implying that you expect them to return vectors or matrices, which is
incorrect---you have to specify the mathematical 'norm' for the ordering
required for minimization.
Another problem is there because your fmp() depends both on x and
parameters a,b,c, but from the point of view of optimization, only a,b,c
matter. The optimizer shouldn't even see the variable 'x'---you either
optimize the function for a single specific value of 'x' or, as is often
the case, optimize a sum of squares of your function's deviations from
data:
sum2=0; for x=x1:xN ; sum2 += (fmp(x)-data(x))^2; endfor
Note that the end result is a scalar value, as mentioned earlier.
In this case, you used x=1:100 but then tried to find values of a,b,c
that minimize f(x,a,b,c). In general, each 'x' could give different
values of a,b,c---do you want to minimize separately for each 'x' or
find an overall minimum for some statistical measure that involves all
values of 'x'? if so, what is that measure: least squares, as described
above, or something else?
If you want to minimize separately for each 'x', you could write it like
this:
function y=fmp(x,p); y=p(1)*cos(p(2)*x+p(3)) * exp(x/p(4)); endfunction
x=13;
f=@(p) fmp(x,p);
fminunc(f,[1,1,1,1])
this works because the anonymous function f() has only one
parameter---your parameter vector. It doesn't depend on 'x' because it
implicitly uses its current value.