# Syntax error trying to rewrite expression using rules

I want to use Mathematica to calculate derivatives of some long, complicated expressions. I have an integral with integrand $$I_*$$ which is a functional that depends on $$h(x,t)$$ and $$\phi(x,t)$$. The first thing I need to do though is to rewrite the functional in terms of $$h,\psi$$ where $$\psi = h \phi$$. I wrote the following couple lines in Mathematica:

ClearAll["Global*"]
mag[vec_] := Sqrt[vec . vec]
SubStar[I] = \[Gamma] (1 + mag[Grad[h[x, t], {x}]]^2) +
f[h[x, t], \[Phi][x, t]] +
h[x, t] g[\[Phi][x, t]] + \[Sigma]/
2 h[x, t] mag[Grad[\[Phi][x, t], {x}]]^2
\[Psi]sub = {\[Phi][x, t] -> \[Psi][x, t]/h[x, t],
\!$$\*SuperscriptBox[\(\[Phi]$$,
TagBox[
RowBox[{"(",
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t] -> D[\[Psi][x, t]/h[x, t], x],
\!$$\*SuperscriptBox[\(\[Phi]$$,
TagBox[
RowBox[{"(",
RowBox[{"2", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t] -> D[\[Psi][x, t]/h[x, t], {x, 2}]}

\!$$\*SubscriptBox[\(I$$, $$**$$]\) = SubStar[I] /. \[Psi]sub


In the first line, I define a magnitude function which calculates the magnitude of a vector. The second line is where I define the integrand I am interested in, here called $$I_*$$. I then define replacement rules for $$\phi, \phi_x, \phi_{xx}$$ as these are the derivatives that appear. The final line is where I try to create a new variable, called $$I_{**}$$, which is strictly written in terms of $$h,\psi$$. However, I am getting a syntax error that says "Incomplete expression; more input is needed. What does this mean? What is the issue?

What seems even stranger to me is the fact that if I don't bother defining $$I_*$$ or $$\psi sub$$ as variables and just try to accomplish all of this in one step, then the code executes with no problem. As in:

int = \[Gamma] (1 + mag[Grad[h[x, t], {x}]]^2) +
f[h[x, t], \[Phi][x, t]] +
h[x, t] g[\[Phi][x, t]] + \[Sigma]/
2 h[x, t] mag[Grad[\[Phi][x, t], {x}]]^2 /. {\[Phi][x,
t] -> \[Psi][x, t]/h[x, t],
\!$$\*SuperscriptBox[\(\[Phi]$$,
TagBox[
RowBox[{"(",
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t] -> D[\[Psi][x, t]/h[x, t], x],
\!$$\*SuperscriptBox[\(\[Phi]$$,
TagBox[
RowBox[{"(",
RowBox[{"2", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t] -> D[\[Psi][x, t]/h[x, t], {x,
2}]}


So what am I doing wrong in the first case? What is the actual syntax error?

• It looks like it's not possible to use two * in the subscript. You'll need to use a different name for the variable Oct 23, 2023 at 21:53
• That's the only error? How can you tell that? Oct 24, 2023 at 11:17

It is not good to use I as a variable, also with stars. By evaluating your code you can make sure that in some cases (at least) Mma understands it as the imaginary unit with star. I suggest to write I1istead of I with one star and I2 instead of that with two ones. Your code takes the form:

ClearAll["Global*"]
mag[vec_] := Sqrt[vec . vec]
I1 = γ (1 + mag[Grad[h[x, t], {x}]]^2) +
f[h[x, t], ϕ[x, t]] +
h[x, t] g[ϕ[x, t]] + σ/
2 h[x, t] mag[Grad[ϕ[x, t], {x}]]^2
ψsub = {ϕ[x, t] -> ψ[x, t]/h[x, t],

\!$$\*SuperscriptBox[\(ϕ$$,
TagBox[
RowBox[{"(",
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t] -> D[ψ[x, t]/h[x, t], x],

\!$$\*SuperscriptBox[\(ϕ$$,
TagBox[
RowBox[{"(",
RowBox[{"2", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t] -> D[ψ[x, t]/h[x, t], {x, 2}]}

I2 = I1 /. ψsub


This at least, successfully evaluates. Try it. If it is what you expected is up to you to deside.

Concerning your first question: rewrite I1 in terms of ψ and h. Try this:

I1 /. ϕ -> (ψ[#1, #2]/h[#1, #2] &) // Simplify

(*  f[h[x, t], ψ[x, t]/h[x, t]] +
g[ψ[x, t]/h[x, t]] h[x, t] + γ (1 +
\!$$\*SuperscriptBox[\(h$$,
TagBox[
RowBox[{"(",
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t]^2) + (σ (ψ[x, t]
\!$$\*SuperscriptBox[\(h$$,
TagBox[
RowBox[{"(",
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t] - h[x, t]
\!$$\*SuperscriptBox[\(ψ$$,
TagBox[
RowBox[{"(",
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[x, t])^2)/(2 h[x, t]^3) *)


I did not understand the second question. Hope this helps. Have fun!