Ask Your Question

Solving differential equations with initial value starting not at 0

asked 2015-03-24 14:14:01 +0200

Photon gravatar image

updated 2015-03-24 21:33:20 +0200


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)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-03-24 14:50:11 +0200

kcrisman gravatar image

updated 2015-03-25 02:26:33 +0200

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.

edit flag offensive delete link more


I think you did not read what I've wrote. Please read again my problem and then answer if you would like.

Photon gravatar imagePhoton ( 2015-03-24 18:17:44 +0200 )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.

kcrisman gravatar imagekcrisman ( 2015-03-25 02:14:17 +0200 )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.

kcrisman gravatar imagekcrisman ( 2015-03-25 02:18:43 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2015-03-24 14:14:01 +0200

Seen: 1,139 times

Last updated: Mar 25 '15