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.Tue, 04 Nov 2014 00:52:38 +0100solving a system of DEs numerically and plotting the solutionhttps://ask.sagemath.org/question/24766/solving-a-system-of-des-numerically-and-plotting-the-solution/Hi everyone. I want to hand Sage a system of nonlinear DEs with initial values, and I'd like a plot of the solutions. Is there a best way to do this? For example, to solve this system: ds/dt=-si, di/st=si-2i, dr/dt=2i, s(0)=1, i(0)= 0.00000127, r(0)=0, I did this in Sage:
sage: (s,i,r)=var('s,i,r')
sage: des=[-1*s*i, s*i-2*i, i]
sage: desolve_system_rk4(des, [s,i,r], ics=[0, 1, 0.00000127, 0], ivar=t, end_points=20)
Sage gave me a list of points, but I couldn't figure out a way to plot s, i, and r using the points it gave me. Thanks!
Mon, 03 Nov 2014 21:32:05 +0100https://ask.sagemath.org/question/24766/solving-a-system-of-des-numerically-and-plotting-the-solution/Answer by calc314 for <p>Hi everyone. I want to hand Sage a system of nonlinear DEs with initial values, and I'd like a plot of the solutions. Is there a best way to do this? For example, to solve this system: ds/dt=-si, di/st=si-2i, dr/dt=2i, s(0)=1, i(0)= 0.00000127, r(0)=0, I did this in Sage:</p>
<p>sage: (s,i,r)=var('s,i,r')</p>
<p>sage: des=[-1<em>s</em>i, s<em>i-2</em>i, i]</p>
<p>sage: desolve_system_rk4(des, [s,i,r], ics=[0, 1, 0.00000127, 0], ivar=t, end_points=20)</p>
<p>Sage gave me a list of points, but I couldn't figure out a way to plot s, i, and r using the points it gave me. Thanks!</p>
https://ask.sagemath.org/question/24766/solving-a-system-of-des-numerically-and-plotting-the-solution/?answer=24767#post-id-24767 Try this:
ans=desolve_system_rk4(des, [s,i,r], ics=[0, 1, 0.00000127,0], ivar=t, end_points=20)
ts=[[a[0],a[1]] for a in ans]
ti=[[a[0],a[2]] for a in ans]
tr=[[a[0],a[3]] for a in ans]
line(ts)+line(ti,color='red')+line(tr,color='green')Mon, 03 Nov 2014 22:17:39 +0100https://ask.sagemath.org/question/24766/solving-a-system-of-des-numerically-and-plotting-the-solution/?answer=24767#post-id-24767Comment by strangelove1661 for <p>Try this:</p>
<pre><code>ans=desolve_system_rk4(des, [s,i,r], ics=[0, 1, 0.00000127,0], ivar=t, end_points=20)
ts=[[a[0],a[1]] for a in ans]
ti=[[a[0],a[2]] for a in ans]
tr=[[a[0],a[3]] for a in ans]
line(ts)+line(ti,color='red')+line(tr,color='green')
</code></pre>
https://ask.sagemath.org/question/24766/solving-a-system-of-des-numerically-and-plotting-the-solution/?comment=24769#post-id-24769Thanks for the help.Tue, 04 Nov 2014 00:52:38 +0100https://ask.sagemath.org/question/24766/solving-a-system-of-des-numerically-and-plotting-the-solution/?comment=24769#post-id-24769