PDA

View Full Version : more help for matlab beginner

twoodcc
Sep 21, 2006, 11:03 AM
well i'm still having trouble with matlab. we now have our first real assignment, and i'm having trouble.

it's about paying off a credit card.

my first question is: how do i make matlab go from month to month, year to year? in other words, how can i tell matlab that 1 year has passed, and it's time to make the credit card payment?

thanks

mduser63
Sep 21, 2006, 11:13 AM
Your question is much too vague. We'd need to know A LOT more details about the assignment. Besides that, while I can understand your desire for help, you really need to do the assignment yourself. I'm happy to answer questions about Matlab syntax, etc, but to give you help doing abstract things that are the core of your assignment isn't really quite right.

In case you think I'm being harsh, I'm a college student too, and I had to take a beginner's Matlab course when I started school. I use Matlab nearly every day now, so it was really valuable, but I doubt I would know what I do now if someone had just walked me through every step of the way. A big part of learning is struggling to figure things out on your own.

To give you just a little help, I'll say that Matlab isn't made for credit card simulations (or any other really abstract simulation) specifically. Matlab is a programming language fundamentally. You need to figure out how to code the simulation to account for time passed. Matlab doesn't have some built-in "simulate_a_month_passing()" function. You might consider a for loop...

mufflon
Sep 21, 2006, 11:13 AM
well there is for-loops

for i:1:500 or something like that, use "help for" or "man for" - forgot which, to learn more on how to use it.

