3
$\begingroup$

I am porting some Maple code I wrote to Mathematica. In one place, I use Maple's solve with what Maple call, identity.

The help page for this command is here

enter image description here

What is Mathematica's equivalent of the above command? I will show below 3 examples of its use. Basically what it does, is that given one equation, with functions in it that depends on $x$, it tries to solve for the unknowns in the equation by comparing coefficients.

For example, given

  eq=c1*(x^2+x)+c2*(2+2*x)+c3==-4*x^2+2*x+6 

and asking it to solve for c1,c2,c3 it should give the solution {c1 = -4, c2 = 3, c3 = 0}

And given

eq = -4*c1*Sin[2*x] + 4*c2*Cos[2*x] + 2*c3*Cos[2*x] - 
    8*c3*x*Sin[2*x] + 2*c4*Sin[2*x] + 8*c4*x*Cos[2*x] == 3*x*Cos[2*x];

and asking it to solve for c1,c2,c3,c4 it should give the solution

{c1 = 3/16, c2 = 0, c3 = 0, c4 = 3/8}

And given

eq = -3*c1*Exp[-x] + Cos[x]*(-3*c2 - c3) + Sin[x]*(-3*c3 + c2) == 
  Exp[-x] + 2*Cos[x]

and asking it to solve for c1,c2,c3 it should give the solution

{c1 = -1/3, c2 = -3/5, c3 = -1/5}

By hand, this is done by "comparing" coefficients. In computer algebra, there are specialized algorithms to do this, but I have not found which command or combination of commands in Mathematica that could do this.

Any suggestions?

Screen shot from Maple for illustration enter image description here

I am using Mathematica Version 12.3.1

$\endgroup$
9
  • 1
    $\begingroup$ Have you tried SolveAlways? $\endgroup$
    – JimB
    Jul 21 at 21:59
  • $\begingroup$ @JimB Yes, that was the first command I tried. !Mathematica graphics $\endgroup$
    – Nasser
    Jul 21 at 22:01
  • $\begingroup$ That works for your first example. But it hasn't completed yet (after a few minutes) for the other two examples. Am I just not seeing SolveAlways mentioned in your question? $\endgroup$
    – JimB
    Jul 21 at 22:04
  • $\begingroup$ @JimB I said but I have not found which command or combination of commands in Mathematica that could do this. I did not list each attempt I made. $\endgroup$
    – Nasser
    Jul 21 at 22:05
  • $\begingroup$ But it should be SolveAlways[eq, x] rather than SolveAlways[eq, {c1,c2,c3}]. $\endgroup$
    – JimB
    Jul 21 at 22:07
2
$\begingroup$

If $f(x)=g(x)$ for all $x$,then $f(0)=g(0)$ and $f'(0)=g'(0),f''(x)=g''(0)$ etc. We use this to get several equations.

expr1 = -4*c1*Sin[2*x] + 4*c2*Cos[2*x] + 2*c3*Cos[2*x] - 
  8*c3*x*Sin[2*x] + 2*c4*Sin[2*x] + 8*c4*x*Cos[2*x]; expr2 = 
 3*x*Cos[2*x];
sol = Solve[{expr1 == expr2, D[expr2, x] == D[expr1, x], 
    D[expr2, {x, 2}] == D[expr1, {x, 2}], 
    D[expr2, {x, 3}] == D[expr1, {x, 3}]} /. x -> 0, {c1, c2, c3, c4},
   Reals]
expr1 == expr2 /. sol[[1]] // Simplify

{{c1 -> 3/16, c2 -> 0, c3 -> 0, c4 -> 3/8}}

True

f = -3*c1*Exp[-x] + Cos[x]*(-3*c2 - c3) + Sin[x]*(-3*c3 + c2);
g = Exp[-x] + 2*Cos[x];
sol = Solve[{f == g, D[f, x] == D[g, x], D[f, {x, 2}] == D[g, {x, 2}]} /. x -> 0]
f == g /. sol[[1]]

{{c1 -> -(1/3), c2 -> -(3/5), c3 -> -(1/5)}}

True

$\endgroup$
10
  • $\begingroup$ Thanks. So is it enough to take only three derivatives each time, regardless of what f and g are and the number of constants $c_i$? I need to test this on more cases to see if it works on all what I have. good method. $\endgroup$
    – Nasser
    Jul 22 at 1:59
  • $\begingroup$ for $n$ constants,we need to use the order of the derivative from $0$ to $n-1$. $\endgroup$
    – cvgmt
    Jul 22 at 2:05
  • $\begingroup$ Ok, but then why in the first example you show, which has 4 constants, you did up to third derivative, but in the second example, which had 3 constants, you also did up to 3rd derivative? That is why I asked. It looks like second example only needs up to 2nd derivative, and I tried it now and it worked. $\endgroup$
    – Nasser
    Jul 22 at 2:10
  • $\begingroup$ @Nasser Sorry, it's my mistake. Only need up to second order derivative. $\endgroup$
    – cvgmt
    Jul 22 at 2:18
  • $\begingroup$ SolveAlways[ Normal@Series[expr1, {x, 0, 3}] == Normal@Series[expr2, {x, 0, 3}], x] $\endgroup$
    – cvgmt
    Jul 23 at 1:04
2
$\begingroup$

Another simple way is helping SolveAlways a little

expr1 = -4*c1*Sin[2*x] + 4*c2*Cos[2*x] + 2*c3*Cos[2*x] - 
        8*c3*x*Sin[2*x] + 2*c4*Sin[2*x] + 8*c4*x*Cos[2*x]; 
expr2 = 3*x*Cos[2*x];

SolveAlways[expr1 == expr2, {x, Sin[2 x], Cos[2 x]}]

(*   {{c3 -> 0, c2 -> 0, c1 -> 3/16, c4 -> 3/8}}   *)

f = -3*c1*Exp[-x] + Cos[x]*(-3*c2 - c3) + Sin[x]*(-3*c3 + c2);
g = Exp[-x] + 2*Cos[x];

SolveAlways[f == g /. Exp[-x] -> ee, {Sin[ x], Cos[ x], ee}]

(*   {{c1 -> -(1/3), c2 -> -(3/5), c3 -> -(1/5)}}   *)

May be in cases with equivalent Sin-Cos expressions you should TrigExpand SolveAlways[expr1 == expr2 // TrigExpand, {x, Sin[ x], Cos[ x]}]

Edit

you can also collect the Sin and Cos and both prefactors have to be zero for equations to be valid for all x.

ee = expr1 - expr2 == 0 // Collect[#, {Sin[2 x], Cos[2 x]}] &

(*   (4 c2 + 2 c3 - 3 x + 8 c4 x) Cos[2 x] + 
          (-4 c1 + 2 c4 - 8 c3 x) Sin[2 x] == 0   *)

SolveAlways[{ee[[1, 1, 1]] == 0, ee[[1, 2, 1]] == 0}, x]

(*   {{c3 -> 0, c2 -> 0, c1 -> 3/16, c4 -> 3/8}}   *)

Edit 2

Yet an other way with the same result: series expansion of Sin, Cos

SolveAlways[
  expr1 == expr2 /. {Sin[2 x] -> Series[Sin[2 x], {x, 0, 6}], 
Cos[2 x] -> Series[Cos[2 x], {x, 0, 6}]}, x]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.