# Solving One Equation with Two Variables

I am trying to solve an equation with two variables. It is the last step in the process of using the method of undetermined coefficients to solve a nonhomogeneous differential equation. The equation is this:

$$y''-5y'+6y=e^tcos(2t)+e^{2t}(3t+4)sin(t)$$

My first particular equation is

$$Y(t)=e^t(Acos(2t)+Bsin(2t))$$

and I need to find A and B values that satisfy:

$$y''-5y'+6y=e^tcos(2t)$$

Plugging $Y(t)$ into the equation gives:

$$-2 e^t ((A+3B) cos(2 t)-(3 A + B) sin(2 t))=e^tcos(2t)$$

So A and B must satisfy:

$$A+3B=-1/2$$ $$B-3A=0$$

And then A and B are easy to solve for.

However, putting the equation into Solve[] gives an error:

Solve[{-2 Exp[t] ((a + 3 b) Cos[2 t] + (-3 a + b) Sin[2 t]) == Exp[t] Cos[2 t]}, {a, b}]


Solve::svars: Equations may not give solutions for all "solve" variables. >>

And the output just gives B in terms of A and t. This particular problem I can solve on paper, but others are much more complex. I need a way to solve an equation like this using Mathematica to save time. This is homework from Boyce Elementary Differential Equations 10, Section 3.5 #23.

EDIT: Made variable names lower case

• Solve[{-2 Exp[t] ((n + 3 m) Cos[2 t] + (-3 n + m) Sin[2 t]) == Exp[t] Cos[2 t]}, {n, m}] which gives me {{n -> -0.05, m -> -0.15}}. You probably assigned something different to A and B, so you need to clear them. Also, it is not a good practice to use upper-case letters as vars, functions, etc as they are built-in symbols. – Sektor Jun 11 '14 at 22:03
• I copy/pasted your input into a freshly re-booted kernel and got the same error. What might I be doing wrong? – Platatat Jun 11 '14 at 23:12
• Whoa :D What is your version of Mathematica ? – Sektor Jun 11 '14 at 23:24
• 8.0 for students – Platatat Jun 11 '14 at 23:37
• Hm, that's strange. Do you have access to other versions of Mathematica ? If so please try to evaluate the input there. – Sektor Jun 11 '14 at 23:39

Here is another way to solve problems of this type: create a system of at least two independent equations for the two variables a and b. How to do this if you only have one equation to begin with?

In your case one side of the equation (Exp[t] Cos[2 t]) is independent of the unknowns, so I just evaluate that side at some specific values of t and use those as new right-hand sides to which the left-hand side must be matched. This can be solved with FindFit:

Rationalize@FindFit[Table[{t, Exp[t] Cos[2 t]},
{t, 0, 10, .1}],
-2 Exp[t] ((a + 3 b) Cos[2 t] + (-3 a + b) Sin[2 t]),
{a, b}, t]

(* ==> {a -> -(1/20), b -> -(3/20)} *)


Alternatively, you could evaluate both sides of the equation at discrete times and create a matrix equation from that. Then apply LeastSquares to solve that equation.

Alternatively to all of the above, one can also avoid having to pick the "right" t values at which the system of equations becomes linearly independent by doing the whole thing in Fourier space:

eqn = -2 Exp[t] ((a + 3 b) Cos[2 t] + (-3 a + b) Sin[2 t]) ==
Exp[t] Cos[2 t];

Solve[
Table[Map[FourierCoefficient[#, t, i] &, eqn], {i, 0, 1}],
{a, b}]

(* ==> {{a -> -(1/20), b -> -(3/20)}} *)


This is probably the cleanest approach mathematically, because the different Fourier components are guaranteed to be linearly independent. If you have more than 2 unknowns, you just increase the upper limit in Table from 1 to the required number of equations.

You want to extract coefficients of terms in Cos[t] and other functions of t and find values of {A, B} that make these terms vanish. SolveAlways can do this sometimes (works reliably when input is polynomial, say).

SolveAlways[
TrigExpand@{-2 Exp[t] ((A + 3 B) Cos[2 t] + (-3 A + B) Sin[2 t]) ==
Exp[t] Cos[2 t]}, {Sin[t], Cos[t]}]


During evaluation of In:= SolveAlways::ifun: Inverse functions are being used by SolveAlways, so some solutions may not be found; use Reduce for complete solution information. >>

(* Out= {{A -> -(1/20), B -> -(3/20)}, {t -> -∞}} *)


Similar to yet distinct from the other solutions. Substitute two values for t (not a period apart), to create two independent equations. Then you will get a unique solution.

Solve[-2 Exp[t] ((n + 3 m) Cos[2 t] + (-3 n + m) Sin[2 t]) ==
Exp[t] Cos[2 t] /. {{t -> 0}, {t -> 1}}, {n, m}]
(*
{{n -> -(1/20), m -> -(3/20)}}
*)


(Of course, the replacements {{t -> 0}, {t -> Pi/4}} yield the system in the OP's question. It gives the same answer.)

For a more rigorous approach, make your system from the equation and its derivative. Then

Solve[
{#, D[#, t]} &[
-2 Exp[2 t] ((n + 3 m) Cos[2 t] + Exp[t] (-3 n + m) Sin[2 t]) == Exp[2 t] Cos[2 t]
],
{n, m}]
(*
{{n -> -(1/20), m -> -(3/20)}}
*)


Since the determinant of the coefficients

Block[{y},
y[t_] := -2 Exp[t] ((n + 3 m) Cos[2 t] + (-3 n + m) Sin[2 t]);
Det[{Coefficient[y[t], {m, n}], Coefficient[y'[t], {m, n}]}] // Simplify
]
(*
-80 E^(2 t)
*)


is nonzero, the equations will be independent. More generally, if the determinant were zero, then the coefficients of m, n would be proportional. That in turn implies y'[t] is a constant multiple of y[t]. Hence y[t] would have to be an exponential function, which it is not in this case.

• Youse guys are too smart. – Daniel Lichtblau Jun 12 '14 at 14:42
• @DanielLichtblau Either that, or we lack any respect for mathematical rigor... – Jens Jun 12 '14 at 19:25
• @Jens My way might be likened more to mathematical rigor mortis. (As for yours, one can always show a posteriori that it was correct.) – Daniel Lichtblau Jun 12 '14 at 20:45