Ask Your Question
2

Manipulate dummy variable after solving trig

asked 3 years ago

jakupl gravatar image

updated 3 years ago

Hi! This might be somewhat basic, but is there a way to directly reference the dummy variable that pops up, when solving trigonometric functions?

sy(t)=sin(t)/2+7*sin(t/3)/2
sols=solve(sy(t)==0,t, to_poly_solve='force')
sols[0]

returns:

t == 3*pi + 6*pi*z27541

This dummy variable obviously changes every time the command is run, so there is no way of directly referencing it without changing the reference every time. Obviously this doesn't work:

sols[0](z=0)

The closest I have come, is just blindly referencing the variable which works if there are no other variables in the right hand side, like this:

sols[0].rhs()(0)

but this gives the obnoxious error message, that "Substitution using function-call syntax and unnamed arguments is deprecated (...)"

Is there a way to cleanly tell sage to evaluate an expression, when all z variables are 0 or 1 etc?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 3 years ago

cav_rt gravatar image

updated 3 years ago

You can get the dummy variable by

dummy_var = sols[0].rhs().variables()[0]

And then define a callable function changing the dummy variable to a proper symbolic one.

var('z')
f(z) = sols[0].rhs().subs({dummy_var:z})

Now you can simply do f(0), f(1) or whatever you want.

PS, for some reason it does not work if you try to define a callable function using the dummy variable directly, like

f(dummy_var) = sols[0].rhs()

EDIT:

Another possibility is to use the solveset from sympy

from sympy import solveset
solveset(sy(t)._sympy_())

It gives the solution as a union of sets.

Preview: (hide)
link

Comments

Thank you! I had been trying something like this, but it's the curly brackets that got me. Not experienced enough in python yet. My life will be so much easier now, being able to quickly do:

var('z')
solz=[]
for i in range(len(sols)):
    dummy_var = sols[i].rhs().variables()[0]
    solz.append(sols[i].rhs().subs({dummy_var:z}))

(as long as there are not other variables obviously.)

jakupl gravatar imagejakupl ( 3 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: 3 years ago

Seen: 351 times

Last updated: Dec 07 '21