3

I would like to seek y particular of ODE y'' - y' - 2y = 4x^2

I made the following script:

syms x A0 A1 A2
ypa = A2*x^2+A1*x+A0; % y_p assume
cyp = diff(ypa,2) - diff(ypa) - 2*ypa % according to ODE
P1 = 4*x^2; P2 = cyp ; % Equating P1 and P2
C = coeffs(P1 - P2,x);
A0 = solve(C(1),A0) 
A1 = solve(C(2),A1) 
A2 = solve(C(3),A2) 

I got the correct answer for A2 = -2. But I did not get for A0 (should be -3) and A1 (should be 2). How to get them automatically?

P.S I'm using MATLAB R2013a.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
User1961
  • 131
  • 8

1 Answers1

2

Instead of calling solve 3 times, once on each equation of C, you should call it once on the entire system of equations so that the proper substitutions are done to give you a numeric result for each variable:

>> [A0, A1, A2] = solve(C)

A0 =
-3

A1 =
2

A2 =
-2
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • Thanks a lot gnovice. It works perfectly. Thanks also to Sardar Usama who explains me how to solve this problem instensively. Since I use the old version of Matlab, It does not work. – User1961 Jun 22 '17 at 02:06