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:
(fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Alaa Elwany

Date: 9 Nov, 2011 20:29:11

Message: 1 of 16

I am trying to solve 2 non-linear equations in two variables x1, and x2.

For simplicity, the 2 equations take the forms:

x1 = A / (B^x2 + C^x2)
x2 = D / (x1*E - F)

Where A,B,C,D,E, and F are known constants. (Well, they are parameters, but I compute their values a priori).

That might sound easy. I simply defined my functions:

function F = Myfun(x)

F(1) = x1*(B^x2 - C^x2)- A;
F(2) = x2*(x1*E - F) - D;

Then used fsolve:

x0 = [0.5,1]; %For example
x = fsolve(@Myfun,x0,options)

MY PROBLEM:

I get the correct answer 90% of the time.
HOWEVER, unfortunately for some instances of the problem, depending on the values of A,....,F, I know that the scales of x1 differ considerably.
--> x1 is in the order of 1e-15, whereas x2 is in the order of 10.

This poor scaling results in the famous:

fsolve stopped because the relative size of the current step is less than the
selected value of the step size tolerance squared, but the vector of function values
is not near zero as measured by the selected value of the function tolerance.


I tried setting TolX and TolFun to minimal values. No use. I am quite sure this will not help anyway because the problem is poorly scaled for specific values of the constants A,...,F. I need to properly scale it rather than setting Tolerances to minimal values!

I did not find an "Autoscale" option in optimset, which sounded strange because even Excel Solver has an "Autoscaling" option!!

Desperate for help. Would very highly appreciate anyone's assistance. I need either to know how to scale such a problem, or how to have Matlab/fsolve/Optimset automatically scale it.

Thank you very much.

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Roger Stafford

Date: 9 Nov, 2011 21:47:28

Message: 2 of 16

"Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <j9enqn$r4o$1@newscl01ah.mathworks.com>...
> function F = Myfun(x)
]> F(1) = x1*(B^x2 - C^x2)- A;
> F(2) = x2*(x1*E - F) - D;
> ........
> x0 = [0.5,1]; %For example
> x = fsolve(@Myfun,x0,options)
> .......
> --> x1 is in the order of 1e-15, whereas x2 is in the order of 10.
> This poor scaling results in the famous:
> fsolve stopped because the relative size of the current step is .......
- - - - - - - - -
  I would suggest that rescaling will not solve your problem. Remember that you are dealing with floating point numbers here, and it is inherent in a floating point number that it adjusts its precision in accordance with its size so that it maintains an accuracy of roughly one part in 10^16 parts no matter what size it is. (Of course there are operations that can greatly reduce such accuracy, such as subtracting two very close numbers, but that difficulty is not cured by rescaling.)

  Very likely your difficulty with 'fsolve' lies in making inappropriate initial estimates in the 'x0' input argument. 'fsolve' is no mental giant and may very well need some help in getting off to a healthy start. It is extremely unlikely that a single estimate will suffice for all possible parameters, A through F, in your problem. You may have to work out some kind of rough formula for adjusting 'x0' to best match these parameters, and that may require quite a bit of experimentation on your part.

Roger Stafford

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Alaa Elwany

Date: 9 Nov, 2011 22:10:31

Message: 3 of 16

Thank you very much for your response Roger. However, being familiar with this "Starting Solution" dilemma, I did have a validation problem instance, for which I knew in advance that the solution is x1 = 1.2e-23, x2=8.2 I set the starting solution to:
(1e-23,8.0) --> fsolve stalled, gave no solution and the same error message.

It's driving me CRAZY!! Apparently, it is not a problem regarding "a good initial guess", but something else, and this is why I thought of a scaling issue, specially that the error message has to do with "step size".

Does this additional (and weird) observation give you a clue to any possible explanation? perhaps there is an alternative solution that I didn't think of?

Thank you very much for your concern again, regardless of whether the problem is resolved or not.

Alaa

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Greg Heath

Date: 9 Nov, 2011 23:29:51

Message: 4 of 16

