# 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])

edit retag close merge delete

Sort by » oldest newest most voted

The examples should instruct you to give the x and y variable initial conditions, like in this example straight from the documentation:

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.

more

( 2015-03-24 12:17:44 -0600 )edit

I'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.

( 2015-03-24 20:14:17 -0600 )edit

Note 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.

( 2015-03-24 20:18:43 -0600 )edit