MATLAB Answers

How to Solve a Second-Order Differential Equation?

50 views (last 30 days)
Filip Bielecki
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)';
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?

Accepted Answer

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 = 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;
sqrt(2) C7 - sqrt(pi) sqrt(2) C7 + pi sqrt(2) C8
--------------------- exp(x) + -------------------------- exp(-x)
2 sqrt(pi) 2 sqrt(pi)
exp(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?

More Answers (1)

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

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!