Ask Your Question
0

How to get access of a single solution from a bunch of solutions

asked 2020-10-21 07:12:04 -0600

geroyx gravatar image

I do not use the thing for longer time, so I had a question.

If I say

a,b,c,d,e,x = var('a b c d e x')
f(x)=a*x^4+b*x^3+c*x^2+d*x+e
f1(x)=diff(f(x),x)
f2(x)=diff(f(x),x,2)
sols = solve([f(0)==-2, f(2)==0,  f1(4)==0, f(4)==8,  f2(2)==0], a,b,c,d,e)
print(sols)

I get

 [
[a == (3/16), b == (-41/16), c == (87/8), d == -12, e == -2]
]

How can I have these single value separately?
That means: I want to get an output f(x) = 3/16*x^4 -41/16*x^3 +87/8*x^2 -12*x -2
(which I can copy and paste).

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2020-10-21 11:00:32 -0600

tmonteil gravatar image

updated 2020-10-21 16:40:01 -0600

slelievre gravatar image

The first trick is to use the solution_dict parameter to get the solutions as Python dictionaries, instead of lists of expressions:

sage: relations = [f(0) == -2, f(2) == 0,  f1(4) == 0, f(4) == 8,  f2(2) == 0]
sage: sols = solve(relations, a, b, c, d, e, solution_dict=True)
sage: sols
[{a: 3/16, b: -41/16, c: 87/8, d: -12, e: -2}]

As you can see, sols is a list, with a single element, that is a dictionary:

sage: sols[0]
{a: 3/16, b: -41/16, c: 87/8, d: -12, e: -2}
sage: sols[0][a]
3/16
sage: sols[0][b]
-41/16

Now, you can use this dictionary to substitute the symbols a, b, c, d, e with their values:

sage: f.substitute(sols[0])
x |--> 3/16*x^4 - 41/16*x^3 + 87/8*x^2 - 12*x - 2
edit flag offensive delete link more

Comments

Ah, nice ;)

geroyx gravatar imagegeroyx ( 2020-10-21 11:20:48 -0600 )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-10-21 07:12:04 -0600

Seen: 55 times

Last updated: Oct 21