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.Sat, 28 Mar 2020 17:50:13 +0100Plot solutions of two differential equations on same axes with legendhttps://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/I need to plot two differential equations on the same set of axes
(with a legend showing which is which).
My domain has to be 0 ≤ t ≤ 100.
Listed below is my code, any help is greatly appreciated.
t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = sol[0].rhs()
g(t) = sol[1].rhs()Fri, 27 Mar 2020 21:59:50 +0100https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/Comment by John Palmieri for <p>I need to plot two differential equations on the same set of axes
(with a legend showing which is which).</p>
<p>My domain has to be 0 ≤ t ≤ 100.</p>
<p>Listed below is my code, any help is greatly appreciated.</p>
<pre><code>t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = sol[0].rhs()
g(t) = sol[1].rhs()
</code></pre>
https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?comment=50395#post-id-50395What do you mean by "plot" a differential equation? Do you mean plot the solution? Plot a vector field?Fri, 27 Mar 2020 22:36:40 +0100https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?comment=50395#post-id-50395Comment by John Palmieri for <p>I need to plot two differential equations on the same set of axes
(with a legend showing which is which).</p>
<p>My domain has to be 0 ≤ t ≤ 100.</p>
<p>Listed below is my code, any help is greatly appreciated.</p>
<pre><code>t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = sol[0].rhs()
g(t) = sol[1].rhs()
</code></pre>
https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?comment=50396#post-id-50396Does http://doc.sagemath.org/html/en/prep/Symbolics-and-Basic-Plotting.html#basic-2d-plotting help?Fri, 27 Mar 2020 22:39:35 +0100https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?comment=50396#post-id-50396Comment by PolarizedIce for <p>I need to plot two differential equations on the same set of axes
(with a legend showing which is which).</p>
<p>My domain has to be 0 ≤ t ≤ 100.</p>
<p>Listed below is my code, any help is greatly appreciated.</p>
<pre><code>t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = sol[0].rhs()
g(t) = sol[1].rhs()
</code></pre>
https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?comment=50398#post-id-50398Yes, sorry for the lack of clarity, but I'm meaning to plot the solutions of de1 and de2 at the same time. Thank you for the link.Sat, 28 Mar 2020 02:56:46 +0100https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?comment=50398#post-id-50398Answer by Juanjo for <p>I need to plot two differential equations on the same set of axes
(with a legend showing which is which).</p>
<p>My domain has to be 0 ≤ t ≤ 100.</p>
<p>Listed below is my code, any help is greatly appreciated.</p>
<pre><code>t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = sol[0].rhs()
g(t) = sol[1].rhs()
</code></pre>
https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?answer=50399#post-id-50399The code below completes your code, as well as that by @dsejas, adding a label to the plot. It also shows an alternative way of extracting the solutions $x(t)$ and $y(t)$ in order to define $f(t)$ and $g(t)$. Concerning the legend, I have deliberately used many customization options, just for the purpose of illustrating them. For a complete list of options, first evaluate the code, then type `p.set_legend_options?`.
t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = x(t).subs(sol)
g(t) = y(t).subs(sol)
p = plot(f, (t, 0, 10), color='red', legend_label="de1")
p += plot(g, (t, 0, 10), color='blue', legend_label="de2")
legend_op = dict(loc=1, handlelength=3, handletextpad=0.5,
labelspacing=0.6, borderpad=0.8, title="Solutions",
back_color=(0.85,0.95,0.85), fancybox=True,
font_family="serif", font_style="italic")
p.set_legend_options(**legend_op)
p.show(frame=True, axes=False)
This is the result:
![image description](/upfiles/15853666606166231.png)Sat, 28 Mar 2020 04:38:19 +0100https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?answer=50399#post-id-50399Answer by dsejas for <p>I need to plot two differential equations on the same set of axes
(with a legend showing which is which).</p>
<p>My domain has to be 0 ≤ t ≤ 100.</p>
<p>Listed below is my code, any help is greatly appreciated.</p>
<pre><code>t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = sol[0].rhs()
g(t) = sol[1].rhs()
</code></pre>
https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?answer=50397#post-id-50397Hello, @PolarizedIce! If what you mean by "plot a differential equation" is "plot it's solution", your code is really close. You can do the following:
t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = sol[0].rhs()
g(t) = sol[1].rhs()
p = plot(f, (t, 0, 100), color='red')
p += plot(g, (t, 0, 100), color='blue')
p.show()
Here, I used the `color` option just to make clear which function is which. You can also plot both functions at the same time:
plot([f,g], (t,0,100))
As an alternative, in this particular case, you can also use the `desolve_system_rk4` function:
t,x,y = var('t x y')
de1 = -3/20*x + 1/12*y
de2 = 3/20*x-3/20*y
sol = desolve_system_rk4([de1,de2], [x,y], ivar=t, ics=[0,4,0], end_points=100)
Notice that the differential equations must be of the form $\frac{dx}{dt}=F(x,t)$, so you use only the right-hand-side of your equations (see `de1` and `de2` in the previous code). Once you have specified the rhs of the equations, you specify the dependent variables, the independent variable, the initial condition, and finally, you introduce the final point of integration (I chose 10 in this case). You can also specify a step of integration with the `step` option (the default is `step=0.1`) . I must point out that `desolve_system_rk4` solves the system numerically, as opposed to `desolve_system`, that solves symbolically.
In order to plot the numerical solution, you can do the following:
f = [(i,j) for i,j,k in sol]
g = [(i,k) for i,j,k in sol]
p = line(f, color='red')
p += line(g, color='blue')
p.show()
Whichever method you use, this should be the result:
![image description](/upfiles/15854138279430856.png)
On the other hand, you can also plot the phase portrait. If you used the `desolve_system`, you should do
fp = [(f(t),g(t)) for t in srange(0,100,0.1)]
line(fp)
If you used `desolve_system_rk4`, then you should do
fp = [(j,k) for i,j,k in sol]
line(fp)
Whatever method you used, the result should be like this:
![image description](/upfiles/15853540211198081.png)
I hope this helps!Sat, 28 Mar 2020 01:07:14 +0100https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?answer=50397#post-id-50397Comment by dsejas for <p>Hello, <a href="/users/27440/polarizedice/">@PolarizedIce</a>! If what you mean by "plot a differential equation" is "plot it's solution", your code is really close. You can do the following:</p>
<pre><code>t = var('t')
x = function('x')(t)
y = function('y')(t)
de1 = diff(x,t) == -3/20*x + 1/12*y
de2 = diff(y,t) == 3/20*x-3/20*y
sol = desolve_system([de1,de2],[x,y],ics=[0,4,0])
f(t) = sol[0].rhs()
g(t) = sol[1].rhs()
p = plot(f, (t, 0, 100), color='red')
p += plot(g, (t, 0, 100), color='blue')
p.show()
</code></pre>
<p>Here, I used the <code>color</code> option just to make clear which function is which. You can also plot both functions at the same time:</p>
<pre><code>plot([f,g], (t,0,100))
</code></pre>
<p>As an alternative, in this particular case, you can also use the <code>desolve_system_rk4</code> function:</p>
<pre><code>t,x,y = var('t x y')
de1 = -3/20*x + 1/12*y
de2 = 3/20*x-3/20*y
sol = desolve_system_rk4([de1,de2], [x,y], ivar=t, ics=[0,4,0], end_points=100)
</code></pre>
<p>Notice that the differential equations must be of the form $\frac{dx}{dt}=F(x,t)$, so you use only the right-hand-side of your equations (see <code>de1</code> and <code>de2</code> in the previous code). Once you have specified the rhs of the equations, you specify the dependent variables, the independent variable, the initial condition, and finally, you introduce the final point of integration (I chose 10 in this case). You can also specify a step of integration with the <code>step</code> option (the default is <code>step=0.1</code>) . I must point out that <code>desolve_system_rk4</code> solves the system numerically, as opposed to <code>desolve_system</code>, that solves symbolically.</p>
<p>In order to plot the numerical solution, you can do the following:</p>
<pre><code>f = [(i,j) for i,j,k in sol]
g = [(i,k) for i,j,k in sol]
p = line(f, color='red')
p += line(g, color='blue')
p.show()
</code></pre>
<p>Whichever method you use, this should be the result:
<img alt="image description" src="/upfiles/15854138279430856.png"></p>
<p>On the other hand, you can also plot the phase portrait. If you used the <code>desolve_system</code>, you should do</p>
<pre><code>fp = [(f(t),g(t)) for t in srange(0,100,0.1)]
line(fp)
</code></pre>
<p>If you used <code>desolve_system_rk4</code>, then you should do</p>
<pre><code>fp = [(j,k) for i,j,k in sol]
line(fp)
</code></pre>
<p>Whatever method you used, the result should be like this:
<img alt="image description" src="/upfiles/15853540211198081.png"></p>
<p>I hope this helps!</p>
https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?comment=50403#post-id-50403Hello, @PolarizedIce! I just noticed that you wanted your domain to be $0\le t\le 100$. My previous answer worked one example in $0\le t\le 10$. So I have updated my answer to work with your required domain. The only difference is that I changed `end_points=10` to `end_points=100` in the command `desolve_system_rk4`, and the first figure has changed in consequence; everything else remains the same.Sat, 28 Mar 2020 17:50:13 +0100https://ask.sagemath.org/question/50394/plot-solutions-of-two-differential-equations-on-same-axes-with-legend/?comment=50403#post-id-50403