64.6.23 problem 23

Internal problem ID [13302]
Book : Differential Equations by Shepley L. Ross. Third edition. John Willey. New Delhi. 2004.
Section : Chapter 2, Miscellaneous Review. Exercises page 60
Problem number : 23
Date solved : Monday, March 31, 2025 at 07:47:41 AM
CAS classification : [_linear]

\begin{align*} \left (x +2\right ) y^{\prime }+y&=\left \{\begin {array}{cc} 2 x & 0\le x \le 2 \\ 4 & 2<x \end {array}\right . \end{align*}

With initial conditions

\begin{align*} y \left (0\right )&=4 \end{align*}

Maple. Time used: 0.067 (sec). Leaf size: 31
ode:=(x+2)*diff(y(x),x)+y(x) = piecewise(0 <= x and x <= 2,2*x,2 < x,4); 
ic:=y(0) = 4; 
dsolve([ode,ic],y(x), singsol=all);
 
\[ y = \frac {\left \{\begin {array}{cc} 8 & x <0 \\ x^{2}+8 & x <2 \\ 4+4 x & 2\le x \end {array}\right .}{x +2} \]
Mathematica. Time used: 0.077 (sec). Leaf size: 43
ode=(x+2)*D[y[x],x]+y[x]==Piecewise[{{2*x,0<=x<=2},{4,x>2}}]; 
ic={y[0]==4}; 
DSolve[{ode,ic},y[x],x,IncludeSingularSolutions->True]
 
\[ y(x)\to \begin {array}{cc} \{ & \begin {array}{cc} \frac {8}{x+2} & x\leq 0 \\ \frac {4 (x+1)}{x+2} & x>2 \\ \frac {x^2+8}{x+2} & \text {True} \\ \end {array} \\ \end {array} \]
Sympy
from sympy import * 
x = symbols("x") 
y = Function("y") 
ode = Eq((x + 2)*Derivative(y(x), x) - Piecewise((2*x, (x >= 0) & (x <= 2)), (4, x > 2)) + y(x),0) 
ics = {y(0): 4} 
dsolve(ode,func=y(x),ics=ics)