Generated code that mulitples previous number

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

 采纳的回答

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

7 个评论

Thanks for yout answer.
I am doing this for n numbers of any size
hope to hear from you soon
thanks in advance
Tino
"I am doing this for n numbers of any size"
Of course, it is trivial to change the vector sizes:
(e*p.^(e-1)).^(0:4)
% ^ change this!
cumprod(e*p.^(e-1)*ones(1,4))
% ^ change this!
Thanks for your answer and your patient with me Stephen
Another question
I have the following matrices [m{:}] representing p
when I implemented it using the
ccv, mi] = size(m);
disp(size(m))
disp(m);
e = 0.92;
nnn = e * [m{:}].^(e - 1) ;
X = cumprod(e * [m{:}].^(e - 1)).* ones(1, ccv);
I get error for the arrays
Please can you see if I am doing anything wrong
Thanks in advance
Tino
If p is non-scalar then those methods are not suitable. You should simply use a loop and indexing.
Can you assist me with a loop pls?
if not then you ve done your best and I appreciate it
Regards
e = 1.2;
p = [3,4;5,6];
N = 5;
C = cell(1,N);
C{1} = 1;
for k = 2:N
C{k} = e*p.^(e-1) * C{k-1};
end
Giving:
>> C{:}
ans = 1
ans =
1.4949 1.5834
1.6557 1.7172
ans =
4.8563 5.0860
5.3181 5.5703
ans =
15.680 16.423
17.172 17.986
ans =
50.631 53.029
55.449 58.076
thanks Stephen. I appreciate

请先登录,再进行评论。

更多回答(1 个)

Use the cumprod function. [Don't make individually numbered variables, store them as elements of one vector.]

1 个评论

Thanks Steve
I have done this but not working
e = 0.92;
nnn = e * P.^(e - 1);
for j = 1 : kol
X{j} = cumprod(nnn);
end
But not working how do I go about it

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2019a

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by