I am trying to generate a list of x
-values for a function using a module, where my x
-value must increase by 'a' if the term number is even, and by 'y' if it is odd. However, x[1]
is 0
.
For example: $x_1 = 0, x_2 = a, x_3 = a+y, x_4 = 2a+y, x_5 = 2a+2y...$you get the point.
I am attempting to use a recurrence relation within an If-clause to generate values of $x_n$. However, my code does not work.
My code is:
XValues[counter_] :=
Module[{a, y, xvalues, x},
x[1] -> 0;
xvalues =
Table[x[j] -> If[OddQ[j] == True , x[j - 1] + y, x[j - 1] + a], {j, 2, counter}];
Flatten@{x[1] -> 0, xvalues}
]
However, if I evaluate the module for a certain number, I get the following:
{ x$10387[1] -> 0,
x$10387[2] -> a$10387 + x$10387[1],
x$10387[3] -> y$10387 + x$10387[2],
x$10387[4] -> a$10387 + x$10387[3],
x$10387[5] -> y$10387 + x$10387[4]}
I think it is probably to do with protected variables or something along those lines, but I am not sure.
Any ideas would be gratefully received!
Thanks.
---------------------EDIT---------------------
Thanks for all the answers!
@Teake Nutma I have used your algorithm in a module to generate the values as it seems most appropriate to what I want to do with the x
values.
XValues[counter_] :=
Module[{xvalues},
xsubstitute[1] = 0;
xsubstitute[i_Integer?EvenQ] :=
xsubstitute[i] = xsubstitute[i - 1] + a;
xsubstitute[i_Integer?OddQ] :=
xsubstitute[i] = xsubstitute[i - 1] + y;
xvalues = Table[xsubstitute[i], {i, 1, counter}]
]
I now want to replace the x
value in the Phi
functions of the equations generated below with the x
value generated through the XValues module (so a different value of x
for each 'Eq' generated). This is my attempt so far but it does not appear to work. Any ideas?
simeqs = Table[Eq[j] -> Phi[j] == Phi[j + 1] /. x -> xsubstitute[j]/. eqs , {j, 2*number}];
a
,x
andy
. What do You want to get, if list of values why are You using->
? $\endgroup$f[n_] := Plus @@ (Through[{Floor, Ceiling}[(n - 1)/2]] {y, a})
or similar $\endgroup$