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:
Dicide between fsolve and lsqnonlin

Subject: Dicide between fsolve and lsqnonlin

From: Friedrich

Date: 23 Mar, 2007 07:25:03

Message: 1 of 5

Hi eyeryone,
I have a question concerning the iterative solution of
equation-systems:
I have a set of equations of the form:
F(1)=A1+A2*X(1)+A3*X(2)+A4*X(3);
F(2)=B1+B2*X(3);
F(3)=C1+C2*X(4);
F(4)=X(3)+D1*fun(X(1),X(2));
For this set of equations, I want to find the set of X that satisfy
the equation F=0.
As the 4th element of F is calculated by an external function (a MEX)
which brings a high grade of nonlinearity into the system, the
solution has to be found iterative. Therefore I wanted to use fsolve,
but had some problems with it (bad or no convergence, high dependence
from the initial guess). A possible alternative could be the function
lsqnonlin as there can be set upper and lower boundarys.
So my question is now, where is the difference between both and can I
use lsqnonlin for my problem?
How to treat the strong dependence of the startvalue?? Is there a
certain method to generate 'good' starting values??
I appreciate any answers!
Friedrich

Subject: Dicide between fsolve and lsqnonlin

From: John D'Errico

Date: 23 Mar, 2007 08:19:03

Message: 2 of 5

Friedrich wrote:
>
>
> Hi eyeryone,
> I have a question concerning the iterative solution of
> equation-systems:
> I have a set of equations of the form:
> F(1)=A1+A2*X(1)+A3*X(2)+A4*X(3);
> F(2)=B1+B2*X(3);
> F(3)=C1+C2*X(4);
> F(4)=X(3)+D1*fun(X(1),X(2));
> For this set of equations, I want to find the set of X that satisfy
> the equation F=0.
> As the 4th element of F is calculated by an external function (a
> MEX)
> which brings a high grade of nonlinearity into the system, the
> solution has to be found iterative. Therefore I wanted to use
> fsolve,
> but had some problems with it (bad or no convergence, high
> dependence
> from the initial guess). A possible alternative could be the
> function
> lsqnonlin as there can be set upper and lower boundarys.
> So my question is now, where is the difference between both and can
> I
> use lsqnonlin for my problem?
> How to treat the strong dependence of the startvalue?? Is there a
> certain method to generate 'good' starting values??
> I appreciate any answers!
> Friedrich

You might find my optimization tips
and tricks doc interesting, although
I don't really talk about explicit
differences between fsolve and
lsqnonlin. I do talk at length on
what to do about convergence problems,
poor starting values, etc.

 <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8553>

Finally, why use a mack truck here
to carry a pea to Boston? I.e., why
not simplify your problem? Think
about your problem before you throw
an optimizer at it?

If you know that

  0 = C1+C2*X(4)

and x(4) appears in no other place,
then what do you know about x3? Hint:
do you need a nonlinear rootfinder
when basic algebra will fully suffice?

Next, look at x(3).

  0 = B1+B2*X(3)

Can you find its value?

Finally, if you know x(3), this
reduces to a two variable problem.

  0 = A1+A2*X(1)+A3*X(2)+A4*X(3)
  0 = X(3)+D1*fun(X(1),X(2))

You can even do better. You can solve
for x(1) in terms of the other unkown
x(2) using the linear equation?

Then substitute into the nonlinear
equation. This leaves you a SINGLE
equation to solve. Use a solver like
fminbnd to solve this. Or use a tool
like my rmsearch, which is quite
good at solving 1-d problsm where
there may be multiple solutions.

 <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=13733&objectType=FILE>

HTH,
John D'Errico

Subject: Dicide between fsolve and lsqnonlin

From: Anderson

Date: 20 Oct, 2013 03:54:06

Message: 3 of 5

