Substitution in Mathematica

  • #1
Hi dear All
Sorry for a stupid question, but I am really newbe in M.
I have some function, for example D[y[r],r]. Now I want to substitute y[r] to Sin[r] and to ask Mathematicato make all the calculations. (i.e I want to get Cos[x])
If I make smth like D[y[r],r]/.y[r]->Sin[r] or D[y[r], r] /. y -> Sin[r] it doesn't work.
How do to it? Thank you very much
(In the real life I have a set of Einstein equations, that I solve for some arbitrary function of r, θ,χ and then try to subs exact form).
  • #2
That isn't as stupid a question as you think. Mathematica goes to considerable trouble to make itself look like "an ordinary programming language" while it is actually doing things behind the scenes that are very different from what it appears to be doing.

After you learn the first few levels of Mathematica spells you will probably be able to use the evaluation mechanism to accomplish typical simple things.

But then there are many many deeper levels in this game. In some of these you are trying to sidestep the "usual" behavior to accomplish what you want.

FullForm will sometimes be very helpful when you are trying to understand why substitutions are not working.

FullForm[D[y[r], r]] shows you Derivative[1][y][r]. Notice there is no y[r] in that. This is probably one of several reasons that your substitution isn't working.

This works
D[y[r], r] /. Derivative[1][y][r] -> Derivative[1][Sin][r]

There are other ways, perhaps even simpler ways, and with Mathematica there are always other completely mind boggling ways, to do what you want. But perhaps at your level this might be enough to get you started.
  • #3
What Bill said is all completely correct! I'd just like to add that if you want to replace y with Sin, then that is exactly what you should do:


For more complicated substitutions, you can use a pure function notation, e.g.

  • #4
Thank you very much. It seems that its what I am looking for. And If its not hard
1) Why its not .y->(Sin[#]+Cos[#])&
2) If I have smth like
how to tell him make the differentiation AFTER substitution.
Last edited:
  • #5
1) Because of the binding of operators. If you put the ampersand outside of the bracket, then it binds to the whole Rule as opposed to just the right hand side.
To see how terms are grouped, you can either select and then repeatedly click on the term of interest or use the keyboard shortcut Ctrl-.

2) The most straightforward way is to put the replacement rule inside the D:
D[t^2 /. t -> Sin[r], r]

Or use t[r]:
D[t[r]^2, r] /. t -> Sin

Or you could wrap the D[] with Hold[], then release the hold:
Hold[D[t^2, r]] /. t -> Sin[r] // ReleaseHold

Alternatively, you could use With[] to inject the rule into your code:
With[{t = Sin[r]}, D[t^2, r]]

Or you could use the total derivative operator Dt, which assumes implicit dependence (unless told otherwise):
Dt[t^2, r] /. t -> Sin[r]

Or you could tell D[] that t is not a constant
D[t^2, r, NonConstants -> {t}] /. t -> Sin[r]
(but that doesn't quite behave like I want it to...
the replacement rule acts on the option, even if you use SetOptions[] instead of an explicit option.)
  • #6
Thank you very much!
  • #7
Hi Helpful peeps. So I'm posting in this thread because I think my problem falls under substitution.

I'm new to mathematica and I'm finding it difficult to find the code that I want to use.

So I'm busy with the secular equation of the double pendullum. What I have:

c[1, \[Theta]1_, \[Theta]2_] := Subscript[l, 1]*Cos[\[Theta]1]
c[2, \[Theta]1_, \[Theta]2_] := Subscript[l, 1]*Sin[\[Theta]1]
c[3, \[Theta]1_, \[Theta]2_] :=
Subscript[l, 1]*Cos[\[Theta]1] + Subscript[l, 2]*Cos[\[Theta]2]
c[4, \[Theta]1_, \[Theta]2_] :=
Subscript[l, 1]*Sin[\[Theta]1] + Subscript[l, 2]*Sin[\[Theta]2]

\[Theta][\[Alpha]_] := \[Theta]1 /; \[Alpha] == 1
\[Theta][\[Alpha]_] := \[Theta]2 /; \[Alpha] == 2

