Message Boards Message Boards

Solving system of equations with Solve takes too long?

Hello, I have been trying to solve the system below but it executes forever and doesn't seem to find an answer.

F[x_]:=x/(1+x)

lambda=l1+l2+l3

c1=RandomReal[3]

c2=RandomReal[3]

c3=RandomReal[3]

c4=RandomReal[3]

pn=(P1^(-2)+P2^(-2)+P3^(-2)+(P4*F[lambda]/2)^(-2))^(-1/2)

s1=(P1/pn)^(-1/2)

s2=(P2/pn)^(-1/2)

s3=(P3/pn)^(-1/2)

s4=(P4/pn)^(-1/2)  

Solve[P1\[Equal] (2*(1-s1))/(3*(1-s1)+s1)*c1&&P2\[Equal] (2*(1-s2))/(3*(1-s2)+s2)*c2&&P3==(2*(1-s3))/(3*(1-s3)+s3)*c3&& P4\[Equal] (2*(1-s4))/(3*(1-s4)+s4)*c4&&y*(1-c1/P1)*2*s1*(s4)\[Equal](1+F[lambda]/2)^3 /(D[F[lambda],l1]/2)&&y*(1-c2/P2)*2*s2*(s4)\[Equal](1+F[lambda]/2)^3 /(D[F[lambda],l2]/2)&&y*(1-c3/P3)*2*s3*(s4)\[Equal](1+F[lambda]/2)^3 /(D[F[lambda],l3]/2),{P1,P2,P3,P4,l1,l2,l3}]  

I have been trying to solve the equations one after the other in the following way :

sol1=Solve[P1 == (2*(1-s1))/(3*(1-s1)+s1)*c1,{P1}] 

X1=Array[x1,{Length@sol1}]

x1[k_] /; MemberQ[Range[Length@sol1],l] :=sol1[[k,1,2]]

P1=X1


sol2=Solve[P2 == (2*(1-s2))/(3*(1-s2)+s2)*c2,{P2}] 

X2=Array[x2,{Length@sol2}]

x2[k_] /; MemberQ[Range[Length@sol2],l] :=sol2[[k,1,2]]

P2=X2  

The first part works and I was planning to continue this way but the second part returns with a serie of errors. And still, even the first part takes a very long time to run...

Can you tell please tell me why it doesn't work or/and whether there is an other way to have a result in a faster way?

12 Replies

I think, this

2=(P2/pn)^(-1/2)

is a typo. Shouldn't it be

s2=(P2/pn)^(-1/2)
POSTED BY: Hans Dolhaine

Are you sure it is F(lambda) instead of F[lambda]? If you define c1 = RandomReal[3,1] you get a list instead of a number: is that really what you want? I would simply write c1 = RandomReal[3].

POSTED BY: Gianluca Gorni

And what is

pP

it is nowhere defined

POSTED BY: Hans Dolhaine

Yes, thank you, I have edited my post

Another typo, I am sorry. I have corrected it.

You are right about F[lambda], thank you. About the RandomReal[3,1] I guess you are right too, even if I am not sure it is problematic to have a list of one element instead of a number. I have corrected my post anyway, for both of your remarks.

So, then be so kind and show the corrected code. But these equations look complicated.

POSTED BY: Hans Dolhaine

I have edited my post to correct for your observations, thank you.

Posted 3 years ago

I could not solve your problem, but tried a lot which might be helpful. The basic problem is that your equations are far too complicated and must be simplified before use. But even this does not work straight forward.

See the attached notebook "210219 Solve Simplify.nb" to understand what I was thinking about.

There is pretty strange and incorrect code within your first post: Use a block or module, use semicolons and commas, avoid symbols starting with uppercase letters. The code can be formulated shorter. Solve and everything else should run over Reals.

Since you don't solve for y is it true that y is some fixed given general parameter?

I have added the symbol eqns for your system of equations and simplified it. I realized that your original system is too complicated. It can't even be simplified, let alone solved, in a reasonable amount of time.

Even by assuming some conditions conds the system cannot be simplified. Just by examining your original system I found necessary conditions (that are hopefully correct for your original problem): - The p1, p2, p3, p4 have to be positive reals. - The l1, l2, l3 have to be real. - The sum lambda = l1+l2+l3 has to be real and unequal to -1 and unequal to 0. - The given value y has to be real.

Unfortunatly this system cannot be simplified by Simplify or FullSimplify. I try to solve it anyway for the variables p1, p2, p3, p4, l1, l2, l3, lambda, which doesn't succeed. This is expected and the same what you observed.

