# Subscript to replaced parameters

I'm trying to manipulate numerical scheme's indexes: $$T=D-N \\ \frac{T_i^{n+1}-T_i^n}{\Delta t}=\frac{D_i^{n+1}-D_i^n-(N_i^{n+1}-N_i^n)}{\Delta t}$$

But I don't know how to do the transition on the second line with the replace option:

EQ1=EQ//.T-> Dd-Nn


Apparently, Subscript[x,n] can not apply the index n when its first argument is a summation. The input:

Subscript[x+y,n]


Output:

x+y_n


So how can I do it without manually changing term by term, and applying the replace option?

• Don't use subscripts. Also don't use capital D or capital N as they are reserved. Write $D_i^{n}$ as d[i,n] instead and $N_i^{n+1}$ as m[i,n+1]. – flinty Apr 10 at 10:54
• @flinty My mistake, I forgot to mention that I use Dd and Nd as D, N are reserved. Your suggestion is a manual substitution, I'm looking for something more elegant. – Ymir Apr 10 at 14:47

## 2 Answers

You may operate as follows. Let us introduce two rules:

rule1 = {Dd -> Dd[i, n + 1] - Dd[i, n],
Nn -> Nn[i, n + 1] - Nn[i, n]};
rule2 = {Dd -> Subscript[Dd, i, n + 1] - Subscript[Dd, i, n],
Nn -> Subscript[Nn, i, n + 1] - Subscript[Nn, i, n]};


Now if T is defined as follows:

Clear[T];
T = Dd - Nn;


the application of the first rule gives you:

T /. rule1

(*  -Dd[i, n] + Dd[i, 1 + n] + Nn[i, n] - Nn[i, 1 + n]   *)


while the second one,

T /. rule2


yields In principle, you may work with subscripts if you like them very much and are very careful. However, some important functions, such as Simplify may sometimes work differently from their expected action. Besides, it will be difficult to clear the variables with subscripts—the latter prone to errors that are hard to notice. For these reasons, it is generally recommended to avoid the subscripts if possible.

Have fun!

• Thanks, but it is not what I was looking for, I was looking for a generalization. I posted the way I found most convenient. Again, thanks anyways! – Ymir Apr 15 at 14:36

The solution is to use the Rule option a bit differently:

EQ1 = EQ //. Subscript[T, i_] -> Subscript[Dd, i] - Subscript[Nn, i]