# Replace constant in summation function using replace or If or /

Hi i how do i replace constant for k in this summation.

For following:

1. Even 2*n Even replace k by a
2. Even 2*n+1 Odd replace k by b

Here is the code

ue[x_, t_, k_, n_] :=
(1/2)*Sum[Subscript[k, j]*(Subscript[x, j - 1][t] - Subscript[x, j][t])^2,{j, 1, n + 1}];


Subscript[k, j_?EvenQ] = a;
Subscript[k, j_?OddQ] = b;


For extra conditions at $$j=0$$ and $$j=n+1$$ you can use a two-index form $$k_{n,j}$$:

Subscript[k, n_, 0] = 0;
Subscript[k, n_, j_] /; j == n + 1 = 0;
Subscript[k, n_, j_?EvenQ] = a;
Subscript[k, n_, j_?OddQ] = b;


Check:

With[{n = 10}, Table[Subscript[k, n, j], {j, 0, n + 1}]]


{0, b, a, b, a, b, a, b, a, b, a, 0}

• sir how do i impose extra condition beside that above that k=0 at j=0 and j=n+1; k[0] = 0; k[n + 1] = 0; Commented Mar 12, 2019 at 18:14
• ok i got it sir. thanks Commented Mar 12, 2019 at 18:20
• Subscript[k, j_?EvenQ] = a; Subscript[k, j_?OddQ] = b; "to impose extra condition on beside that above that k=0 at j=0 and j=n+1; use " for n=4; Subscript[k, 0] = 0; Subscript[k, 4] = 0; Commented Mar 12, 2019 at 19:10