Search code examples
matlabsymbolic-mathodecoefficients

Equating symbolic coefficients


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.


Solution

  • 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