use a loop in matlab to determine how long it will take to accumulate in a bank account if you deposit initially and at the end of each year; the account pays percent annual interest. in matlab

Here's an example code to calculate how long it will take for a certain amount to accumulate with annual additional deposits and interest:

main.m
% Define variables
interest_rate = 0.05;     % annual interest rate (5%)
initial_deposit = 1000;   % initial deposit
annual_deposit = 500;     % annual deposit
target_amount = 5000;     % target amount to accumulate

% Initialize variables
current_amount = initial_deposit;
years_passed = 0;

% Loop until the target amount is reached
while current_amount < target_amount
    % Add annual deposit
    current_amount = current_amount + annual_deposit;
    % Add interest
    current_amount = current_amount * (1 + interest_rate);
    % Increase number of years passed
    years_passed = years_passed + 1;
end

% Display result
disp(['It will take ', num2str(years_passed), ' years to accumulate $', num2str(target_amount)])
723 chars
23 lines

This code uses a while loop to repeatedly add the annual deposit and interest to the current amount, until the target amount is reached. The number of years passed is also updated every iteration.

related categories

gistlibby LogSnag