Please be more specific, hard to help otherwise :(

twoodcc
Sep 21, 2006, 12:23 PM
well the assignment is this:

assume the person only pays the min payment (\$15), how long will it take to pay off the credit?

i have to display how long it took in years and months. my question was:

what's the best way to tell matlab 1 year has passed, and tell it to start the months count over.

i know i need to do this myself, but i can sit around here all day trying, but i don't think i'm just gonna dream up the answer

mduser63
Sep 21, 2006, 12:31 PM
Use a for loop to simulate the passage of time. I really think that's the only hint you should need. The rest is math and the specifics of coding the thing in Matlab. There shouldn't be any reason why you can't keep track of the time in months until the very end where you convert it to years + months.

twoodcc
Sep 21, 2006, 12:37 PM
Use a for loop to simulate the passage of time. I really think that's the only hint you should need. The rest is math and the specifics of coding the thing in Matlab. There shouldn't be any reason why you can't keep track of the time in months until the very end where you convert it to years + months.

thanks. but i think i'm supposed to do a while loop. it's not required though. either way, how do i make the months go back to zero after 12 months?

mufflon
Sep 21, 2006, 12:53 PM
you can make nested for loops

while loop here (years)
increase years

for loop months
do stuff

try to combine stuff - no one stops you unless you get an error message!
(and I think I might have given too much help here)

edit: double -> nested, a slight typo noticed thanks to bearbo

bearbo
Sep 21, 2006, 12:55 PM
thanks. but i think i'm supposed to do a while loop. it's not required though. either way, how do i make the months go back to zero after 12 months?
nested loop?

but i agree with mduser63, you really should think hard on the assignment yourself.. be able to think like a programmer makes you a good programmer.. syntex are the easy part (well mostly), the hard part is to think of creative way for computer to understand logically...

and just a little and perhaps too much hint, you probably need more than just two loops.. you might want to the while loop to do something along the line of "while i still have money to pay off"... btw, are you counting interest?

twoodcc
Sep 21, 2006, 04:36 PM
thanks guys. yes it does count interest, but just simply interest every month.

about helping me....i'm not going to dream up how to program in matlab. if i don't get help here, i'll have to get it somewhere else. if everyone were always confined to their own ideas, no one would ever learn anything.

back to the problem. so either i make the months go back to 0 after 12, or i make another variable divide the total months by 12 for the years, and remainder is the months, right? which one is better/easier?

atszyman
Sep 21, 2006, 05:13 PM
The easiest method would be to keep a running total for the months. Once you hit the end divide that total by 12. Round down the nearest integer, now you have years. Multiply that integer by 12 and subtract that value from your total months, that is the number of months beyond the full years that have past. That's the simple and dirty way to do it.

If you want to get complex nested loops would work (although MATLAB is horrendously slow with loops). Another time saving tool would be to find the equivalent of the C++ % operator which gives you the remainder of a division (i.e. 17%5 = 2).

twoodcc
Sep 21, 2006, 05:47 PM
The easiest method would be to keep a running total for the months. Once you hit the end divide that total by 12. Round down the nearest integer, now you have years. Multiply that integer by 12 and subtract that value from your total months, that is the number of months beyond the full years that have past. That's the simple and dirty way to do it.

If you want to get complex nested loops would work (although MATLAB is horrendously slow with loops). Another time saving tool would be to find the equivalent of the C++ % operator which gives you the remainder of a division (i.e. 17%5 = 2).

thanks, i guess i'll just do that.

but how exactly should i do it? like this?

floor(months / 12) rem /12

seems like something else should be after rem

bearbo
Sep 21, 2006, 05:53 PM
thanks, i guess i'll just do that.

but how exactly should i do it? like this?

floor(months / 12) rem /12

seems like something else should be after rem
i forgot the operator, but months rem 12 (provided rem is the operator) should work

are you seriously justifying asking for specific programming idea is learning? i doubt your prof would stand on your side in this case

atszyman
Sep 21, 2006, 05:53 PM
thanks, i guess i'll just do that.

but how exactly should i do it? like this?

floor(months / 12) rem /12

seems like something else should be after rem

How exactly to do it is up to you. Start simple. Ignore interest make a \$1 payment every month. Set the balance to a number you know the answer to, 12, 24, 13, 14, 15, etc. Verify that your loop counts the correct number of total months and then convert and see that you are getting the correct number of years and months. Once you have that working Start adding in other features like interest, variable payments and other nifty bits.

twoodcc
Sep 21, 2006, 06:15 PM
are you seriously justifying asking for specific programming idea is learning? i doubt your prof would stand on your side in this case

but i'm not asking for ideas, or for you to do the problem for me, i'm simply asking for commands, or a way to do a certain task. what's the harm in that? what's the difference in me asking here, and googling?

twoodcc
Sep 21, 2006, 06:16 PM
How exactly to do it is up to you. Start simple. Ignore interest make a \$1 payment every month. Set the balance to a number you know the answer to, 12, 24, 13, 14, 15, etc. Verify that your loop counts the correct number of total months and then convert and see that you are getting the correct number of years and months. Once you have that working Start adding in other features like interest, variable payments and other nifty bits.

thanks. i'm trying. it seems my programming is not very organized though

Erasmus
Sep 22, 2006, 02:26 AM
thanks. i'm trying. it seems my programming is not very organized though

If you're getting bogged down with long and hard to visualise code, try breaking it down.

Make a part that calculates the interest, and another bit that deals with the payments, etc, and only put them together at the end. Flowcharts are great for this stuff.

You obviously need counters to tell how long the time past is for your output.
I would suggest an overall While loop (while amount_owing > 0) which contains a for loop (For i=1:12 (or whatever it is)) to simulate the months, which you would count, and then every time the for loop ends, you add a year, and reset the month counter to zero.

Of course, in the for loop, you would put all your monthly calculations. Easy!

And of course output the years and months.

Hope this helps. I did an Engineering orientated Matlab intro course for Uni last semester. Great program. Good luck!

Oh, and by the way, who really does assignments like that on their own? It's called collaboration, and it's a good thing for everyone involved. Other than being a nice thing to do, helping others with their assignments (Which are just extensions of learning) helps them learn how to do it in the future, doesn't waste the "professor"'s time in explaining, and also helps the person who's explaining gain a firmer knowledge of the material. It is my belief that anyone not willing to offer help has forgotten that the purpose of assignments is not to make people fail, but to make learning assessible.

Hmmm... It seems this rant has gone way over the top, and really wasn't warranted, but it's too good to waste, so...

Cyaz!

notjustjay
Sep 22, 2006, 07:56 AM
I think I gave you this advice last time (if you're the same guy who asked for matlab help in another thread -- sorry, I'm about to leave for work and too lazy to look it up :) )

Forget about Matlab, or programming, for a moment.

Grab a pen and paper.

Do the entire problem on paper first.

Please don't take offense, but the kinds of questions you are asking suggest that you're not quite familiar with the concept of developing algorithms. In other words you're not quite thinking like a programmer yet.

There's nothing wrong with that. Everyone starts somewhere. Some people can't think like programmers at all (I think I work with some of them :eek: )

But sit down and do it yourself. Maybe draw a table on a sheet of paper. Month 1: Balance is this, payment is this, interest is this. Month two, it's that. Month 3, ... Figure out the pattern, e.g. what calculations are you doing. Generalize it. Once you can narrow down EXACTLY what steps you're taking ON PAPER, you can write it into an algorithm.

THEN you ask yourself "ok, what Matlab command can help me do ____".

twoodcc
Sep 22, 2006, 10:06 AM
thanks for the help guys :)

