Multiples of numbers and plotting?

3 views (last 30 days)
jon
jon on 7 Nov 2012
Ok so I have this homework assignment to do and I can't figure out what to do. If you guys have the book (MATLAB for engineers 3rd edition Holly Moore) its chapter 9 problem 9.10 about college savings.
So basically, I have to calculate the amount of money Ill save every month of the year, easy enough. But the trick here is that my teacher wants us to not only calculate the values every month for 18 years, but print out in formatted output every 6 months. So basically, after i calculate for every month, i need to find a way to pull out every multiple of 6 months into formatted output, like a table. Also, why doesnt this plot properly? Thanks for youre help!
function savings=college(x)
x=input('Please input your starting savings>');
z=input('How much are you contributing every month>');
interest=(.5)/(100)*(x);
months=1:1:216;
for i=1:1:216;
new_balance=x+interest+z;
x=new_balance;
interest=(.5)/(100)*(x);
fprintf('%5.2f\n',new_balance')
end
plot(months,x)
xlabel('Months')
ylabel('New Balance in Dollars')
title('College Savings Over 18 Years')
axis([0,216,0,42000])
grid on
end
  6 Comments
Walter Roberson
Walter Roberson on 7 Nov 2012
No, you should read the documentation for array indexing.
jon
jon on 7 Nov 2012
i read it, but i still dont understand what to do. ive tried to access some numbers from the final list of numbers which is a bunch of rown but 1 column. so i typed something like x(6) but i get an error.

Sign in to comment.

Answers (1)

David Barry
David Barry on 7 Nov 2012
Edited: David Barry on 7 Nov 2012
Jon,
We obviously can't give you the direct answer as this is a homework problem but here are some tips that may help you solve this problem in addition with the comments above.
Suppose I want to store the numbers 2, 10, 50, 90 in x I might write x = [2, 10, 50, 90]
I could then change the number stored in position 2 by typing x(2) = newvalue or add a new number by typing x(5) = newvalue. Try to imagine doing this in a loop and using your loop counter somewhere in the code.
If I then want to find the third value in my variable x which we know is 50 then I would type x(3)
If I wanted to find every other value in my variable I might type x(1:2:end) which gives me all the values from the first value to the end value in steps of 2.
One final thing to say is that in MATLAB, the command x = x+1 is allowed.
Please try to correct your code and then re-post what you have done.
  10 Comments
jon
jon on 7 Nov 2012
it actually says its an undefined variable
David Barry
David Barry on 7 Nov 2012
Ahh that's because it's a function not a script, my apologies. Anyway, take my word for it, you are only storing one value in x. Try the following in MATLAB and see what you get.
a = 3;
b = 2;
for counter = 1:10
c = a + b;
end
c
And then try
a = 3;
b = 2;
for counter = 1:10
c(counter) = a + b;
end
c
And then look for the difference.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!