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.m15 chars2 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.m61 chars4 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.m145 chars7 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.m131 chars6 lines
This will produce a plot of the balance over time, which should show exponential growth:
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