# How to use equation solutions returned by solve?

While obtaining the answer for A3 and A3 in a set of equations, the answer is in vector-format(??). I want to use these answer further on. How can I pick these values? Right now I just copy and paste the answer but that's not ideal.

IN:

eq1=-A3-A4*K*l+A3*cos(K*l)+A4*sin(K*l)+((qzc*l^2)/(2*K^2))==0

eq2=-A3*K^2*cos(K*l)-A4*K^2*sin(K*l)+(qzc/(K^2))==0

solve([eq1,eq2],A3,A4)


OUT:

[[A3 == 1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l)), A4 == 1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))]] 

How to get A3=... and A4=...

edit retag close merge delete

Sort by » oldest newest most voted

As another option, you can extract the values of A3 and A4 through the subs method:

sage: sol_A3 = A3.subs(solutions)
sage: sol_A4 = A4.subs(solutions)
sage: sol_A3
1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))
sage: sol_A4
1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))


This also works if the solutions are given as a dictionary.

more

( 2019-06-06 10:06:16 +0200 )edit

Give a name to the solutions:

sage: var('A3 A4 K l qzc')
(A3, A4, K, l, qzc)
sage: eq1 = -A3-A4*K*l+A3*cos(K*l)+A4*sin(K*l)+((qzc*l^2)/(2*K^2)) == 0
sage: eq2 = -A3*K^2*cos(K*l)-A4*K^2*sin(K*l)+(qzc/(K^2)) == 0
sage: solutions = solve([eq1, eq2], A3, A4)


This is now a list of solutions, with just one element, solutions[0].

This element is a list of two values described as equations: A3 == ..., A4 = ....

So we can get A3 and A4 as follows:

sage: sol = solutions[0]
sage: sol_A3 = sol[0].rhs()
sage: sol_A4 = sol[1].rhs()
sage: sol_A3
1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))
sage: sol_A4
1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))


Another way is to tell solve to return each solution as a dictionary.

sage: sol_dict = solve([eq1, eq2], A3, A4, solution_dict=True)
sage: sol_dict
[{A3: 1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l)),
A4: 1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))}]


Then we can get the individual solutions:

sage: sol_A3 = sol_dict[0][A3]
sage: sol_A4 = sol_dict[0][A4]
sage: sol_A3
1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))
sage: sol_A4
1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))

more

Thank you very much, I will try these out.

( 2019-06-05 12:52:52 +0200 )edit

@Markio: Don't hesitate to accept @Juanjo's answer instead -- I think it's better!

( 2019-06-06 10:07:54 +0200 )edit