How to create a loop

2 views (last 30 days)
Egbert  Jansen
Egbert Jansen on 10 Apr 2019
Edited: Stephen23 on 10 Apr 2019
This is my code. How can i create a loop to remove dubble coding.
e(1) = ; %Primary expenditure at starting point
e(2) = e(1)*(1+ge)/(1+y);
e(3) = e(2)*(1+ge)/(1+y);
e(4) = e(3)*(1+ge)/(1+y);
e(5) = e(4)*(1+ge)/(1+y);
e(6) = e(5)*(1+ge)/(1+y);
e(7) = e(6)*(1+ge)/(1+y);
e(8) = e(7)*(1+ge)/(1+y);
e(9) = e(8)*(1+ge)/(1+y);
e(10) = e(9)*(1+ge)/(1+y);
e(11) = e(10)*(1+ge)/(1+y);
e(12) = e(11)*(1+ge)/(1+y);
e(13) = e(12)*(1+ge)/(1+y);
e(14) = e(13)*(1+ge)/(1+y);

Answers (2)

Jan
Jan on 10 Apr 2019
e = zeros(1, 14); % Pre-allocate
for k = 2:14
e(k) = e(k-1) * (1 + ge) / (1 + y);
end
  1 Comment
Adam
Adam on 10 Apr 2019
With
e(1) = ...
whatever it needs setting to between creation of e and the for loop.

Sign in to comment.


Stephen23
Stephen23 on 10 Apr 2019
Edited: Stephen23 on 10 Apr 2019
A geometric series really is simpler to write using vectorized code:
k = (1+ge)/(1+y);
e0 * k.^(0:13)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!