Ask Your Question
2

How to substitute an equation into another equation to simplify it?

asked 2020-12-14 20:23:33 +0100

rhymesayers gravatar image

I solved a system of equations. A solution of one of the coefficients can be plugged into another solution of a separate coefficient. How do I do this?

System of Equations

eqn1 = B0*Q0 == A0*P0 + C*(3*sigma0**2 + 1)
eqn2 = B2*Q2 == A2*P2 + 2*C
eqn3 = B0*kf*Q0d == A0*kp*P0d + 6*kp*C*sigma0
eqn4 = B2*kf*Q2d == A2*kp*P2d
sol = solve( [eqn1, eqn2, eqn3, eqn4], [B0, A0, B2, A2] )

I separated the solutions to the coefficients from the list.

sola = sol[0]
B0 = sola[0].rhs(); B0 = B0.substitute(P0d = 0)
A0 = sola[1].rhs(); A0 = A0.substitute(P0d = 0, P0 =1)
print(B0)
print(A0)

Output

6*C*kp*sigma0/(Q0d*kf)
-(3*C*Q0d*kf*sigma0^2 - 6*C*Q0*kp*sigma0 + C*Q0d*kf)/(Q0d*kf)

Now how can I substitute B0 into A0 to simplify the coefficient A0?

Thanks!

enter code here
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2020-12-14 21:45:16 +0100

slelievre gravatar image

updated 2020-12-15 14:35:18 +0100

Using the solution_dict option of solve makes it easier to use the solutions afterwards.

Here is an example based on the code in the question.

First we need to declare variables.

sage: var('B0 Q0 A0 P0 sigma0 B2 Q2 A2 P2 kf Q0d kp P0d C Q2d P2d')

Define equations:

sage: eqn1 = B0*Q0 == A0*P0 + C*(3*sigma0**2 + 1)
sage: eqn2 = B2*Q2 == A2*P2 + 2*C
sage: eqn3 = B0*kf*Q0d == A0*kp*P0d + 6*kp*C*sigma0
sage: eqn4 = B2*kf*Q2d == A2*kp*P2d

Solve and check out solutions:

sage: sol = solve([eqn1, eqn2, eqn3, eqn4], [B0, A0, B2, A2], solution_dict=True)
sage: len(sol)
1
sage: sol
[{B0: -(3*C*P0d*kp*sigma0^2 - 6*C*P0*kp*sigma0 + C*P0d*kp)/(P0*Q0d*kf - P0d*Q0*kp),
  A0: -(3*C*Q0d*kf*sigma0^2 - 6*C*Q0*kp*sigma0 + C*Q0d*kf)/(P0*Q0d*kf - P0d*Q0*kp),
  B2: -2*C*P2d*kp/(P2*Q2d*kf - P2d*Q2*kp),
  A2: -2*C*Q2d*kf/(P2*Q2d*kf - P2d*Q2*kp)}]

Take the first solution (a dictionary):

sage: sola = sol[0]
sage: sola
{B0: -(3*C*P0d*kp*sigma0^2 - 6*C*P0*kp*sigma0 + C*P0d*kp)/(P0*Q0d*kf - P0d*Q0*kp),
 A0: -(3*C*Q0d*kf*sigma0^2 - 6*C*Q0*kp*sigma0 + C*Q0d*kf)/(P0*Q0d*kf - P0d*Q0*kp),
 B2: -2*C*P2d*kp/(P2*Q2d*kf - P2d*Q2*kp),
 A2: -2*C*Q2d*kf/(P2*Q2d*kf - P2d*Q2*kp)}

Values of the solutions:

sage: sola[A0]
-(3*C*Q0d*kf*sigma0^2 - 6*C*Q0*kp*sigma0 + C*Q0d*kf)/(P0*Q0d*kf - P0d*Q0*kp)
sage: sola[B0]
-(3*C*P0d*kp*sigma0^2 - 6*C*P0*kp*sigma0 + C*P0d*kp)/(P0*Q0d*kf - P0d*Q0*kp)
sage: sola[A2]
-2*C*Q2d*kf/(P2*Q2d*kf - P2d*Q2*kp)
sage: sola[B2]
-2*C*P2d*kp/(P2*Q2d*kf - P2d*Q2*kp)

Simple substitutions:

sage: B = sola[B0].subs({P0d: 0})
sage: B
6*C*kp*sigma0/(Q0d*kf)
sage: A = sola[A0].subs({P0d: 0, P0: 1})
sage: A
-(3*C*Q0d*kf*sigma0^2 - 6*C*Q0*kp*sigma0 + C*Q0d*kf)/(Q0d*kf)

Noticing that one of the terms in A looks like B, we are tempted to substitute.

Unfortunately:

sage: A.subs({B: B0})
-(3*C*Q0d*kf*sigma0^2 - 6*C*Q0*kp*sigma0 + C*Q0d*kf)/(Q0d*kf)

Expanding and matching an exact term works:

sage: A.expand()
-3*C*sigma0^2 + 6*C*Q0*kp*sigma0/(Q0d*kf) - C

sage: A.expand().subs({B*Q0: B0*Q0})
-3*C*sigma0^2 + B0*Q0 - C
edit flag offensive delete link more

Comments

I appreciate you taking the time to help me out, but the last line of code doesn't actually substitute B0 into A0. It would look something like this:

-(3*C*Q0d*kf*sigma0^2  + C*Q0d*kf)/(Q0d*kf) + Q0*B0

Or simplified further

B0*Q0 - C*(3*sigma0^2 + 1)

Or is there anyway to simplify (the dictionary) as a whole when it comes out of the solve function?

rhymesayers gravatar imagerhymesayers ( 2020-12-14 22:46:20 +0100 )edit
1

You can simply get A0 in terms of B0 from the first equation:

sage: A0.subs(solve(eqn1,A0)).subs(P0=1)
-3*C*sigma0^2 + B0*Q0 - C
Juanjo gravatar imageJuanjo ( 2020-12-15 00:22:59 +0100 )edit
1

Edited my answer to name A and B the results of simple substitutions, and to add the "Noticing... Unfortunately ... Expanding and matching ..." part.

What @Juanjo proposes is probably more satisfactory!

slelievre gravatar imageslelievre ( 2020-12-15 14:36:48 +0100 )edit

Thank you, this was giving me a headache! You guys are good and timely! @Juanjo, @slelievre

rhymesayers gravatar imagerhymesayers ( 2020-12-15 18:48:07 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-12-14 20:23:33 +0100

Seen: 783 times

Last updated: Dec 15 '20