On Nov 9, 5:10 pm, "Alaa Elwany" <alaa_elw...@yahoo.com> wrote:
> Thank you very much for your response Roger.  However, being familiar with this "Starting Solution" dilemma, I did have a validation problem instance, for which I knew in advance that the solution is x1 = 1.2e-23, x2=8.2   I set the starting solution to:
> (1e-23,8.0) --> fsolve stalled, gave no solution and the same error message.
>
> It's driving me CRAZY!! Apparently, it is not a problem regarding "a good initial guess", but something else, and this is why I thought of a scaling issue, specially that the error message has to do with "step size".
>
> Does this additional (and weird) observation give you a clue to any possible explanation? perhaps there is an alternative solution that I didn't think of?
>
> Thank you very much for your concern again, regardless of whether the problem is resolved or not.
>
> Alaa

What happens when you make the replacements x1 = 1e-23*x3 and x2 =
10*x4?

Hope this helps.

Greg

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Mark Shore

Date: 10 Nov, 2011 00:12:28

Message: 5 of 16

"Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <j9enqn$r4o$1@newscl01ah.mathworks.com>...
> I am trying to solve 2 non-linear equations in two variables x1, and x2.
>
> For simplicity, the 2 equations take the forms:
>
> x1 = A / (B^x2 + C^x2)
> x2 = D / (x1*E - F)
>

Without taking anything away from Rogers's comment, why two constants B and C when they could be replaced by a single constant (= B + C)?

Also, since X1 is a function of X2 and vice-versa, a recursive solution method might be better suited to your problem.

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Roger Stafford

Date: 10 Nov, 2011 00:38:10

Message: 6 of 16

"Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <j9eton$ia1$1@newscl01ah.mathworks.com>...
> Does this additional (and weird) observation give you a clue to any possible explanation? perhaps there is an alternative solution that I didn't think of?
- - - - - - - - - - -
  In a set of two equations in two variables, when one of these variables can easily be expressed in terms of the other, as in your case, I would think it would improve matters to express the problem in terms of one equation and one variable. Your problem is equivalent to solving

 F(x2) = (x2+(D/F))*(B^x2+C^x2)-(A*E/F)*x2 = 0

which is one equation, one variable, and, in effect, four parameters. The value of x1 can later be determined from

 x1 = (F/E) + (D/E)/x2

You might find this less troublesome to solve and to find suitable initial estimates.

  Note: Your two expressions are inconsistent:

 x1 = A / (B^x2 + C^x2)
 F(1) = x1*(B^x2 - C^x2)- A;

Roger Stafford

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Roger Stafford

Date: 10 Nov, 2011 00:50:30

Message: 7 of 16

"Mark Shore" wrote in message <j9f4tc$ab2$1@newscl01ah.mathworks.com>...
> "Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <j9enqn$r4o$1@newscl01ah.mathworks.com>...
> > x1 = A / (B^x2 + C^x2)
> Without taking anything away from Rogers's comment, why two constants B and C when they could be replaced by a single constant (= B + C)?
- - - - - - - - -
  Mark, B^x2+C^x2 and (B+C)^x2 are usually not equal.

Roger Stafford

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Mark Shore

Date: 10 Nov, 2011 03:43:10

Message: 8 of 16

"Roger Stafford" wrote in message <j9f74m$gkv$1@newscl01ah.mathworks.com>...
> "Mark Shore" wrote in message <j9f4tc$ab2$1@newscl01ah.mathworks.com>...
> > "Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <j9enqn$r4o$1@newscl01ah.mathworks.com>...
> > > x1 = A / (B^x2 + C^x2)
> > Without taking anything away from Rogers's comment, why two constants B and C when they could be replaced by a single constant (= B + C)?
> - - - - - - - - -
> Mark, B^x2+C^x2 and (B+C)^x2 are usually not equal.
>
> Roger Stafford

Aggh... of course not. I originally read this on a small iPhone screen and it looked like B*X2 + C*X2. (I _was_ wondering about it, but not enough it seems.)

Embarrassingly there is no way to correct my original comment.

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Alaa Elwany

Date: 10 Nov, 2011 14:17:30

Message: 9 of 16

Again, thank you very much for your concern Roger.
Your idea is certainly the most valid one, but unfortunately I did make a mistake and did not realize that E is itself a power function of x(2)! Hence, I am not able to simply express one variable in terms of the other and solve 1 equation instead of 2.

