ode and dsolve solver starting with $1000 with 3% annual interest rate in matlab

To model the growth of $1000 with a 3% annual interest rate in MATLAB, we can use the ode45 solver. We can define a function that calculates the change in balance over time, given a fixed interest rate. Here's an example code:

main.m
% Define parameters
tspan = [0 10]; % time span
y0 = 1000; % initial balance
r = 0.03; % annual interest rate

% Define ODE function
dydt = @(t,y) r*y;

% Solve ODE
[t,y] = ode45(dydt, tspan, y0);

% Plot the results
plot(t,y)
xlabel('Time (years)')
ylabel('Balance ($)')
title('Balance growth over time')
306 chars
17 lines

This will plot the balance growth over a 10 year time span. To solve the same problem using dsolve, we can represent the equation directly and solve it symbolically. Here's an example code:

main.m
% Define symbolic variables
syms y(t) r

% Define ODE equation
ode = diff(y,t) == r*y;
cond = y(0) == 1000;

% Solve ODE
ySol(t) = dsolve(ode,cond);

% Plot the results
fplot(ySol)
xlabel('Time (years)')
ylabel('Balance ($)')
title('Balance growth over time')
260 chars
16 lines

This will also plot the balance growth over a 10 year time span, but using a symbolic solver.

gistlibby LogSnag