4
$\begingroup$

I have a small issue in understanding the workflow of Replace /. When I give this input:

(y''[x]+y[x]) /. y[x] -> 1

I am getting just 1+y''[x]. I can understand this but is there some special format so that it takes the constant value and gives the derivative as zero. For arbitrary function is there some special way of defining it?

Thank You for your help.

$\endgroup$
2
  • 2
    $\begingroup$ y[x] -> 1 only says to replace anything that looks like y[x], and y''[x] doesn't look like it at all (check with InputForm[] or FullForm[] to see). Try (y''[x] + y[x]) /. y -> (1 &). $\endgroup$ Commented May 26, 2020 at 14:49
  • $\begingroup$ Thank you so much for clearing this. I used this method in my main problem and it works. $\endgroup$
    – Syed Naqvi
    Commented May 26, 2020 at 15:13

1 Answer 1

7
$\begingroup$

You can do as JM suggested

 (y''[x]+y[x]) /. y -> (1 &)

Or make it a little more explicit, which might make it more clear

  (y''[x] + y[x]) /. y -> Function[{x}, 1]

Both cases return 1

For example, if you want y=x^2 then

  (y''[x] + y[x]) /. y -> Function[{x}, x^2]

gives

   2 + x^2
$\endgroup$
2
  • $\begingroup$ Thank you, this makes my problem much simpler. I had to substitute a solution to a differential equation and check it. Your and @JM method both work. $\endgroup$
    – Syed Naqvi
    Commented May 26, 2020 at 15:16
  • 1
    $\begingroup$ @SyedNaqvi - when solving the differential equation, instead of using DSolve[eqn, y[x], x] which returns {{y[x] -> ...}}, use DSolve[eqn, y, x] which will return {{y -> Function[{x}, ...}}. $\endgroup$
    – Bob Hanlon
    Commented May 26, 2020 at 15:36

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.