# How to Solve a Second-Order Differential Equation?

50 views (last 30 days)
Filip Bielecki on 17 Apr 2021 at 10:59
Answered: Paul on 18 Apr 2021 at 1:43
I need to solve this equation in matlab: I wrote this but it gives me an answer with parameter t, which i don't know what means:
eqn2 = 'x*D2y+2*Dy-x*y=2*exp(x)';
pretty(simplify(dsolve(eqn2)))
Answear: C6*exp((t*((x^2 + 1)^(1/2) - 1))/x) - (2*exp(x))/x + C7*exp(-(t*((x^2 + 1)^(1/2) + 1))/x)
I know that the correct answear is Can you help me with matlab code?

Paul on 18 Apr 2021 at 1:43
>> syms x y(x)
eqn2 = x*diff(y,x,2) + 2*diff(y,x) - x*y == 2*exp(x);
sol = dsolve(eqn2);
sol = rewrite(sol,'exp');
sol = simplify(sol);
sol = expand(sol);
c = children(sol);
c1 = c(1);
c2 = sum(c(2:end));
[num,den]=numden(c2);
num = expand(num);
num = collect(num,exp(-x));
num = collect(num,exp(x));
num = num/coeffs(den,x);
den = den/coeffs(den,x);
num = collect(collect(num,exp(-x)),exp(x));
sol = c1 + num/den;
pretty(sol)
sqrt(2) C7 - sqrt(pi) sqrt(2) C7 + pi sqrt(2) C8
--------------------- exp(x) + -------------------------- exp(-x)
2 sqrt(pi) 2 sqrt(pi)
exp(x) + -----------------------------------------------------------------
x
Make the appropriate substitutions of C1-1/2 and C2 for the coefficients of exp(x) and exp(-x) in the second term on the right. Maybe someone else can get there quicker?

Alan Stevens on 17 Apr 2021 at 14:17
syms x y
eqn2 = 'x*D2y+2*Dy-x*y=2*exp(x)';
dsolve(eqn2,x)
Warning: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations instead.
ans = Filip Bielecki on 17 Apr 2021 at 18:38
But it's not the answer that i wrote in my question, it should be: 