From: JohnV on
I am having trouble using the optimization commands.
I defined the function m.file

function [z]=Meshat(x,r)
z=80*r^2*exp(-x^2-0.3*r^2);
end

then,

k=[4.23,3.56];
F=fminsearch(@meshat,k);


and received the following error messages,

??? Input argument "r" is undefined.

Error in ==> Meshat at 4
z=80*r^2*exp(-x^2-0.3*r^2);

Error in ==> fminsearch at 205
fv(:,1) = funfcn(x,varargin{:});

Error in ==> optimization at 3
F=fminsearch(@meshat,k);

Can someone correct my error?
From: Steven Lord on

"JohnV" <johnvalica(a)hotmail.com> wrote in message
news:1355149227.91194.1278609347154.JavaMail.root(a)gallium.mathforum.org...
>I am having trouble using the optimization commands.
> I defined the function m.file
>
> function [z]=Meshat(x,r)
> z=80*r^2*exp(-x^2-0.3*r^2);
> end
>
> then,
>
> k=[4.23,3.56];
> F=fminsearch(@meshat,k);

Are you expecting FMINSEARCH to call your function with two inputs, a scalar
for x (whose initial guess value is 4.23) and a scalar for r (whose initial
guess value is 3.56)?

If so, take a look at what the documentation page for FMINSEARCH and says
about how FMINSEARCH expects the function you pass in to behave.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fminsearch.html

The easiest way to fix this is to modify your function:

function [z]=Meshat(myparams)
x = myparams(1);
r = myparams(2);
z=80*r^2*exp(-x^2-0.3*r^2);
end

Then FMINSEARCH will call your function with a 2-element vector, your
function will break that vector into the two individual parameters, and it
will then evaluate your function using those two parameters.

*snip*

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com