Ask Your Question
0

How do I assign variables from the solution of an equation?

asked 2012-11-27 02:15:24 -0500

William H. Hooper gravatar image

I was pleased to see how easily Sage solves systems of equations. For example, evaluating:

var('A,B')
f(x) = A*x + B
solve([f(3) == 5, f(7) == -3], A, B)

yields:

[[A == -2, B == 11]]

With a little text editing, I can use that solution to define f, which I can then plot or use to solve other equations:

A = -2; B = 11
f(x) = A*x + B
...

Is there a way to make the assignments of A and B (or more directly, f ) from the solution of the linear equations, without the copy+paste+edit step?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2012-11-27 06:00:49 -0500

ndomes gravatar image

I recommend the option solution_dict=True:

solve returns the solutions as list of python dictionaries and this makes substitution very easy.

var('A,B')
f(x) = A*x + B
sol = solve([f(3) == 5, f(7) == -3], A, B,solution_dict=True)
f.subs(sol[0])
edit flag offensive delete link more
0

answered 2012-11-27 03:01:28 -0500

William H. Hooper gravatar image

Thanks to kcrisman (http://ask.sagemath.org/question/347) I found a method using loops:

var('A,B')
f(x) = A*x + B
sol = solve([f(3) == 5, f(7) == -3], A, B)
for s in sol[0]:
    f = f.subs(s)
...

Now I can plot f, or use it to solve other equations. That's still a little cumbersome, when I have multiple equations to solve and substitutions to apply. If you have a loopless method, please post.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2012-11-27 02:15:24 -0500

Seen: 646 times

Last updated: Nov 27 '12