From: maria on
On 22 Óåðô, 18:00, "Steve" <steve.griksc...(a)mathworks.com> wrote:
> maria <mkou...(a)gmail.com> wrote in message <a5acb8f4-cae0-4889-8b7b-9f40459f2...(a)p9g2000vbl.googlegroups.com>...
> > I have the function
>
> > objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
>
> > I want use thelsqnonlin
>
> > 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
> > thelsqnonlinthe 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 thatlsqnonlincalls 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 forlsqnonlinor 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  
> - calllsqnonlinto solve for x
> - fix p at the result oflsqnonlin(call it xstar)
> - calllsqnonlinto solve for a new xstar
> - ...
>
> Does that sound right?
>
> - Steve- Áðüêñõøç êåéìÝíïõ óå ðáñÜèåóç -
>
> - ÅìöÜíéóç êåéìÝíïõ óå ðáñÜèåóç -

Dear Steve,

You have right.

Exactly this I want to do.

"-fix p
- calllsqnonlinto solve for x
- fix p at the result oflsqnonlin(call it xstar)
- calllsqnonlinto solve for a new xstar"

I suppose that using the anonymous function "@(x) calculx2(x,p)" I
realize it.

Thank you for your advice,
Maria


From: maria on
On Sep 22, 6:05 pm, Alan Weiss <awe...(a)mathworks.com> wrote:
> You are making an error in the syntax forlsqnonlin. 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, seehttp://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnon...
>
> 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, seehttp://www.mathworks.com/access/helpdesk/help/techdoc/math/f2-14970.h...
>
> Alan WeissMATLABmathematical toolbox documentation
>
>
>
> maria wrote:
> > I have the function
>
> > objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
>
> > I want use thelsqnonlin
>
> > 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
> > thelsqnonlinthe 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;- Hide quoted text -
>
> - Show quoted text -

After your advise I conclude in the following function


p=[293;293];

for k=1:4

[x fval history] = myproblem([p])

p=history(:,end)
end


where function myproblem([p]) is:

function [x fval history] = myproblem(x0)

p=x0;

history = [];
options = optimset('OutputFcn',
@myoutput,'LargeScale','on','MaxFunEvals',1000,'TolX',1.0E-6);
lb=293;
ub=2200;
[x,fval, exitflag, output,resnorm]=lsqnonlin
(@calculx2,x0,lb,ub,options);


function stop = myoutput(x,optimvalues,state);
stop = false;
if state == 'iter'
history = [history; x];
end
end



function objf = calculx2(x)
i=1:2;
F=p(i)-x(i);
objf=F;
end


end


I would like to ask. My initial function for optimization is the
square of the sum (objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2))
.. How I can incorporate it in the above objf?

Thank you very much for your time
From: Alan Weiss on
maria wrote:
> On Sep 22, 6:05 pm, Alan Weiss <awe...(a)mathworks.com> wrote:
>> You are making an error in the syntax forlsqnonlin. 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, seehttp://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnon...
>>
>> 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, seehttp://www.mathworks.com/access/helpdesk/help/techdoc/math/f2-14970.h...
>>
>> Alan WeissMATLABmathematical toolbox documentation
>>
>>
>>
>> maria wrote:
>>> I have the function
>>> objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
>>> I want use thelsqnonlin
>>> 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
>>> thelsqnonlinthe 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;- Hide quoted text -
>> - Show quoted text -
>
> After your advise I conclude in the following function
>
>
> p=[293;293];
>
> for k=1:4
>
> [x fval history] = myproblem([p])
>
> p=history(:,end)
> end
>
>
> where function myproblem([p]) is:
>
> function [x fval history] = myproblem(x0)
>
> p=x0;
>
> history = [];
> options = optimset('OutputFcn',
> @myoutput,'LargeScale','on','MaxFunEvals',1000,'TolX',1.0E-6);
> lb=293;
> ub=2200;
> [x,fval, exitflag, output,resnorm]=lsqnonlin
> (@calculx2,x0,lb,ub,options);
>
>
> function stop = myoutput(x,optimvalues,state);
> stop = false;
> if state == 'iter'
> history = [history; x];
> end
> end
>
>
>
> function objf = calculx2(x)
> i=1:2;
> F=p(i)-x(i);
> objf=F;
> end
>
>
> end
>
>
> I would like to ask. My initial function for optimization is the
> square of the sum (objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2))
> . How I can incorporate it in the above objf?
>
> Thank you very much for your time

