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.Wed, 25 Mar 2015 02:18:43 +0100Solving differential equations with initial value starting not at 0https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/ Hello,
I'm trying sage instead of Mathematica and for I have a lot of problem, but first what is realy frustrating is solving diff eq. with initial value. For example, my equation is:
t*diff(y,t) + y == t*exp(t^2), and y(2) = 1
The problem is that every tutorial/documentation I googled, there is something like:
t = var('t')
y = function('y',t)
desolve(t*diff(y,t) + y == t*exp(t^2),y, ics=[1])
but it sets y(0) = 1, and I want to y(2) = 1.
I don't know if it's me googling poorly or something with sage doc, but plz, help me solve this.
Ok, I think I've got the solution:
t = var('t')
y = function('y',t)
de = lambda y: t*diff(y,t) + y - t*exp(t^2)
desolve(de(x),[x,t],[1,2])
Tue, 24 Mar 2015 14:14:01 +0100https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/Answer by kcrisman for <p>Hello,</p>
<p>I'm trying sage instead of Mathematica and for I have a lot of problem, but first what is realy frustrating is solving diff eq. with initial value. For example, my equation is:</p>
<pre><code>t*diff(y,t) + y == t*exp(t^2), and y(2) = 1
</code></pre>
<p>The problem is that every tutorial/documentation I googled, there is something like:</p>
<pre><code>t = var('t')
y = function('y',t)
desolve(t*diff(y,t) + y == t*exp(t^2),y, ics=[1])
but it sets y(0) = 1, and I want to y(2) = 1.
</code></pre>
<p>I don't know if it's me googling poorly or something with sage doc, but plz, help me solve this.</p>
<p>Ok, I think I've got the solution:</p>
<pre><code>t = var('t')
y = function('y',t)
de = lambda y: t*diff(y,t) + y - t*exp(t^2)
desolve(de(x),[x,t],[1,2])
</code></pre>
https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/?answer=26308#post-id-26308The examples should instruct you to give the `x` and `y` variable initial conditions, like in this example [straight from the documentation](http://www.sagemath.org/doc/reference/calculus/sage/calculus/desolvers.html#sage.calculus.desolvers.desolve):
sage: f = desolve(diff(y,x) + y - 1, y, ics=[10,2]); f
(e^10 + e^x)*e^(-x)
To be precise,
sage: t = var('t')
sage: y = function('y',t)
sage: de = t*diff(y,t) + y - t*exp(t^2)
sage: sol = desolve(de,y,[2,1])
sage: sol
-1/2*(e^4 - e^(t^2) - 4)/t
sage: sl = (t*exp(t^2)-x)/t
sage: plot_slope_field(sl,(t,2,2.1),(x,1,10))+plot(sol,(t,2,2.1))
where the slope field coincides very nicely with the solution.
Tue, 24 Mar 2015 14:50:11 +0100https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/?answer=26308#post-id-26308Comment by Photon for <p>The examples should instruct you to give the <code>x</code> and <code>y</code> variable initial conditions, like in this example <a href="http://www.sagemath.org/doc/reference/calculus/sage/calculus/desolvers.html#sage.calculus.desolvers.desolve">straight from the documentation</a>:</p>
<pre><code>sage: f = desolve(diff(y,x) + y - 1, y, ics=[10,2]); f
(e^10 + e^x)*e^(-x)
</code></pre>
<p>To be precise,</p>
<pre><code>sage: t = var('t')
sage: y = function('y',t)
sage: de = t*diff(y,t) + y - t*exp(t^2)
sage: sol = desolve(de,y,[2,1])
sage: sol
-1/2*(e^4 - e^(t^2) - 4)/t
sage: sl = (t*exp(t^2)-x)/t
sage: plot_slope_field(sl,(t,2,2.1),(x,1,10))+plot(sol,(t,2,2.1))
</code></pre>
<p>where the slope field coincides very nicely with the solution.</p>
https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/?comment=26311#post-id-26311I think you did not read what I've wrote. Please read again my problem and then answer if you would like.Tue, 24 Mar 2015 18:17:44 +0100https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/?comment=26311#post-id-26311Comment by kcrisman for <p>The examples should instruct you to give the <code>x</code> and <code>y</code> variable initial conditions, like in this example <a href="http://www.sagemath.org/doc/reference/calculus/sage/calculus/desolvers.html#sage.calculus.desolvers.desolve">straight from the documentation</a>:</p>
<pre><code>sage: f = desolve(diff(y,x) + y - 1, y, ics=[10,2]); f
(e^10 + e^x)*e^(-x)
</code></pre>
<p>To be precise,</p>
<pre><code>sage: t = var('t')
sage: y = function('y',t)
sage: de = t*diff(y,t) + y - t*exp(t^2)
sage: sol = desolve(de,y,[2,1])
sage: sol
-1/2*(e^4 - e^(t^2) - 4)/t
sage: sl = (t*exp(t^2)-x)/t
sage: plot_slope_field(sl,(t,2,2.1),(x,1,10))+plot(sol,(t,2,2.1))
</code></pre>
<p>where the slope field coincides very nicely with the solution.</p>
https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/?comment=26321#post-id-26321I'm not sure why you think I didn't read it. You only specified one variable for your initial condition, and you need two of them - in your case, they are called `x` and `t`, in the one in the doc it's `y` and `x`, but no matter. In such a case, you are supposed to use two numbers in a list like `[1,2]`, as you use in your updated answer. Which is great! I just don't see why the answer I provided seemed irrelevant to you.Wed, 25 Mar 2015 02:14:17 +0100https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/?comment=26321#post-id-26321Comment by kcrisman for <p>The examples should instruct you to give the <code>x</code> and <code>y</code> variable initial conditions, like in this example <a href="http://www.sagemath.org/doc/reference/calculus/sage/calculus/desolvers.html#sage.calculus.desolvers.desolve">straight from the documentation</a>:</p>
<pre><code>sage: f = desolve(diff(y,x) + y - 1, y, ics=[10,2]); f
(e^10 + e^x)*e^(-x)
</code></pre>
<p>To be precise,</p>
<pre><code>sage: t = var('t')
sage: y = function('y',t)
sage: de = t*diff(y,t) + y - t*exp(t^2)
sage: sol = desolve(de,y,[2,1])
sage: sol
-1/2*(e^4 - e^(t^2) - 4)/t
sage: sl = (t*exp(t^2)-x)/t
sage: plot_slope_field(sl,(t,2,2.1),(x,1,10))+plot(sol,(t,2,2.1))
</code></pre>
<p>where the slope field coincides very nicely with the solution.</p>
https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/?comment=26322#post-id-26322Note also that `de(x)` in your update will yield `-t*e^(t^2) + x` which I'm not sure is what you're going for. In principle, using `[x,t]` shouldn't even work in the `dvar` spot as you did, so that might even be a bug - except it isn't, because what you typed will give a `NotImplementedError` under normal circumstances.Wed, 25 Mar 2015 02:18:43 +0100https://ask.sagemath.org/question/26307/solving-differential-equations-with-initial-value-starting-not-at-0/?comment=26322#post-id-26322