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 2022-10-17 21:04:49 +0100

g117ch gravatar image

updated 2022-10-18 18:57:11 +0100

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''(x)=x^{2}$$

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-10-18 10:27:23 +0100

Emmanuel Charpentier gravatar image

updated 2022-10-19 11:58:52 +0100

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 ?

edit flag offensive delete link more

Comments

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

g117ch gravatar imageg117ch ( 2022-10-18 18:57:02 +0100 )edit

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 ( 2022-10-19 14:45:56 +0100 )edit

@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 ( 2022-10-19 16:36:14 +0100 )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

Stats

Asked: 2022-10-17 21:04:49 +0100

Seen: 459 times

Last updated: Oct 19 '22