All of this is still too complicated to be simplified and solved. See the following code marked with comments "First Attempt". This is exactly your code, only reformulated.

There are several problems with your equations which pre-empt simplification: - The complicated expression for pn appears over and over again. - The expression l1+l2+l3 appears over and over again.

That's why in the second attempt (marked with "Second Attempt" and the changes marked with "--#--"): - I didn't assign pn first, but left it unevaluated for now, then simplified everything and inserted it only at the very end. - I didn't assign lambda first, but left it unevaluated for now, then simplified everything and inserted it only at the very end. - Actually I reused fl = f[lambda] and dfls = D[f[lambda],li], i=1,2,3, from the first attempt and replaced the sums l1+l2+l3 by lambda. - I used the fact that all partial derivatives dfls are equal and independend of the li. I name them dfl. This gives the new equations eqns2.

The conditions conds2 slighty change: - From the definition of pn one can see that pn must be real positive. - The value of lambda has to be real and unequal to -1. - l1, l2, l3 do no longer occur.

This system can now be significantly simplified. I try to solve it then for the variables p1, p2, p3, p4, pn, lambda. But this does not succeed. Hence we are not further on than before.

If this succeeded I would back-replace pn and lambda into the solution, possibly solve again a new System and verify that the solution satisfies the original system. This is not yet coded.

As an extra step I try to proof that after back-replacement of pn and lambda into eqns2 and a further simplification step the new system eqns2a is equivalent to the original system eqns under conds. Usually such proofs are done with Resolve, but this doesn't succeed either. May be the proof is too complicated for Resolve, or I made some error and eqns2a is indeed not equivalent to eqns.

I think you need to significantly simplify your original problem and thus its equations to arrive at a solution. Formal simplification, as I have tried, is not sufficient, but you have to start with the underlying physics (or whatever).

The following code within the attached notebook executes both attempts. Set test=True to see what happens. Set maxTime as the maximal time for execution of FullSimplify and Resolve, maxTimeS as the maximal time for execution of Solve. I use them just for forcing continuation and termination of the program within reasonable time.

Block[{test = True, maxTime = 5, maxTimeS = 10, maxT, f, lambda, l1, 
  l2, l3, fl, dfls, attempt, c1, c2, c3, c4, pn, s1, s2, s3, s4, p1, 
  p2, p3, p4, y, eqns, conds, eqns0, abort, sol, pn2, lambda2, fl2, 
  dfl2s, dfl2, eqns2, conds2, sol2, eqns2a, proof, proofRes, sol2a},

 (* Defining the base function and calculating its partial \
derivatives *)
 f[x_] := x/(1 + x);

 (*... 
 See the attached notebook for the complete coding 
 ... *)

 (* Solving the system *)
 maxT = If[! abort, maxTimeS, maxTime];
 abort = False;
 sol2a = TimeConstrained[
   Solve[Join[eqns2a, conds2], {p1, p2, p3, p4, l1, l2, l3}, Reals]
   , maxT, abort = True];
 If[abort, sol2a = {}; 
  Echo[Row[{"no solution after ", maxT, " sec"}], 
   attempt <> "Solve aborted: "]];
 If[test && ! abort, Echo[Column[sol2a], attempt <> "sol2a:\n"]];
 (* Return solution *)
 Print[Style[Row[{attempt, "sol2a = ", Column[sol2a]}], Blue, Bold]];
 Print[];
 ]
POSTED BY: Werner Geiger
Posted 3 years ago

Strange, Olimpia, that you don't answer. I would like to hear if I could help you a little.

POSTED BY: Werner Geiger

Dear Werner, Thank you very much for all your work and great explanations. I have been working on it for the last couple of days, and your help was very useful.

First of all thanks a lot for the reformulation of my code in the "first attempt", as you have noticed I am very new to this language, and it helped me a lot to learn.

It is true that I consider y as a fixed general parameter and you are totally right, that the first derivatives are the same and this simplifies the code. The conditions that pn must be a real positive is totally fine by assumption (and lambda different from one too).

For your second attempt, I think fixing pn and lambda in a first step should be a godd way to proceed even if it not working yet and I think I will continue on this way, 1) simplifying the problem (I will try to remove one equation by imposing p3=l3=0), 2) trying to show that this second system - where we replace lambda and pn afterwards is equivalent to the first one. I will certainly let you know here if this way I manage to solve it.

I also wanted to tell that one of the reasons I wanted to solve this problem was to find whether their actually is a solution (and I have the intuition there might not be).

Again, thanks a lot!

Posted 3 years ago

I'm glad that I could help a little.

POSTED BY: Werner Geiger
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract