From: maria on
I have the function

objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);

I want use the lsqnonlin

I can use the objf as it is or I have to modify it? I want the new
calculated value of x pass to p, that is p=x and each time that I use
the lsqnonlin the x0=p. I have written the following code. Is it
right?



x0=p;

lb=293;

ub=2200;

options = optimset('LargeScale','off','MaxFunEvals',1000,'TolX',
1.0E-6);

[x, fval, exitflag, output]=lsqnonlin (@(x) calculx2
(x,p),x0,lb,lu,options);





function objf= calculx2(x,p)

objf=sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);


p=x;



From: Steve on
maria <mkour80(a)gmail.com> wrote in message <a5acb8f4-cae0-4889-8b7b-9f40459f2d9c(a)p9g2000vbl.googlegroups.com>...
> I have the function
>
> objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
>
> I want use the lsqnonlin
>
> I can use the objf as it is or I have to modify it? I want the new
> calculated value of x pass to p, that is p=x and each time that I use
> the lsqnonlin the x0=p. I have written the following code. Is it
> right?
>
>
>
> x0=p;
>
> lb=293;
>
> ub=2200;
>
> options = optimset('LargeScale','off','MaxFunEvals',1000,'TolX',
> 1.0E-6);
>
> [x, fval, exitflag, output]=lsqnonlin (@(x) calculx2
> (x,p),x0,lb,lu,options);
>
>
>
>
>
> function objf= calculx2(x,p)
>
> objf=sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
>
>
> p=x;
>
>
There are a few problems here...
First, I don't think that this code does what you want it to. When you define the anonymous function "@(x) calculx2(x,p)", the value of p is taken from the workspace and then frozen for the lifetime of this anonymous function. That is, p = x0 each time that lsqnonlin calls calculx2.

Another issue which is negated by the first problem above ^ is that the assignment at the end of calculx2 "p=x;" is lost as soon as calculx2 is completed. So, there are 2 problems that prevent the value of p from being updated as you go.

The thrid problem, and perhaps the biggest, is that you seem to want to change the value of the parameter p every time objf is calculated. This is a problem for lsqnonlin or any least-squares/fitting/optimization routine because objf will be calculated many times to get one estimate of x. Changing p every time objf is calculated means the problem changes every time. That would be difficult to impossible for a solver.

Maybe, what you want to do is the following:
- fix p
- call lsqnonlin to solve for x
- fix p at the result of lsqnonlin (call it xstar)
- call lsqnonlin to solve for a new xstar
- ...

Does that sound right?

- Steve
From: Alan Weiss on
You are making an error in the syntax for lsqnonlin. Do not square the
terms and sum them in your objective function. Return the vector
p - x
where p and x are the vectors in question. For an example, see
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnonlin.html#f662559


You would probably do best to use a nested function, so that the
function can access the previous value of x. For an example of using a
nested function to access the previous values of x, see
http://www.mathworks.com/access/helpdesk/help/techdoc/math/f2-14970.html#f2-935539

Alan Weiss
MATLAB mathematical toolbox documentation

maria wrote:
> I have the function
>
> objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
>
> I want use the lsqnonlin
>
> I can use the objf as it is or I have to modify it? I want the new
> calculated value of x pass to p, that is p=x and each time that I use
> the lsqnonlin the x0=p. I have written the following code. Is it
> right?
>
>
>
> x0=p;
>
> lb=293;
>
> ub=2200;
>
> options = optimset('LargeScale','off','MaxFunEvals',1000,'TolX',
> 1.0E-6);
>
> [x, fval, exitflag, output]=lsqnonlin (@(x) calculx2
> (x,p),x0,lb,lu,options);
>
>
>
>
>
> function objf= calculx2(x,p)
>
> objf=sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
>
>
> p=x;
>
>
>