m[k_] := Subscript[m, 1] /; k == 1 || k == 2
m[k_] := Subscript[m, 2] /; k == 3 || k == 4

a[\[Alpha]_, \[Beta]_, \[Theta]1_, \[Theta]2_] :=
Sum[m[k]*D[c[k, \[Theta]1, \[Theta]2], \[Theta][\[Alpha]]]*
D[c[k, \[Theta]1, \[Theta]2], \[Theta][\[Beta]]], {k,
4}] //. {\[Theta]1 -> 0, \[Theta]2 -> 0}
Subscript[m, T] = {{a[1, 1, \[Theta]1, \[Theta]2],
a[1, 2, \[Theta]1, \[Theta]2] }, {a[2, 1, \[Theta]1, \[Theta]2],
a[2, 2, \[Theta]1, \[Theta]2]}}

b[\[Alpha]_, \[Beta]_, \[Theta]1_, \[Theta]2_] :=
D[D[V[\[Theta]1, \[Theta]2], \[Theta][\[Beta]]], \[Theta][\[Alpha]]] \
//. {\[Theta]1 -> 0, \[Theta]2 -> 0}
Subscript[m, V] = {{b[1, 1, \[Theta]1, \[Theta]2],
b[1, 2, \[Theta]1, \[Theta]2] }, {b[2, 1, \[Theta]1, \[Theta]2],
b[2, 2, \[Theta]1, \[Theta]2]}}

Subscript[m, s] =
Det[Subscript[m, V] - (\[Omega]^2)*Subscript[m, T]] == 0

The code pasted should be fine. Now what I want to do is make substitutions in Subscript[m, s]. I would like to substitute in things like Ω1 =Sqrt[g/l1] etc. How do I go about this? Hope my post isn't too confusing.
  • #8
Now what I want to do is make substitutions in Subscript[m, s]. I would like to substitute in things like Ω1 =Sqrt[g/l1] etc. How do I go about this?

As a bit of general advice based on a decade or two of experience using Mathematica, "drinking the Subscript Koolaid(tm)" in Mathematica is often and perhaps even usually a very bad idea.

Note for those who are not english speakers, Koolaid is a flavored sugar drink mix for children and "drinking the Koolaid" is a slang term for having lost your ability to think rationally.

Now back to Mathematica and Subscripts. I do understand that the graphical user interface and Mathematica in particular have compelled people to think they must desktop publish their math and this just has to include subscripted variables. This is sometimes possible, but often makes your task at least twice as difficult.

Mathematica goes to some length to make new users think that subscripted variables are just ordinary variables. This is simply false.

Sometimes subscripted variables will work just fine. A year or two ago I saw a page of dense code that someone had written making heavy use of subscripted variables. The code actually worked. I was stunned. I should have saved that and tried to diagnose exactly how he had accomplished this.

Much much more often there will be perplexing problems followed by posted questions asking why my code doesn't work. One of the top ten reasons is "because it uses subscripted variables." There are even notes made up by some of the Wolfram support staff to try to explain how subscripted variables behave and what to avoid. I've even seen recommendations to just save subscripts for desktop publishing your finished product.

Then you wrote "I would like to substitute in things like Ω1 =Sqrt[g/l1] etc."

I am assuming you wanted to replace all Subscript[Ω,1] with Sqrt[g/Subscript[l,1]] but I don't find any Ω1 or Subscript[Ω,1] anywhere in your code.

I compared the output of

Subscript[m, s]

with the output of

Subscript[m, s] /. {\[Omega] -> Sqrt[g/Subscript[l, 1]]}

and the replacement did seem to be what I would expect.

One thing you should watch for, pattern matching and replacement do not check that what you are doing is mathematically meaningful or correct, if the pattern matches the replacement is done. For example, outside of pattern matching Mathematica will watch for signs with squares and square roots, but with your own substitutions it will completely ignore those signs unless you account for them yourself.

Suggested for: Substitution in Mathematica