trying to write a function to compute different account balances using the compo
ID: 3676737 • Letter: T
Question
trying to write a function to compute different account balances using the compound interest formula in matlab. In this case, it needs to be able to support multiple Interest rates and number of years input. I'm currently using nested for loops, and it works fine for the first set of interest rates, but then it runs only the last number of years calculating for the second and any later values in the interest input array
This is the output I'm getting currently:
What am I missing? How can I make it go back to the first value of n for the second run of I?
trying to write a function to compute different account balances using the compound interest formula in matlab. In this case, it needs to be able to support multiple Interest rates and number of years input. I'm currently using nested for loops, and it works fine for the first set of interest rates, but then it runs only the last number of years calculating for the second and any later values in the interest input array
function ans = growth_of_money(P,I,n) disp([' P ','n ','I ','Total']); for I = I for n = n disp([P,n,I,compound_interest(P,I,n)]); end end end function T = compound_interest(P,I,n) T= P.*((1.+I).^n); end
This is the output I'm getting currently:
P n I Total 2 1 4 10 2 3 4 250 2 3 7 1024
What am I missing? How can I make it go back to the first value of n for the second run of I?
Explanation / Answer
Fortunately, we can create a function in Matlab for the compound interest formula, like this:
function A = comp_int(P, r, n, t)
A = P*(1 + r/n)^(n*t);
and we can call it from another m-file, script, or from the command window, in this way:
P = 10000;
r = 0.05;
n = 12;
t = 3;
format bank
A = comp_int(P, r, n, t)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.