1
$\begingroup$

I want to implement a simple function to solve an exact differential equation, it's:

exact[Mx_, Ny_] := c + Integrate[g'[y], y] /. (Solve[
    D[Integrate[Mx, x] + g[y], y] == Ny, g'[y]])

Solve gives a rule: {{g'[y]-> something something}} and I want to Integrate the something something, but it just integrates g'[y] and gives as a result g[y].

Is there a way to force Mathematica to first apply the rule and then integrate?

$\endgroup$
3
  • $\begingroup$ Try Integrate[g'[y] /. First@Solve[...], y] $\endgroup$
    – MarcoB
    Commented Feb 16, 2017 at 4:39
  • $\begingroup$ Just tried it, still get the same. $\endgroup$
    – Theennus
    Commented Feb 20, 2017 at 22:36
  • $\begingroup$ I used a doble replacement rule, not very elegant, but it worked: Integrate[a[y], y] + c /. (a[y] -> g'[y] /. Solve[D[Integrate[Mx, x] + g[y], y] == Ny, g'[y]][[1]]). Still I'm hoping a better solution will arrive. $\endgroup$
    – Theennus
    Commented Feb 20, 2017 at 22:37

1 Answer 1

1
$\begingroup$

Below is a long-form of the suggestion I showed in comments, where I am afraid I was not clear. I meant to suggest that you should do two things: 1. do the replacement within Integrate; 2. apply First to the result of Solve so you get an expression, rather than a single-element list containing your expression, from the replacement:

Clear[exact]
exact[Mx_, Ny_] := 
   c + Integrate[g'[y] /. First@Solve[D[Integrate[Mx, x] + g[y], y] == Ny, g'[y]], y]

Let's try it out on something simple:

exact[2 x + 2 y, 2 x + 2 y]

(* Out: c + y^2 *)

This seems to be what you wanted. Reconstructing piece by piece:

  • Integrate[Mx, x] becomes Integrate[2 x + 2 y, x], which returns 2 (x^2/2 + x y);
  • Add g[y] to get 2 (x^2/2 + x y) + g[y];
  • Take the derivative wrt $y$ and set it equal to Ny: 2 x + g'[y] == 2 x + 2 y
  • Solving for g'[y] returns {{g'[y] -> 2 y}}; take the first part, to give {g'[y] -> 2 y}
  • Summarizing, you then apply the replacement within Integrate:

    g'[y] /. First@Solve[D[Integrate[2 x + 2 y, x] + g[y], y] == 2 x + 2 y, g'[y]]
    (* Out: 2 y *)
    
  • Finally, find the antiderivative wrt $y$, which is y^2, and add c. This confirms the result obtained with your exact function.
$\endgroup$
2
  • $\begingroup$ Thank you very much, when I tried it the first time, I was still using the replacement rule after the Integrate. This worked just perfect. $\endgroup$
    – Theennus
    Commented Feb 23, 2017 at 21:39
  • $\begingroup$ @Theennus Glad it helped! $\endgroup$
    – MarcoB
    Commented Feb 23, 2017 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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