How to create a loop

2 visualizzazioni (ultimi 30 giorni)
Egbert  Jansen
Egbert Jansen il 10 Apr 2019
Modificato: Stephen23 il 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);

Risposte (2)

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

Accedi per commentare.


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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by