it looks like i'm starting to get there, but i still have a long way to go.

notjustjay
Sep 22, 2006, 10:41 AM
Good luck. I think this is a good learning experience either way. :)

Jack has \$2000 in his bank account. Assume it's a crappy bank with 0 interest. Every 2 months he withdraws \$150. Every 2 months his mother deposits \$50 as an allowance. How long until he's out of money?

(Common sense says he's taking \$100 every 2 months from his savings, so we know it'll be 40 months. But let's work it through step by step.)

So you start to think about it...

First 2 months: \$2000 - \$150 + \$50 = \$1900
Next two months: \$1900 - \$150 + \$50 = \$1800
etc.

It doesn't take long to notice that every two months it's the same old pattern. Take the existing balance, subtract his withdrawal amount, add his allowance amount, to get the new balance. We can even write it as an equation: balance = balance - 150 + 50.

So, common sense says, while he's still got money in the account, he can do his transaction (use the equation), and he will last for two more months.

Now it starts to look like an algorithm! I've bolded a few of the action words that help you see it.

Let's rewrite the above statement in a structured algorithm form. It's obvious from the statement that we need to keep track of his balance, and how many months. Those become variables.

So:

while balance > 0 ("while he's still got money")
balance = balance - 150 + 50 ("do the monthly transaction")
months = months + 2 ("lasts for two more months")
end loop

Notice we haven't even touched matlab yet. Now that the algorithm is written in clear terms, you can write it in whatever programming language you want. Matlab, C++, Java, BASIC, Fortran, Perl, whatever.

Now you are in a position to Google search (or ask in these forums) and ask questions such as "what is the syntax of a while loop in Matlab?" or, to calculate years out of months, "How do I do modulus (remainders) in Matlab?"

Do you see the difference between that and your original question of "how do I tell matlab that 1 year has passed"? Your original question mixes up Matlab syntax (what command do I use) with algorithm questions (how do I keep track of credit card payments).

Hope that helps.

twoodcc
Sep 24, 2006, 11:10 PM
Good luck. I think this is a good learning experience either way. :)

Jack has \$2000 in his bank account. Assume it's a crappy bank with 0 interest. Every 2 months he withdraws \$150. Every 2 months his mother deposits \$50 as an allowance. How long until he's out of money?

(Common sense says he's taking \$100 every 2 months from his savings, so we know it'll be 40 months. But let's work it through step by step.)

So you start to think about it...

First 2 months: \$2000 - \$150 + \$50 = \$1900
Next two months: \$1900 - \$150 + \$50 = \$1800
etc.

It doesn't take long to notice that every two months it's the same old pattern. Take the existing balance, subtract his withdrawal amount, add his allowance amount, to get the new balance. We can even write it as an equation: balance = balance - 150 + 50.

So, common sense says, while he's still got money in the account, he can do his transaction (use the equation), and he will last for two more months.

Now it starts to look like an algorithm! I've bolded a few of the action words that help you see it.

Let's rewrite the above statement in a structured algorithm form. It's obvious from the statement that we need to keep track of his balance, and how many months. Those become variables.

So:

while balance > 0 ("while he's still got money")
balance = balance - 150 + 50 ("do the monthly transaction")
months = months + 2 ("lasts for two more months")
end loop

Notice we haven't even touched matlab yet. Now that the algorithm is written in clear terms, you can write it in whatever programming language you want. Matlab, C++, Java, BASIC, Fortran, Perl, whatever.

Now you are in a position to Google search (or ask in these forums) and ask questions such as "what is the syntax of a while loop in Matlab?" or, to calculate years out of months, "How do I do modulus (remainders) in Matlab?"

Do you see the difference between that and your original question of "how do I tell matlab that 1 year has passed"? Your original question mixes up Matlab syntax (what command do I use) with algorithm questions (how do I keep track of credit card payments).

Hope that helps.

thanks for this example. this does help. the only thing is that i know what i want the program to do (i think), i just don't know how to tell it to do it.*

tomorrow i'll try to use this as a guide and start over and see if i can do it this way. i would do it now but i have a test in another class tomorrow

notjustjay
Sep 25, 2006, 04:16 PM
thanks for this example. this does help. the only thing is that i know what i want the program to do (i think), i just don't know how to tell it to do it.

Welcome to the world of programming ;)

Just remember to keep the "what I want it to do" well-separated from "how I want it to happen". It may take some practice to break down your problem into steps small enough to put into a program, but that's exactly what your class should be trying to teach you.

The nice thing is that once you develop a mindset for writing algorithms, you will find it much easier to move from Matlab to any other programming or scripting language.

