Using ode45 to solve an interest rate problem regarding an ODE.

So, I am currently taking a course that requires me to use MATLAB, and one of the assignments I have due coming up is utilizing MATLAB to solve ODEs utilizing the ode45 application.
There are two problems I am running into:
1.) My professor does not lecture at all, so everything is self taught through brief power point guides. 2.) I have not taken a differential equations class in almost 2 years, and that professor was terrible (don't know how I passed), therefore my DE knowledge is somewhat rusty.
These two problems are working together to pretty much give me a task that I have no clue how to complete. Any help to get me started on solving this problem would be greatly appreciated.
I have attached a snip of the problem:
I am completely stuck as to where to start. I have researched across the internet, but with no results for my specific need.
Thanks, Ryan

1 Comment

That was exactly what I needed! Thank you so much James! It was just a matter of reassigning the variables as needed. I was just having a hard time putting that together in my head.
Here is my code (looks ridiculously familiar to the example haha):
Define given values:
r=0.08;
S0=1000;
t=[0 10];
Evaluate from 0 to 10 years.
[t,S]=ode45(@(t,S) r*S, t, S0)
All I have left to do now is grab the last value for when t = 10 and fprintf. Thank you so much! You're a life saver!

Sign in to comment.

 Accepted Answer

Look at the very first example here in the doc:
https://www.mathworks.com/help/matlab/ref/ode45.html?searchHighlight=ode45&s_tid=doc_srchtitle
There you will see an example of solving the following DE: y' = 2t
You have a similar 1st order DE that looks like this: S' = rS
So, the documented example has a derivative in terms of time, but your derivative is in terms of the variable itself. Not to worry, since ode45 supports both. In the doc, you will see that you need to create a function for your derivative. It looks like this in that first example (the function will always have input arguments of (t,y) ... i.e. time and variable):
@(t,y) 2*t
That's an anonymous function handle that has two input arguments, t and y, and outputs the derivative value 2*t. You can see that it matches the above derivative formula y' = 2t.
But for your case, you will not be using the first argument t in the derivative, but will be using the second argument (the variable itself) y. For ease of notation, let's use S instead since that is your variable name. You simply need to construct a derivative function to match your formula that has (t,S) as inputs (again ... using S as the dummy variable name instead of y for ease in matching it up to your formula). That would simply be the following:
r = 8/100 <-- the continuous percentage rate per year
@(t,S) = r*S <-- the derivative function (be sure you define r first!)
Looking back at the example in the doc, you will also need to supply a time span and an initial value. Since the interest rate is in time units of years, your time span vector should be in units of years also. So simply give an initial time in years and a final time in years, i.e. a time span of [0 10]. And then the initial value is given in the problem statement as S0 = 1000 in dollars.
Your job now is to put all of this together into a program to get the result. Again, look at the example code and simply make substitutions of your particular equations and values in the appropriate places and you should have it. If you still have problems, come back and post your code and we can work through the issues.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!