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?
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!