I do thank you very much for your suggestions though. Your time is appreciated. If you get other thoughts, I'll be more than happy to hear and try them out.

Best,
Alaa

"Roger Stafford" wrote in message <j9f6di$ej8$1@newscl01ah.mathworks.com>...
> "Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <j9eton$ia1$1@newscl01ah.mathworks.com>...
> > Does this additional (and weird) observation give you a clue to any possible explanation? perhaps there is an alternative solution that I didn't think of?
> - - - - - - - - - - -
> In a set of two equations in two variables, when one of these variables can easily be expressed in terms of the other, as in your case, I would think it would improve matters to express the problem in terms of one equation and one variable. Your problem is equivalent to solving
>
> F(x2) = (x2+(D/F))*(B^x2+C^x2)-(A*E/F)*x2 = 0
>
> which is one equation, one variable, and, in effect, four parameters. The value of x1 can later be determined from
>
> x1 = (F/E) + (D/E)/x2
>
> You might find this less troublesome to solve and to find suitable initial estimates.
>
> Note: Your two expressions are inconsistent:
>
> x1 = A / (B^x2 + C^x2)
> F(1) = x1*(B^x2 - C^x2)- A;
>
> Roger Stafford

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Alaa Elwany

Date: 10 Nov, 2011 14:19:14

Message: 10 of 16

Thanks a lot Greg. I would like to try your scaling suggestion, but I am not sure how I can implement this in my code. You said "Make the replacement x1 = 1e-23*x3". Do you mean I should regard x3 and x4 as my variables, solve the equation, then transform back to x1 and x2 after I solve? Please advise.

Alaa

Greg Heath <heath@alumni.brown.edu> wrote in message <244944b4-f56a-4312-a540-9c77cc0cb51c@c18g2000yqj.googlegroups.com>...
> On Nov 9, 5:10 pm, "Alaa Elwany" <alaa_elw...@yahoo.com> wrote:
> > Thank you very much for your response Roger.  However, being familiar with this "Starting Solution" dilemma, I did have a validation problem instance, for which I knew in advance that the solution is x1 = 1.2e-23, x2=8.2   I set the starting solution to:
> > (1e-23,8.0) --> fsolve stalled, gave no solution and the same error message.
> >
> > It's driving me CRAZY!! Apparently, it is not a problem regarding "a good initial guess", but something else, and this is why I thought of a scaling issue, specially that the error message has to do with "step size".
> >
> > Does this additional (and weird) observation give you a clue to any possible explanation? perhaps there is an alternative solution that I didn't think of?
> >
> > Thank you very much for your concern again, regardless of whether the problem is resolved or not.
> >
> > Alaa
>
> What happens when you make the replacements x1 = 1e-23*x3 and x2 =
> 10*x4?
>
> Hope this helps.
>
> Greg

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Roger Stafford

Date: 10 Nov, 2011 16:31:11

Message: 11 of 16

"Mark Shore" wrote in message <j9fh8e$fdu$1@newscl01ah.mathworks.com>...
> Aggh... of course not. I originally read this on a small iPhone screen and it looked like B*X2 + C*X2. (I _was_ wondering about it, but not enough it seems.)
> ......
- - - - - - - - -
  I thought it might be something like that, Mark. I sympathesize. I would be at a total loss with something as small as an iPhone screen. Even with my 24-in iMac screen sometimes I have to do a "zoom" to see its symbols clearly.

Roger Stafford

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Roger Stafford

Date: 10 Nov, 2011 16:34:14

Message: 12 of 16

"Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <j9gmdq$54r$1@newscl01ah.mathworks.com>...
> Your idea is certainly the most valid one, but unfortunately I did make a mistake and did not realize that E is itself a power function of x(2)! .......
- - - - - - - - -
  Even so, you could still substitute the right hand side of the first equation in place of x1 in the second equation and obtain an equation involving only x2, which ought to give you far less difficulties than you are experiencing at present. Your first equation would then allow you to determine x1 after x2 was solved.

Roger Stafford

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Alaa Elwany

Date: 10 Nov, 2011 18:10:34

Message: 13 of 16

THANK YOU! WORKED!

