Problem with equation input

2 views (last 30 days)
Amaya
Amaya on 14 Feb 2015
Commented: Star Strider on 14 Feb 2015
>>r= APR/1200
A= 200000
APR= [4,5,6]
M= [36,48,60,72]
These are my variables When I put my formula in matlab i get this message
>> P= (A*r*(1+r)^M)/(((1+r)^M)-1)
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
So i replace the ^ with .^ and i get this message instead:
>> P= (A*r*(1+r).^M)/(((1+r).^M)-1)
Error using .^
Matrix dimensions must agree.
Does anyone know what is wrong with my equation and what I can do to fix it?

Accepted Answer

Star Strider
Star Strider on 14 Feb 2015
There are more efficient ways to approach this, but probably the easiest is to use a for loop:
A= 200000;
APR= [4,5,6];
M= [36,48,60,72];
for k1 = 1:length(APR)
r = APR(k1)/1200;
P(k1,:)= (A.*r.*(1+r).^M)./(((1+r).^M)-1);
end
I am not certain what you want to do, but this is one solution. It uses ‘APR’ to define the array size, and computes ‘P’ for all values of ‘M’ for each value of ‘APR’.
Note that I completely vectorised your ‘P’ assignment. See the documentation for Special Characters, ‘Array Indexing’, Array vs. Matrix Operations, and for loops.
  4 Comments
Amaya
Amaya on 14 Feb 2015
Thank you! It worked, I didn't copy the for and end and now it works, thank you!
Star Strider
Star Strider on 14 Feb 2015
My pleasure!
There are version differences (I’m using R2014b), so I was ready to troubleshoot those if you had problems running my code in an earlier version. I’m glad that was not necessary.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!