John,
Very informative response, but I would still like to see an answer to the original question: When should LSQNONLIN be used as opposed to FSOLVE? Anyone know?
Andy

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ef515b3.0@webcrossing.raydaftYaTP>...
> Friedrich wrote:
> >
> >
> > Hi eyeryone,
> > I have a question concerning the iterative solution of
> > equation-systems:
> > I have a set of equations of the form:
> > F(1)=A1+A2*X(1)+A3*X(2)+A4*X(3);
> > F(2)=B1+B2*X(3);
> > F(3)=C1+C2*X(4);
> > F(4)=X(3)+D1*fun(X(1),X(2));
> > For this set of equations, I want to find the set of X that satisfy
> > the equation F=0.
> > As the 4th element of F is calculated by an external function (a
> > MEX)
> > which brings a high grade of nonlinearity into the system, the
> > solution has to be found iterative. Therefore I wanted to use
> > fsolve,
> > but had some problems with it (bad or no convergence, high
> > dependence
> > from the initial guess). A possible alternative could be the
> > function
> > lsqnonlin as there can be set upper and lower boundarys.
> > So my question is now, where is the difference between both and can
> > I
> > use lsqnonlin for my problem?
> > How to treat the strong dependence of the startvalue?? Is there a
> > certain method to generate 'good' starting values??
> > I appreciate any answers!
> > Friedrich
>
> You might find my optimization tips
> and tricks doc interesting, although
> I don't really talk about explicit
> differences between fsolve and
> lsqnonlin. I do talk at length on
> what to do about convergence problems,
> poor starting values, etc.
>
> <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8553>
>
> Finally, why use a mack truck here
> to carry a pea to Boston? I.e., why
> not simplify your problem? Think
> about your problem before you throw
> an optimizer at it?
>
> If you know that
>
> 0 = C1+C2*X(4)
>
> and x(4) appears in no other place,
> then what do you know about x3? Hint:
> do you need a nonlinear rootfinder
> when basic algebra will fully suffice?
>
> Next, look at x(3).
>
> 0 = B1+B2*X(3)
>
> Can you find its value?
>
> Finally, if you know x(3), this
> reduces to a two variable problem.
>
> 0 = A1+A2*X(1)+A3*X(2)+A4*X(3)
> 0 = X(3)+D1*fun(X(1),X(2))
>
> You can even do better. You can solve
> for x(1) in terms of the other unkown
> x(2) using the linear equation?
>
> Then substitute into the nonlinear
> equation. This leaves you a SINGLE
> equation to solve. Use a solver like
> fminbnd to solve this. Or use a tool
> like my rmsearch, which is quite
> good at solving 1-d problsm where
> there may be multiple solutions.
>
> <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=13733&objectType=FILE>
>
> HTH,
> John D'Errico

Subject: Dicide between fsolve and lsqnonlin

From: Bruno Luong

Date: 20 Oct, 2013 14:22:07

Message: 4 of 5

"Anderson " <afhoke@gmail.removethis.com> wrote in message <l3vk4u$gbn$1@newscl01ah.mathworks.com>...
> John,
> Very informative response, but I would still like to see an answer to the original question: When should LSQNONLIN be used as opposed to FSOLVE? Anyone know?
> Andy

fsolve uses least-square to solve equation. It is just formulated conveniently shouldn't make any difference at the end.

Bruno

Subject: Dicide between fsolve and lsqnonlin

From: olgun adak

Date: 2 Jan, 2014 20:30:12

Message: 5 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <l40ouf$485$1@newscl01ah.mathworks.com>...
> "Anderson " <afhoke@gmail.removethis.com> wrote in message <l3vk4u$gbn$1@newscl01ah.mathworks.com>...
> > John,
> > Very informative response, but I would still like to see an answer to the original question: When should LSQNONLIN be used as opposed to FSOLVE? Anyone know?
> > Andy
>
> fsolve uses least-square to solve equation. It is just formulated conveniently shouldn't make any difference at the end.
>
> Bruno



If you want to set limits to the variables you are solving, you need to use LSQNONLIN. FSOLVE does not take XMIN and XMAX as argument.

O.A.

Tags for this Thread

No tags are associated with 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