ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 25 Mar 2022 23:23:23 +0100How to tabulate $(r\cos\theta,r\sin\theta)$ from $(t,r,\theta)$ obtained from solving a system of ODE using RK4?https://ask.sagemath.org/question/61608/how-to-tabulate-rcosthetarsintheta-from-trtheta-obtained-from-solving-a-system-of-ode-using-rk4/ I am trying to solve a system of first-order differential equations of the following form:
$$\frac{dr}{dt}=f(r,\theta), \quad\frac{d\theta}{dt}=g(r,\theta)$$
I am using desolve_system_rk4 which gives me a list of triples $(t,r,\theta)$. Now I need to plot the trajectory in the xy-plane. For this, I require to obtain the x and y coordinate using
$$x=r\cos\theta, \quad y=r\sin\theta$$
and then obtain the xy-plot. How can I tabulate the pair $(r\cos\theta,r\sin\theta)$ from $(t,r,\theta)$?Thu, 24 Mar 2022 13:35:03 +0100https://ask.sagemath.org/question/61608/how-to-tabulate-rcosthetarsintheta-from-trtheta-obtained-from-solving-a-system-of-ode-using-rk4/Comment by tolga for <p>I am trying to solve a system of first-order differential equations of the following form:
$$\frac{dr}{dt}=f(r,\theta), \quad\frac{d\theta}{dt}=g(r,\theta)$$
I am using desolve_system_rk4 which gives me a list of triples $(t,r,\theta)$. Now I need to plot the trajectory in the xy-plane. For this, I require to obtain the x and y coordinate using
$$x=r\cos\theta, \quad y=r\sin\theta$$
and then obtain the xy-plot. How can I tabulate the pair $(r\cos\theta,r\sin\theta)$ from $(t,r,\theta)$?</p>
https://ask.sagemath.org/question/61608/how-to-tabulate-rcosthetarsintheta-from-trtheta-obtained-from-solving-a-system-of-ode-using-rk4/?comment=61614#post-id-61614Can a parametric plot solve your problem?Thu, 24 Mar 2022 16:02:51 +0100https://ask.sagemath.org/question/61608/how-to-tabulate-rcosthetarsintheta-from-trtheta-obtained-from-solving-a-system-of-ode-using-rk4/?comment=61614#post-id-61614Comment by rburing for <p>I am trying to solve a system of first-order differential equations of the following form:
$$\frac{dr}{dt}=f(r,\theta), \quad\frac{d\theta}{dt}=g(r,\theta)$$
I am using desolve_system_rk4 which gives me a list of triples $(t,r,\theta)$. Now I need to plot the trajectory in the xy-plane. For this, I require to obtain the x and y coordinate using
$$x=r\cos\theta, \quad y=r\sin\theta$$
and then obtain the xy-plot. How can I tabulate the pair $(r\cos\theta,r\sin\theta)$ from $(t,r,\theta)$?</p>
https://ask.sagemath.org/question/61608/how-to-tabulate-rcosthetarsintheta-from-trtheta-obtained-from-solving-a-system-of-ode-using-rk4/?comment=61609#post-id-61609Please add your code using `desolve_system_rk4`, so that it's easier to give an answer.Thu, 24 Mar 2022 13:38:03 +0100https://ask.sagemath.org/question/61608/how-to-tabulate-rcosthetarsintheta-from-trtheta-obtained-from-solving-a-system-of-ode-using-rk4/?comment=61609#post-id-61609Answer by Emmanuel Charpentier for <p>I am trying to solve a system of first-order differential equations of the following form:
$$\frac{dr}{dt}=f(r,\theta), \quad\frac{d\theta}{dt}=g(r,\theta)$$
I am using desolve_system_rk4 which gives me a list of triples $(t,r,\theta)$. Now I need to plot the trajectory in the xy-plane. For this, I require to obtain the x and y coordinate using
$$x=r\cos\theta, \quad y=r\sin\theta$$
and then obtain the xy-plot. How can I tabulate the pair $(r\cos\theta,r\sin\theta)$ from $(t,r,\theta)$?</p>
https://ask.sagemath.org/question/61608/how-to-tabulate-rcosthetarsintheta-from-trtheta-obtained-from-solving-a-system-of-ode-using-rk4/?answer=61624#post-id-61624Starting from the example given in the documentation that you could have bothered to read by typing `desolve_system_rk4?` :
sage: from sage.calculus.desolvers import desolve_system_rk4
sage: x,y,t=var('x y t')
sage: P=desolve_system_rk4([x*(1-y),-y*(1-x)],[x,y],ics=[0,0.5,2],ivar=t,end_points=20)
you can see that the solution `P` is a list of `[t, x, y]` triplets. You can display the `x` and `y` functions by :
sage: line2d([[i, j] for i, j, k in P])
sage: line2d([[i, k] for i, j, k in P])
(`line2d` interpolates between the points that `list_plot` displays, which is aesthetically more pleasing).Your phase trajectory can therefore be displayed by :
sage: line2d([[j,k] for i, j, k in P])
HTH,Fri, 25 Mar 2022 23:23:23 +0100https://ask.sagemath.org/question/61608/how-to-tabulate-rcosthetarsintheta-from-trtheta-obtained-from-solving-a-system-of-ode-using-rk4/?answer=61624#post-id-61624