Why can't I form a table from my equation
2 views (last 30 days)
Show older comments
Hello, I have code that is sucessfully working. However I can't seem to form a table. Im new to matlab and honestly considering just turning this in as is, becuase ive spent more time than I would like to on this simple problem. Can anyone help me understand? I get in my command window:
Error using table (line 233)
All table variables must have the same number of rows.
Error in scratch (line 26)
T=table(n',P(mon+1)')
clc
clear
P=1000;
tar=10000;
con=100;
interest=0.05/12;
mon=0;
while P<=tar
mon=mon+1;
P(mon+1) = P(mon)+(P(mon)*interest);
P(mon+1) = P(mon+1) + con;
end
fprintf('It will take %d months to reach %2.2f', mon, tar)
year=fix(mon/12);
mon=mon+1
n = [1:1:mon];
T=table(n',P(mon+1)')
mon=mon-(year*12);
fprintf('\n')
if mon>0
fprintf('Amount of time : %d and %d months\n',year,mon);
else
fprintf('Amount of time : %d \n',year);
end
0 Comments
Answers (1)
neil jerome
on 27 Jul 2020
Edited: neil jerome
on 27 Jul 2020
you are indexing the (mon+1)th element of P, which is to say you are trying to line up n, which has many elements, with P(mon+1) which has only one element. hence, they don't match, and the error. (also, P only has mon elements until then, so the index would be wrong anyway). just use:
T=table(n',P')
and the table works.
if you don't mind an unsolicited word of warning - when you're dealing with compound interest, you need to consider that the growth is exponential in the period when it is compounded, which in this code looks to be per month. so if you are multiplying up by (1.05/12), that is not going to give the same answer as the true monthly interest rate, given by:
exp( log(1.05)/12 ) - 1
and so the one you are using is (just a little bit) too high. try your code with con=0, and at month 13 (ie 12 months after the first deposit) you do not get 1050, which is what you should expect with a true interest of 5% per year. good luck!
3 Comments
neil jerome
on 27 Jul 2020
Edited: neil jerome
on 27 Jul 2020
no problem tyson! don't edit that part :) here's the code i copied from you, with that one modified line where you define T. (my reply refers explicitly to this code, in case our experiences differ.)
clc
clear
P=1000;
tar=10000;
con=100;
interest=0.05/12; % this is your original line
% interest = exp( log(1.05)/12) -1; % this is my 'suggestion' instead :)
mon=0;
while P<=tar
mon=mon+1;
P(mon+1) = P(mon)+(P(mon)*interest);
P(mon+1) = P(mon+1) + con;
end
fprintf('It will take %d months to reach %2.2f', mon, tar)
year=fix(mon/12);
mon=mon+1
n = [1:1:mon];
T=table(n',P') % this is the edited line that gives the table
mon=mon-(year*12);
fprintf('\n')
if mon>0
fprintf('Amount of time : %d and %d months\n',year,mon);
else
fprintf('Amount of time : %d \n',year);
end
something i should've said before - when you get errors about mismatched sizes, a good strategy is to go to the line giving the error, and individually copy out each of the parameters (use copy & paste, don't retype; that way you know you are copying any typos you might have made) and insert it into size(). really break it down. in your original code, we look at the line giving the error (beginning "T=..." ), and we run " size(n') " and " size(P(mon+1)' " and get the answers " 75 1 " (all good), and an error (no good). the error message here is trying to tell you that you are asking for an element of P that does exist - specifically, you are asking for (mon+1) when there are only mon, so it can't give you what you want. if we rectify this and ask " size(P(mon)' ", we see that the answer " 1 1 " means that P(mon) is only one number, ie only this one element of P, the one at position mon, and the size of this element on its own is 'a matrix of 1 row, 1 column'. so i hope you see that this single element it isn't the same size as n, so table() can't work. but the whole of P, as you correctly made it, is the right length, so if you give
T=table(n',P')
then it works. for what it's worth, error messages in matlab are often hard to interpret, but they really are trying to be helpful, so its worth giving them the benefit of the doubt and spending some time trying to work with them. and i say this with soooo much red text as part of my every day matlab experience :)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!