% Example using a "for" loop to calculate interest. % Brian Hornbuckle, January 18, 2011. clear; principal = 5000; % will invest $5000. years = 20; % let's say that the money will be invested % for 20 years and interest is compounded yearly rate = 5; % interest rate, % sum = zeros(years,1); % initialize variable to hold sum after each year for k = 1:years if (k == 1) sum(k) = principal; % first year put money in the bank but there is no interest else sum(k) = sum(k-1) + (rate/100)*sum(k-1); % interests compounds every year after first year end; end; sum % amount of money in bank at end of each year