MATLAB: Generated code that mulitples previous number

MATLAB

Hello I want to compute a value assuming Xo = 1
using the formular X = e * (p) . ^ ( e – 1) * X
for first X = e * (p) . ^ ( e – 1) * Xo
Second X = e * (p) . ^ ( e – 1) * X1
Third X = e * (p).^(e – 1) * X2
fourth X = e * (p). ^ (e – 1) * X3
it can be simplified
X0 = 1
X1 = e * (p) . ^ ( e – 1) * Xo
X2 = e * (p) . ^ ( e – 1) * X1
X3 = e * (p) . ^ ( e – 1) * X2
X4 = e * (p) . ^ ( e – 1) * X3
Can anyone assist me in generating this code
Thanks in advance
Regards
Tino

Best Answer

  • I specified:
    e = 1.2;
    p = 2.3;
    and then your code produces:
    X0 =
    1
    X1 =
    1.4175
    X2 =
    2.0093
    X3 =
    2.8483
    X4 =
    4.0375
    Simpler MATLAB code using power:
    >> (e*p.^(e-1)).^(0:4)
    ans =
    1 1.4175 2.0093 2.8483 4.0375
    or cumprod (as Steven Lord suggested):
    >> cumprod(e*p.^(e-1)*ones(1,4))
    ans =
    1.4175 2.0093 2.8483 4.0375