Problem with equation input

1 次查看(过去 30 天)
>>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?

采纳的回答

Star Strider
Star Strider 2015-2-14
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 个评论
Amaya
Amaya 2015-2-14
Thank you! It worked, I didn't copy the for and end and now it works, thank you!
Star Strider
Star Strider 2015-2-14
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by