Actually, Roger, I do feel stupid now. I realized what you wrote in this message a few minutes ago, went and tried it, and this idea worked.
Indeed, just substituting the RHS of Eqn (1) into Eqn (2) was sufficient, and fsolve seems to be having much less trouble with that.

Thank you very much Roger. That was a very simple, yet efficient, resolution. I feel bad because I should have thought about it myself, but you know, sometimes when you delve too deep into a problem, you miss very evident and simple resolutions like what happened with me now!!!! That's why it's useful to have a second opinion.

Thanks again for you and for all the participants in this thread.

Cheers!

"Roger Stafford" wrote in message <j9gue6$3td$1@newscl01ah.mathworks.com>...
> "Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <j9gmdq$54r$1@newscl01ah.mathworks.com>...
> > Your idea is certainly the most valid one, but unfortunately I did make a mistake and did not realize that E is itself a power function of x(2)! .......
> - - - - - - - - -
> Even so, you could still substitute the right hand side of the first equation in place of x1 in the second equation and obtain an equation involving only x2, which ought to give you far less difficulties than you are experiencing at present. Your first equation would then allow you to determine x1 after x2 was solved.
>
> Roger Stafford

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Greg Heath

Date: 11 Nov, 2011 07:01:24

Message: 14 of 16

On Nov 10, 9:19 am, "Alaa Elwany" <alaa_elw...@yahoo.com> wrote:
> Thanks a lot Greg.  I would like to try your scaling suggestion, but I am not sure how I can implement this in my code.  You said "Make the replacement x1 = 1e-23*x3".  Do you mean I should regard x3 and x4 as my variables, solve the equation, then transform back to x1 and x2 after I solve?  Please advise.
>
> Alaa
>
>
>
> Greg Heath <he...@alumni.brown.edu> wrote in message <244944b4-f56a-4312-a540-9c77cc0cb...@c18g2000yqj.googlegroups.com>...
> > On Nov 9, 5:10 pm, "Alaa Elwany" <alaa_elw...@yahoo.com> wrote:
> > > Thank you very much for your response Roger.  However, being familiar with this "Starting Solution" dilemma, I did have a validation problem instance, for which I knew in advance that the solution is x1 = 1.2e-23, x2=8.2   I set the starting solution to:
> > > (1e-23,8.0) --> fsolve stalled, gave no solution and the same error message.
>
> > > It's driving me CRAZY!! Apparently, it is not a problem regarding "a good initial guess", but something else, and this is why I thought of a scaling issue, specially that the error message has to do with "step size".
>
> > > Does this additional (and weird) observation give you a clue to any possible explanation? perhaps there is an alternative solution that I didn't think of?
>
> > > Thank you very much for your concern again, regardless of whether the problem is resolved or not.
>
> > > Alaa
>
> > What happens when you make the replacements x1 = 1e-23*x3 and x2 =
> > 10*x4?
>
> > Hope this helps.
>
> > Greg- Hide quoted text -
>
> - Show quoted text -

Yes.

Please do not TOP-POST.

Put your reply within and/or after the previous post.

HTH.

Greg

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Greg Heath

Date: 11 Nov, 2011 07:12:54

Message: 15 of 16

On Nov 9, 7:12 pm, "Mark Shore" <msh...@magmageosciences.ca> wrote:
> "Alaa Elwany" <alaa_elw...@yahoo.com> wrote in message <j9enqn$r4...@newscl01ah.mathworks.com>...
> > I am trying to solve 2 non-linear equations in two variables x1, and x2.
>
> > For simplicity, the 2 equations take the forms:
>
> > x1 = A / (B^x2 + C^x2)
> > x2 = D / (x1*E - F)
>
> Without taking anything away from Rogers's comment, why two constants B and C when they  could be replaced by a single constant (= B + C)?

No. The terms are eponential, not linear.

Hope this helps.

Greg

Subject: (fsolve) + (Scaling): Possibly Simple Problem - Urgent Help Needed

From: Mark Shore

Date: 11 Nov, 2011 19:25:29

Message: 16 of 16

> Greg Heath
> No. The terms are exponential, not linear.
>
> Hope this helps.
>
> Greg

Hi Greg. See my earlier comment to Roger. I misread the original post.

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