Multiples of numbers and plotting?

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 comentarios

Walter Roberson
Walter Roberson el 7 de Nov. de 2012
You overwrite all of "x" in every iteration of the loop, so at the end "x" only has a single value.
jon
jon el 7 de Nov. de 2012
i dont get what youre saying, could you explain more?
Inside your loop you have
x=new_balance;
This changes all of the variable "x", so after the loop is finished, "x" will only be the new_balance calculated in the last iteration of the loop.
You want to change your code so that in each iteration it records an additional "x" value rather than overwriting all of "x".
jon
jon el 7 de Nov. de 2012
so should i just take it out?
Walter Roberson
Walter Roberson el 7 de Nov. de 2012
No, you should read the documentation for array indexing.
jon
jon el 7 de Nov. de 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.

Iniciar sesión para comentar.

Respuestas (1)

David Barry
David Barry el 7 de Nov. de 2012
Editada: David Barry el 7 de Nov. de 2012

2 votos

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 comentarios

jon
jon el 7 de Nov. de 2012
Editada: jon el 7 de Nov. de 2012
i realize you cant just giev me the answer, and i appreciate that. anyways, i tried what you said but its not giving me every 6th value. heres the code:\
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;
interest=(.5)/(100)*(x);
x=new_balance;
fprintf('%5.2f\n',x');
x(1:6:end);
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
David Barry
David Barry el 7 de Nov. de 2012
Jon,
Please edit and use the code button to make it easy to read.
jon
jon el 7 de Nov. de 2012
sorry about that
David Barry
David Barry el 7 de Nov. de 2012
Still not right but I can read it now. Highlight the code and click the code button.
jon
jon el 7 de Nov. de 2012
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;
interest=(.5)/(100)*(x);
x=new_balance;
fprintf('%5.2f\n',x');
x(1:6:end);
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
David Barry
David Barry el 7 de Nov. de 2012
You are still not indexing the variable in the loop. If in your loop you just type x = new_balance then at the end of the loop you will only have one value stored in x rather than 216 values. You need to read my comments above to make sure you store every value during each iteration of the loop.
jon
jon el 7 de Nov. de 2012
when i run the code though i do get a bunch of numbers.
David Barry
David Barry el 7 de Nov. de 2012
You will get a bunch of numbers displayed on the screen because you are calling fprintf during each iteration of the loop. If you actually look at the variable x, just by typing x in the command window, then you will see it only has one value stored in it.
jon
jon el 7 de Nov. de 2012
it actually says its an undefined variable
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

jon
el 7 de Nov. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by