Ask Your Question
1

How to use equation solutions returned by `solve`?

asked 5 years ago

Markio gravatar image

updated 5 years ago

slelievre gravatar image

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=...

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 5 years ago

Juanjo gravatar image

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.

Preview: (hide)
link

Comments

Excellent point! I turned your comment into a proper answer!

slelievre gravatar imageslelievre ( 5 years ago )
1

answered 5 years ago

slelievre gravatar image

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))
Preview: (hide)
link

Comments

Thank you very much, I will try these out.

Markio gravatar imageMarkio ( 5 years ago )

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

slelievre gravatar imageslelievre ( 5 years ago )

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: 5 years ago

Seen: 369 times

Last updated: Jun 05 '19