I don't understand your code very well. Did you try running it in debug
mode to see if it is working properly? In particular, does the line
p=history(:,end)
give you want you expect? I don't understand why you want to keep an
entire history vector, why not just take
history = x; instead of history = [history; x];

In any case, to answer your question, if I understand your problem
correctly, the "initial function for optimization is sqrt((p(1)-x(1))^2
+(p(2)-x(2))^2))". So after you define p and x0, just call calculx2(x0).

Alan Weiss
MATLAB mathematical toolbox documentation
From: maria on
On 25 Óåðô, 16:26, Alan Weiss <awe...(a)mathworks.com> wrote:
> maria wrote:
> > On Sep 22, 6:05 pm, Alan Weiss <awe...(a)mathworks.com> wrote:
> >> You are making an error in the syntax forlsqnonlin. 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, seehttp://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnon...
>
> >> 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, seehttp://www.mathworks.com/access/helpdesk/help/techdoc/math/f2-14970.h...
>
> >> Alan WeissMATLABmathematical toolbox documentation
>
> >> maria wrote:
> >>> I have the function
> >>> objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
> >>> I want use thelsqnonlin
> >>> 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
> >>> thelsqnonlinthe 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;- Hide quoted text -
> >> - Show quoted text -
>
> > After your advise I conclude in the following function
>
> > p=[293;293];
>
> > for k=1:4
>
> >     [x fval history] = myproblem([p])
>
> >        p=history(:,end)
> > end
>
> > where function myproblem([p]) is:
>
> > function [x fval history] = myproblem(x0)
>
> >  p=x0;
>
> >     history = [];
> >     options = optimset('OutputFcn',
> > @myoutput,'LargeScale','on','MaxFunEvals',1000,'TolX',1.0E-6);
> >     lb=293;
> >     ub=2200;
> >     [x,fval, exitflag, output,resnorm]=lsqnonlin
> > (@calculx2,x0,lb,ub,options);
>
> >     function stop = myoutput(x,optimvalues,state);
> >         stop = false;
> >         if state == 'iter'
> >           history = [history; x];
> >         end
> >     end
>
> >     function objf = calculx2(x)
> >       i=1:2;
> >     F=p(i)-x(i);
> >     objf=F;
> >     end
>
> > end
>
> > I would like to ask. My initial function for optimization is the
> > square of the sum (objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2))
> > .  How I can incorporate it in the above objf?
>
> > Thank you very much for your time
>
> I don't understand your code very well. Did you try running it in debug
> mode to see if it is working properly? In particular, does the line
> p=history(:,end)
> give you want you expect? I don't understand why you want to keep an
> entire history vector, why not just take
> history = x;  instead of history = [history; x];
>
> In any case, to answer your question, if I understand your problem
> correctly, the "initial function for optimization is sqrt((p(1)-x(1))^2
> +(p(2)-x(2))^2))". So after you define p and x0, just call calculx2(x0).
>
> Alan Weiss
> MATLAB mathematical toolbox documentation- Áðüêñõøç êåéìÝíïõ óå ðáñÜèåóç -
>
> - ÅìöÜíéóç êåéìÝíïõ óå ðáñÜèåóç -

Hello Alan,

I hope that my code is better now.

p=[293;293];

for k=1:4

[x fval p] = myproblem([p])

end



------------------------------------



function [x fval p] = myproblem(x0)

p=x0;
options = optimset('OutputFcn',
@myoutput,'LargeScale','on','MaxFunEvals',1000,'Display','iter');
% 'TolX',1.0E-6,
lb=293;
ub=2200;
[x,fval, exitflag, output,resnorm]=lsqnonlin
(@calculx2,x0,lb,ub,options);


