Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: fminsearch and functions
Posted:
Jan 31, 2013 1:20 PM


On 1/31/2013 11:57 AM, Robert Ahrens wrote: > I should preface this post by saying that I have never used fminsearch > for multivariable functions and I am not a heavy user of anonymous > functions. > > I have defined a function myfun2: > > function f = myfun2(x,y,xc,yc) > > n = length(x) > f = sqrt((sum((sqrt((xc  x).^2 + (yc  y).^2)  ... > sum(sqrt((xc  x).^2 + (yc  y).^2)/n)).^2)/n)) > end > > where x and y are vectors (of length upwards of 10,000) and xc and yc > are scalars. Basically, I have known x and y vectors and I want to > find xc and yc such that f is minimized. I've read through the > fminsearch documentation and posts on the Matlab newgroups and I have > not been able to find any information as to how to do so. > > Any and all help is appreciated. > Bob There is an example showing you exactly how to do this case: http://www.mathworks.com/help/matlab/math/examplecurvefittingviaoptimization.html
The main idea is fminsearch wants a function of one variable, say x. I believe, from your description, that the variables you want to minimize are xc and yc. So in your function you should write something like
function f = myfun3(x,xdata,ydata)
xc = x(1) % the first scalar variable yc = x(2) % the second variable n = length(xdata); ...
Put the xdata and ydata vectors in your workspace, create an initial guess x0, then call
xolution = fminsearch(@(x)myfun3(x,xdata,ydata),x0)
For more information about passing parameters xdata and ydata this way, see http://www.mathworks.com/help/optim/ug/passingextraparameters.html#bskkr4z
Alan Weiss MATLAB mathematical toolbox documentation



