# Using the method of undetermined coefficients

I am having trouble figuring out how to find a particular solution to a differential equation using the method of undetermined coefficients. Everything I've found from other sites hasn't worked.

Example equation

y''[x] - 5y'[x] + 6y[x] = xe^x

• method of Undetermined Coefficients requires a lookup to match the right hand side with guess particular solution since only limited RHS function will meet this. Only polynomials, trig and exponentials and constants. So hard to program. The first step is really only the hard one to program. Need patterns matching and can be tricky. Use method of variation of parameters. This requires no guess and works easily with computers and can be automated. – Nasser Nov 7 '17 at 1:23

This finds the particular solution using variation of parameters.

(*returns homogeneous and particular solutioins*)

solH      = (y[x]/.First@DSolve[odeH==0,y[x],x]);
{y1,y2}   = solH/.C[1] y1_ +C[2] y2_:> {y1,y2}; (*basis solutions*)

wronskian = Det[{{y1,y2},{D[y1,x],D[y2,x]}}];

{solH, Simplify[y1 u1+y2 u2]}
];


To use like this

Example 1

odeH=y''[x]-5y'[x]+6y[x];
rhs=x Exp[x];
{yh,yp}=hAndp[odeH,rhs,y,x]


fullSolution=yh+yp


Verify

 y[x]/.First@DSolve[odeH==rhs,y[x],x]


Example 2

odeH=3 y''[x]-5y'[x]+6y[x];
rhs=x Sin[x];
{yh,yp}=hAndp[odeH,rhs,y,x]


fullSolution=yh+yp


Verify

 mSolution = Simplify[y[x]/.First@DSolve[odeH==rhs,y[x],x]]


 Simplify[mSolution -fullSolution]


QED

• Style comment: A function that claims to produce "pAndH" -- particular and homogeneous solutions -- in reversed order "{yh,yp}" is likely to lead to much confusion. – Eric Towers Nov 7 '17 at 6:53