# How to code this equation to find y?

1 view (last 30 days)
Huy Nguyen on 17 Dec 2021
Edited: John D'Errico on 17 Dec 2021
y'' + 2y' + 5y = 1 + x * e^x
John D'Errico on 17 Dec 2021
syms y(x)
ode = diff(y,x,2)+2*diff(y,x)+5*y == 1+x*exp(x)
ySol(x) = dsolve(ode)
my result that i solved is:
exp(-x)*(C1*cos(2x)+C2*sin(2x))+exp(x)*(1/8 * x - 1/16) +1/5.
but the result in matlab is:
sin(2*x)*(cos(2*x)/10 + sin(2*x)/5 - (sin(2*x)*exp(x))/16 + (x*cos(2*x)*exp(x))/8 + (x*sin(2*x)*exp(x))/8) - cos(2*x)*(sin(2*x)/10 - cos(2*x)/5 + (cos(2*x)*exp(x))/16 - (x*cos(2*x)*exp(x))/8 + (x*sin(2*x)*exp(x))/8) + C1*cos(2*x)*exp(-x) - C2*sin(2*x)*exp(-x)

John D'Errico on 17 Dec 2021
Edited: John D'Errico on 17 Dec 2021
Is the result that MATLAB provided a valid result?
syms y(x)
ode = diff(y,x,2)+2*diff(y,x)+5*y == 1+x*exp(x)
ode(x) = ySol(x) = dsolve(ode)
ySol(x) = Since you provided no initial conditions, this is a valid solution. It may not look as simple as the one that you got, but it is still a solution. Is the solution you found also valid, and a complete solution? Probably, though this would need to be confirmed. Often when there are undetermined constants, two solutions may look very different, yet are equally valid. And with trig functions, there are many trig identities that can simplify or expand a given solution. But computer symbolic tools do not care that what you found LOOKs different, nor do they remotely care about what looks elegant to you.
So to test your claim, that you have a VALID solution to the ODE, I'll define the solution that you claim to be a solution. yAlt is EXACTLY what you wrote. Then I substituted it into the ODE, and did a simplify. If it is true that your solution is valid, then I will get zero.
syms C1 C2
yAlt = exp(-x)*(C1*cos(2*x)+C2*sin(2*x))+exp(x)*(1/8 * x - 1/16) +1/5;
simplify(diff(yAlt,x,2)+2*diff(yAlt,x)+5*yAlt - 1 - x*exp(x))
ans =
0
That I get zero out here suggests the solution you found may be equivalent to the one returned by dsolve.
Is it a complete solution? That is does it include all possible solutions? There are two undetermined coefficients, C1, and C2. So it has two degrees of freedom, as should a 2nd order ODE. So my guess is this is a complete solution, just a less complex looking one than was found by dsolve. Again, dsolve gives not a hoot about elegance.
Just looking at the solution found by dsolve, we see terms like this: sin(2*x)*cos(2*x). And of course, we know a trig identity to reduce that into sin(4*x)/2. I also notice some duplicate terms with opposite signs that did not get removed. So what happens when we apply simplify to ySol?
simplify(ySol)
ans(x) = That looks a bit like yours, though I have not checked too carefully. It looks like dsolve has chosen the opposite sign on C2, but other than that, they seem the same. Again, two solutions may be different because of subtle differences in the undetermined constants. Here they just had the opposite sign applied.
The point is, try using simplify or expand on complicated expressions. You might sometimes be surprised.