Message Boards Message Boards

[Solved] Storing solutions of systems of equations in a loop

Hello, I have a system of equations that I solve with respect to three variables : pd, pf and lambda. I solve it for two different values of a parameter (eqns1 and eqns2) and over a variety of values for cd.

I now want to store the results as a matrix with three rows : cd, lambda from eqns1 and lambda from eqns2 in order to be able to represent graphically the two rows of lambda with respect to cd.

Any suggestions on how to store the results as a unique matrix?

conds = {pd > 0 && pf > 0, pd \[Element] Reals, pf \[Element] Reals, 
  lambda >= 0, lambda \[Element] Reals}
eqns1 = {
pd + cd (-2 + 1/(2 + ((1 + lambda^(3/4)) 2 pf*2)/pd*2)) == 0,
       10 pf ==   27/2 + (9 pd*2)/((2 + 8 lambda*3/4 + 4 lambda*3/2) pd*2 +  4 (1 + lambda^(3/4)) 4 pf*2), 
      4/3 (1 + lambda^(3/4)) 3 lambda*1/4 == (10 (-cd + pd))/(pd*3 (1/pd^2 + 1/((1 + lambda^(3/4))^2 pf^2)) 3/2 pf*2)
}
    eqns2 = {
    pd + cd (-2 + 1/(2 + ((1 + lambda^(3/4)) 2 pf*2)/pd*2)) == 0,
       5 pf == 6 +  1/(1/2 + 2 lambda*3/4 +  lambda*3/2 + ((1 + lambda^(3/4)) 4 pf*2)/pd*2), 
      4/3 (1 + lambda^(3/4)) 3 lambda*1/4 == (10 (-cd + pd))/(pd*3 (1/pd^2 + 1/((1 + lambda^(3/4))^2 pf^2)) 3/2 pf*2)
}
    For[cd = 0.1, cd < 0.5, cd += 0.1, 
     Print[sol1 = 
       FullSimplify[NSolve[Join[eqns1, conds], {pd, pf, lambda}]], 
      sol2 = FullSimplify[NSolve[Join[eqns2, conds], {pd, pf, lambda}]]]]
6 Replies
Posted 1 year ago

Edit:

What I would try is

conds={};
eqns1={
pd+cd (-2+1/(2+((1+lambda^(3/4))2 pf*2)/pd*2))==0,
10 pf==27/2+(9 pd*2)/((2+8 lambda*3/4+4 lambda*3/2) pd*2+4 (1+lambda^(3/4))4 pf*2),
4/3 (1+lambda^(3/4))3 lambda*1/4==(10 (-cd+pd))/(pd*3 (1/pd^2+1/((1+lambda^(3/4))^2 pf^2))3/2 pf*2)
};
eqns2={
pd+cd (-2+1/(2+((1+lambda^(3/4))2 pf*2)/pd*2))==0,
5 pf==6+1/(1/2+2 lambda*3/4+lambda*3/2+((1+lambda^(3/4))4 pf*2)/pd*2),
4/3 (1+lambda^(3/4))3 lambda*1/4==(10 (-cd+pd))/(pd*3 (1/pd^2+1/((1+lambda^(3/4))^2 pf^2))3/2 pf*2)
};
matrix=Reap[
For[cd=0,cd<1/2,cd+=0.1,
 sol1=FullSimplify[NSolve[Join[eqns1,conds],{pd,pf,lambda}]];
 sol2=FullSimplify[NSolve[Join[eqns2,conds],{pd,pf,lambda}]];
 Sow[{cd,lambda/.sol1[[1]],lambda/.sol2[[1]]}]
]][[2,1]]

which intends to create a matrix of three columns, cd, lambda from 1 and lambda from 2.

But when I run that I don't get any solutions for lambda in any of those.

POSTED BY: Bill Nelson

Thank you for your answer. You don't find any solutions because it should be eqns1=... and eqns2=... without capital letters. Sorry, my message got auto corrected, I just edited my post.

I have tried your way and I have two errors when solving: ReplaceAll::reps and General::stop. Any idea why?

Posted 1 year ago

Ah, the errors I've seen autocorrect introduce.

You also have pd2,pf2,lambda1,lambda3,pd3 which may be pd*2,pf*2,lambda*1,lambda*3,pd*3 or maybe not. And you don't show what conds are.

I've made guesses and changed my code. I only did the first 5 solutions to make it faster while trying to understand why this may still have problems.

Can you revise your code to make it closer to what you really want? Thanks

POSTED BY: Bill Nelson
Posted 1 year ago

Please, for cd==0 manually show the matrice you want. Repeat that for cd==1/10. Maybe from that I can understand what it is you want in what form and where you want it stored. Thank you

Or you might think of using Table instead of For. Table can store the result of your calculations with each iteration and give you all those when it is finished.

POSTED BY: Bill Nelson
Posted 1 year ago

Sorry I didn't understand it properly, but the "matrix" matrix is exactly what I wanted. Thanks a lot!

POSTED BY: Updating Name
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