Ask Your Question
1

How to use equation solutions returned by `solve`?

asked 2019-06-05 12:04:59 +0100

Markio gravatar image

updated 2019-06-05 16:06:20 +0100

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

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2019-06-05 19:26:01 +0100

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.

edit flag offensive delete link more

Comments

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

slelievre gravatar imageslelievre ( 2019-06-06 10:06:16 +0100 )edit
1

answered 2019-06-05 12:36:58 +0100

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))
edit flag offensive delete link more

Comments

Thank you very much, I will try these out.

Markio gravatar imageMarkio ( 2019-06-05 12:52:52 +0100 )edit

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

slelievre gravatar imageslelievre ( 2019-06-06 10:07:54 +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: 2019-06-05 12:04:59 +0100

Seen: 328 times

Last updated: Jun 05 '19