function stop = myoutput(x,optimvalues,state)

if state == 'iter'
p=x;
end
stop = false;
end



function objf = calculx2(x)
i=1:2;
F=p(i)-x(i);
objf=F;
end


end



I would like to put as F my function: F=sqrt((p(1)-x(1))^2 +(p(2)-x
(2))^2))" but when I use it I get the message:
"F =
0
Warning: Large-scale method requires at least as many equations as
variables;
using line-search method instead. Upper and lower bounds will be
ignored.
> In optim\private\lsqncommon at 160
In lsqnonlin at 181
In myproblem at 10
In test_myproblem at 6
F =
4.3660e-006
F =
4.3660e-006

Directional
Iteration Func-count Residual Step-size derivative
Lambda
0 3 0
F =
NaN
??? Error using ==> eq
Matrix dimensions must agree.

Error in ==> myproblem>myoutput at 15
if state == 'iter'

Error in ==> callAllOptimOutputFcns at 12
stop(i) = feval(OutputFcn{i},xOutputfcn,optimValues,state,varargin
{:});

Error in ==> optim\private\nlsq at 400
stop = callAllOptimOutputFcns
(outputfcn,xOutputfcn,optimValues,'interrupt',varargin{:});

Error in ==> optim\private\lsqncommon at 224
[x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...

Error in ==> lsqnonlin at 181
[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

Error in ==> myproblem at 10
[x,fval, exitflag, output,resnorm]=lsqnonlin
(@calculx2,x0,lb,ub,options);

Error in ==> test_myproblem at 6
[x fval history p] = myproblem([p])"

What I could consider?

Thank you,
Maria
From: Alan Weiss on
maria wrote:
> On 25 ����, 16:26, Alan Weiss <awe...(a)mathworks.com> wrote:
>> maria wrote:
>>> On Sep 22, 6:05 pm, Alan Weiss <awe...(a)mathworks.com> wrote:
>>>> You are making an error in the syntax forlsqnonlin. 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, seehttp://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnon...
>>>> 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, seehttp://www.mathworks.com/access/helpdesk/help/techdoc/math/f2-14970.h...
>>>> Alan WeissMATLABmathematical toolbox documentation
>>>> maria wrote:
>>>>> I have the function
>>>>> objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2);
>>>>> I want use thelsqnonlin
>>>>> 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
>>>>> thelsqnonlinthe 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;- Hide quoted text -
>>>> - Show quoted text -
>>> After your advise I conclude in the following function
>>> p=[293;293];
>>> for k=1:4
>>> [x fval history] = myproblem([p])
>>> p=history(:,end)
>>> end
>>> where function myproblem([p]) is:
>>> function [x fval history] = myproblem(x0)
>>> p=x0;
>>> history = [];
>>> options = optimset('OutputFcn',
>>> @myoutput,'LargeScale','on','MaxFunEvals',1000,'TolX',1.0E-6);
>>> lb=293;
>>> ub=2200;
>>> [x,fval, exitflag, output,resnorm]=lsqnonlin
>>> (@calculx2,x0,lb,ub,options);
>>> function stop = myoutput(x,optimvalues,state);
>>> stop = false;
>>> if state == 'iter'
>>> history = [history; x];
>>> end
>>> end
>>> function objf = calculx2(x)
>>> i=1:2;
>>> F=p(i)-x(i);
>>> objf=F;
>>> end
>>> end
>>> I would like to ask. My initial function for optimization is the
>>> square of the sum (objf= sqrt((p(1)-x(1))^2 +(p(2)-x(2))^2))
>>> . How I can incorporate it in the above objf?
>>> Thank you very much for your time
>> I don't understand your code very well. Did you try running it in debug
>> mode to see if it is working properly? In particular, does the line
>> p=history(:,end)
>> give you want you expect? I don't understand why you want to keep an
>> entire history vector, why not just take
>> history = x; instead of history = [history; x];
>>
>> In any case, to answer your question, if I understand your problem
>> correctly, the "initial function for optimization is sqrt((p(1)-x(1))^2
>> +(p(2)-x(2))^2))". So after you define p and x0, just call calculx2(x0).
>>
>> Alan Weiss
>> MATLAB mathematical toolbox documentation- �������� �������� �� �������� -
>>
>> - �������� �������� �� �������� -
>
> Hello Alan,
>
> I hope that my code is better now.
>
> p=[293;293];
>
> for k=1:4
>
> [x fval p] = myproblem([p])
>
> end
>
>
>
> ------------------------------------
>
>
>
> function [x fval p] = myproblem(x0)
>
> p=x0;
> options = optimset('OutputFcn',
> @myoutput,'LargeScale','on','MaxFunEvals',1000,'Display','iter');
> % 'TolX',1.0E-6,
> lb=293;
> ub=2200;
> [x,fval, exitflag, output,resnorm]=lsqnonlin
> (@calculx2,x0,lb,ub,options);
>
>
> function stop = myoutput(x,optimvalues,state)
>
> if state == 'iter'
> p=x;
> end
> stop = false;
> end
>
>
>
> function objf = calculx2(x)
> i=1:2;
> F=p(i)-x(i);
> objf=F;
> end
>
>
> end
>
>
>
> I would like to put as F my function: F=sqrt((p(1)-x(1))^2 +(p(2)-x
> (2))^2))" but when I use it I get the message:
> "F =
> 0
> Warning: Large-scale method requires at least as many equations as
> variables;
> using line-search method instead. Upper and lower bounds will be
> ignored.
>> In optim\private\lsqncommon at 160
> In lsqnonlin at 181
> In myproblem at 10
> In test_myproblem at 6
> F =
> 4.3660e-006
> F =
> 4.3660e-006
>
> Directional
> Iteration Func-count Residual Step-size derivative
> Lambda
> 0 3 0
> F =
> NaN
> ??? Error using ==> eq
> Matrix dimensions must agree.
>
> Error in ==> myproblem>myoutput at 15
> if state == 'iter'
>
> Error in ==> callAllOptimOutputFcns at 12
> stop(i) = feval(OutputFcn{i},xOutputfcn,optimValues,state,varargin
> {:});
>
> Error in ==> optim\private\nlsq at 400
> stop = callAllOptimOutputFcns
> (outputfcn,xOutputfcn,optimValues,'interrupt',varargin{:});
>
> Error in ==> optim\private\lsqncommon at 224
> [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...
>
> Error in ==> lsqnonlin at 181
> [x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
>
> Error in ==> myproblem at 10
> [x,fval, exitflag, output,resnorm]=lsqnonlin
> (@calculx2,x0,lb,ub,options);
>
> Error in ==> test_myproblem at 6
> [x fval history p] = myproblem([p])"
>
> What I could consider?
>
> Thank you,
> Maria

Maria, the longer I look at your problem the less I understand it. Let
me begin with some simple observations.

Are you trying to bound both components of x below by 293 and above by
2200? If so, you need to set
lb = 293*ones(2,1);
ub = 2200*ones(2,1);

In this code:
> function objf = calculx2(x)
> i=1:2;
> F=p(i)-x(i);
> objf=F;
> end
why didn't you simply write
function objf = calculx2(x)
objf = p - x;

You should not call optimset within your function. optimset takes a long
time to run. Call optimset once and reuse the resulting options structure.

> for k=1:4
>
> [x fval p] = myproblem([p])
>
> end
This code computes the same thing four times, since p does not depend on
k. Why do it?

Now for the real problem. You set p = [293;293] to start.
You set x0 = p.
Therefore your problem is minimized at the start; x = p, satisfies the
bounds, your objective function is zero, and there is nothing to do.

In summary, I do not understand what you are trying to do. I wrote this
note to give you some programming pointers, and to make as clear as I
can why I do not understand what you are trying.

Alan Weiss
MATLAB mathematical toolbox documentation