Processing math: 0%

First time here? Check out the FAQ!

Ask Your Question
1

Solve an ODE in Sagemath where the the value of the function and the value of its derivative is known at different points

asked 2 years ago

g117ch gravatar image

updated 2 years ago

I am new to SageMath (migrating from Mathematica), and I am having trouble solving differential equations where the value of the function and the value of its derivatives are available at different points. As for example,

y(x)+y

where, y(1)=1 and y'(2)=1.

In SageMath, it seems that the boundary conditions needs to be given in the form [x_0,y_0,x_1,y_1] or [x_0,y_0,{y_0}'], implying that the value of the function and its derivative needs to be specified at the same point.

Any help on how to solve such equations would be highly appreciated. Thank you!

Kindly note, I am not interested in the actual solution to this problem, but in the Sage implementation of such problems.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 2 years ago

Emmanuel Charpentier gravatar image

updated 2 years ago

Your differential equation being linear first order, its solution involves one integration constant, which cannot (in general) satisfy two boundary conditions.

If you need a verification :

 sage: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:
:y = function("y")
:de = y(x)+y(x).diff(x) == x^2
:sol = desolve(de, y(x)) ; sol
:# Declare the integration constant
:Csts = [str(u) for u in sol.variables() if str(u) not in globals()]
:if len(Csts)>0 : var(Csts)
:# Check solution
:chk = bool(de.substitute_function(y, sol.function(x)))
:# Satisfy the first boundary condition:
:C1=solve(sol(x=1)==1, _C) ; C1
:# Satisfy the second boundary condition:
:C2 = solve(sol.diff(x)(x=1)==1, _C) ; C2
:
:--
((x^2 - 2*x + 2)*e^x + _C)*e^(-x)
[_C == 0]
[_C == -e]

your boundary conditions are mutually incompatible...

HTH,

EDIT : The same principle applies ; and since your DE is linear second order, two integration constants appear, which may fullfill two boundary conditions : :

sage: y=function("y")
sage: de=y(x)+y(x).diff(x,2)==x^2
sage: sol=desolve(de,y(x)) ; sol
x^2 + _K2*cos(x) + _K1*sin(x) - 2
sage: Csts = [str(u) for u in sol.variables() if str(u) not in globals()] ; Csts
['_K1', '_K2']
sage: if len(Csts)>0 : var(Csts)
(_K1, _K2)
# Solving for simultaneous boundary conditions :
sage: solve([sol(x=1)==1, sol.diff(x)(x=2)==1],(_K1, _K2))
[[_K1 == -(3*cos(1) - 2*sin(2))/(cos(2)*cos(1) + sin(2)*sin(1)), _K2 == (2*cos(2) + 3*sin(1))/(cos(2)*cos(1) + sin(2)*sin(1))]]

Note : Now, your question strongly smells of an (initialy mistyped) homework. Are you sure that, by taking the ask.sagemath.org shortcut, you aren't missing the point of said homework ?

Preview: (hide)
link

Comments

Sorry, I made a mistake in writing the ODE. Can you kindly answer my query now?

g117ch gravatar imageg117ch ( 2 years ago )

This is not a homework. I am a research scholar who is trying to make the shift from Mathematica to Sagemath. Therefore, I asked this quesion. Thank you for your help.

g117ch gravatar imageg117ch ( 2 years ago )

@g117ch: If you already know Mathematica, you may benefit from installing the (gratis but not free) Wolfram engine and use it with Sage's mathematica interface. Notwithstanding the limits of this interface (including translations from one system to the other), it complements nicely Sage...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2 years ago )

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2 years ago

Seen: 509 times

Last updated: Oct 19 '22