Let us know if you have any other problems, and good luck! :)

twoodcc
Sep 25, 2006, 04:56 PM
Welcome to the world of programming ;)

Just remember to keep the "what I want it to do" well-separated from "how I want it to happen". It may take some practice to break down your problem into steps small enough to put into a program, but that's exactly what your class should be trying to teach you.

The nice thing is that once you develop a mindset for writing algorithms, you will find it much easier to move from Matlab to any other programming or scripting language.

Let us know if you have any other problems, and good luck! :)

thanks. but i'm still having trouble. here is what i have in a text document:

min_payment = 15
balance = amount_owed * interest_rate

while balance > min payment
balance = balance - min payment
months = months + 1

how do i convert the months to years when it gets over 12 months?

mufflon
Sep 25, 2006, 05:03 PM
you still need a nested loop.

year loop here (while, so year increases constantly)
month loop here, goes from 1-12

So basically, you have one function (month) that does what you described and a loop outside of this that makes months go from 1->12.

year = 0
while there's \$ on account in sufficient amount: begin year one
year = year+1
go through each month, but remove some money. add interest at a fitting point

try to wrap your head around this and tell us if you need any further assisntance, mathlab and programming can be hard if you haven't done much of it in the past *tries to comfort* :cool:

twoodcc
Sep 25, 2006, 05:20 PM
you still need a nested loop.

year loop here (while, so year increases constantly)
month loop here, goes from 1-12

So basically, you have one function (month) that does what you described and a loop outside of this that makes months go from 1->12.

year = 0
while there's \$ on account in sufficient amount: begin year one
year = year+1
go through each month, but remove some money. add interest at a fitting point

try to wrap your head around this and tell us if you need any further assisntance, mathlab and programming can be hard if you haven't done much of it in the past *tries to comfort* :cool:

well i'm trying this.

i have a while loop, and inside of that, a for loop.

my for loop starts: for months < 13

and i get an error message for that. i had it at =< 12, and got the same error. what is the problem?

notjustjay
Sep 26, 2006, 12:10 AM
while balance > min payment
balance = balance - min payment
months = months + 1
how do i convert the months to years when it gets over 12 months?

Well, you have two options.

One is to keep it in months until the very end, then do a conversion by dividing months by 12 and all that stuff (already talked about in earlier messages in this thread).

Another is to use an if statement (have you learned about those?) and keep two separate counters for months and years:

months = months + 1
if months > 12 then
years = years + 1
months = 0

mufflon
Sep 26, 2006, 01:13 AM
I believe the syntax for a for-loop in mathlab is ? for i=1:12 - or something like that. write "help for" or "man for" (dunno which to get manual) directly in matlab main window.

edit: I was indeed correct

for loop variable = startvalue : endvalue
commands
end

twoodcc
Sep 26, 2006, 10:03 AM
I believe the syntax for a for-loop in mathlab is ? for i=1:12 - or something like that. write "help for" or "man for" (dunno which to get manual) directly in matlab main window.

edit: I was indeed correct

for loop variable = startvalue : endvalue
commands
end

thanks, it worked with that. but now if i put i only owe \$20, it still goes through all 12 months

twoodcc
Sep 26, 2006, 07:00 PM
well i would like to thank all who have helped me in this thread. i've finally got it working right, now i've just got to do the finishing touches and stuff

i still might have a few questions, but the program is working (i think at least)

twoodcc
Sep 27, 2006, 09:24 PM
well it looks like i'm still having a little trouble. and it's due tomorrow

how can i make my program stop after a certain amount of years? like 40?

you don't have to write the code for me, just an example of stopping a program after a certain time or whatever.

since i have a while loop, this might be complicated?

twoodcc
Sep 27, 2006, 09:40 PM
well, i've tried a break, and it stops it, but it's still not quite right.

i'm using all months, then converting to years and months. i have an if statement at the end of everything, saying if months < 480, then display a message.

else, display another message

but when i make it where it should take more than 480 months, the break stops it at 480, and it always displays the message that i don't want it too

twoodcc
Sep 27, 2006, 10:07 PM
well, i've tried a break, and it stops it, but it's still not quite right.

i'm using all months, then converting to years and months. i have an if statement at the end of everything, saying if months < 480, then display a message.

else, display another message

but when i make it where it should take more than 480 months, the break stops it at 480, and it always displays the message that i don't want it too

well i think i figured it out. i just had to move that if statement up past where i converted the months to years

notjustjay
Sep 29, 2006, 03:11 PM
Glad you got it going :)

Sorry, I wasn't checking the forums around the time when you posted.