ode45 solver for bank saving rate of 2% annually in matlab

To use the ode45 solver in MATLAB to simulate the bank saving rate with an annual rate of 2%, we would first need to define the differential equation that determines the growth of the savings account over time. Assuming that the interest rate is compounded continuously, the differential equation for the change in the balance, y(t), over time, t, is:

main.m
dy/dt = 0.02*y
15 chars
2 lines

where 0.02 represents the annual interest rate of 2% expressed as a decimal.

In MATLAB, we can define this equation as a function, which we'll call savingsgrowth, of the form:

main.m
function dydt = savingsgrowth(t, y)
    dydt = 0.02 * y;
end
61 chars
4 lines

This function takes in the current time, t, and balance, y, and returns the change in balance over time.

We then call the ode45 solver to simulate the evolution of the balance over time. We'll assume an initial balance of $1000 and a simulation time of 20 years:

main.m
% Define the initial balance and simulation time
y0 = 1000;
tspan = [0, 20];

% Call the ode45 solver
[t, y] = ode45(@savingsgrowth, tspan, y0);
145 chars
7 lines

The ode45 solver returns two arrays, t and y, containing the time points and corresponding balance values at those time points.

We can then plot the balance over time using:

main.m
% Plot the balance over time
plot(t, y);
xlabel('Time (years)');
ylabel('Balance ($)');
title('Bank Savings with 2% Annual Rate');
131 chars
6 lines

This will produce a plot of the balance over time, which should show exponential growth:

Bank Savings Plot

Note that in our example, the differential equation is very simple and could be solved analytically. However, ode45 is useful for more complex differential equations where an analytical solution may not be available.

gistlibby LogSnag