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

Got a tip for us?
Let us know

- Thread starter twoodcc
- Start date

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

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...

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

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?mduser63 said:

Code:

```
while loop here (years)
increase years
for loop months
do stuff
```

(and I think I might have given too much help here)

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

nested loop?twoodcc said: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?

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?

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?

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.atszyman said:

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).

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 worktwoodcc said: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

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

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 said: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

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?bearbo said:are you seriously justifying asking for specific programming idea is learning? i doubt your prof would stand on your side in this case

thanks. i'm trying. it seems my programming is not very organized thoughatszyman said: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.

If you're getting bogged down with long and hard to visualise code, try breaking it down.twoodcc said:thanks. i'm trying. it seems my programming is not very organized though

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!

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 )

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 ____".

thanks for the help guys

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

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

Here's a trivial example that might help you with thinking through your credit card problem.

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,

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

So:

while balance > 0 (

balance = balance - 150 + 50 (

months = months + 2 (

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.*notjustjay said:

Here's a trivial example that might help you with thinking through your credit card problem.

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 moneyin the account, he cando his transaction(use the equation), and he willlast 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 hisbalance, andhow 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.

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

Welcome to the world of programmingtwoodcc said: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.

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:notjustjay said: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!

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?

Code:

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

Code:

```
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
```

well i'm trying this.mufflon said:

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.Code:`year loop here (while, so year increases constantly) month loop here, goes from 1-12`

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*Code:`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`

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?

Well, you have two options.twoodcc said: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?

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