Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fminsearch and functions

Subject: fminsearch and functions

From: Robert Ahrens

Date: 31 Jan, 2013 16:57:08

Message: 1 of 4

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

Subject: fminsearch and functions

From: Alan_Weiss

Date: 31 Jan, 2013 18:20:41

Message: 2 of 4

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/example-curve-fitting-via-optimization.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/passing-extra-parameters.html#bskkr4z

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: fminsearch and functions

From: Marc

Date: 1 Feb, 2013 07:06:05

Message: 3 of 4

Alan_Weiss <aweiss@mathworks.com> wrote in message <keeclp$945$1@newscl01ah.mathworks.com>...
> 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/example-curve-fitting-via-optimization.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/passing-extra-parameters.html#bskkr4z
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

If you really have upwards of 10,000 data points, you may want to tic/toc your function for fminsearch to minimize....

If you are in the 0.1 sec range, do not freak out if fminsearch takes some time.... Like hours...

Subject: fminsearch and functions

From: Johan Lofberg

Date: 1 Feb, 2013 10:24:08

Message: 4 of 4

Are you sure about the formulation you've posted here. It looks like you are trying to solve a very simple geometrical problem, but the placement of the sqrt and sums makes it a bit odd.

What is it you want to do? You have 10000 points in 2D, and you want to find a point (xc,yc) such that what is minimized? Minimize total Euclidean distance - average distance?


"Robert Ahrens" wrote in message <kee7p4$iju$1@newscl01ah.mathworks.com>...
> 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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us