Simple While Loop Problem [MATLAB]

In summary, the conversation discusses a homework problem where the task is to write a script using a while loop to calculate the number of years it will take for a bank account to accumulate at least $100,000. The account starts with a certain amount of money and adds the same amount at the end of each year, with an additional percentage of interest. The conversation also mentions an attempt to find a general equation for the account balance as a function of years. The solution involves setting up a while loop and writing the equation in MATLAB, but the individual is unsure of how to do so and how to determine the number of years it takes for the account balance to exceed $100,000.
  • #1
nickadams
182
0

Homework Statement



Write a script using a while loop that calculates the number of years it will take your bank account to accumulate at least $100,000 if you begin with s number of dollars, and you add that same amount at the end of each year. In addition, your account pays a percentage of interest, inter, every year.

Test your code for s = 500, inter = 5 and s = 1, inter = 50.

Homework Equations



In an attempt to find a general equation of money in the account as a function of years, I wrote the equation for the first 4 years and looked for a trend. The general form I found looks like...

$n = [itex]\frac{inter^n * s}{100^n}[/itex] + 2*[itex]\frac{inter ^(n-1) * s}{100^ (n-1)}[/itex] + [itex]\frac{inter ^(n-2) * s}{100^(n-2)}[/itex] + [itex]\frac{inter^(n-3) * s}{100^(n-3)}[/itex] + [itex]\frac{inter^(n-4) * s}{100^(n-4)}[/itex]

... and it keeps adding these terms until the x in "inter^(n-x)" and "100^(n-x)" is equal to n. As you can see, when x is equal to n, the final term will be just an s. That is what we want because at the end of every year we add "s" to the account.

The Attempt at a Solution



How can I get that above equation in a general form so that I can use it in MATLAB to find dollars in the account as a function of years?

In order to solve the problem, I feel that I need to set up a while loop that will terminate once the dollars in the account exceed 100,000. But the problem is that I don't know how to put my function into MATLAB, and I also don't know how to identify how many years it took for the total $ to exceed 100,000.Please help!
 
Physics news on Phys.org
  • #2
bump?
 

Related to Simple While Loop Problem [MATLAB]

1. What is a "Simple While Loop" in MATLAB?

A simple while loop in MATLAB is a control flow statement that allows a set of instructions to be repeated until a certain condition is met. It consists of a while keyword, followed by a logical expression that evaluates to either true or false, and a block of code that will be executed as long as the condition is true.

2. How is a "Simple While Loop" different from a "For Loop" in MATLAB?

While loops and for loops are both control flow statements in MATLAB, but they have different structures and purposes. A while loop repeats a block of code as long as the specified condition is true, while a for loop repeats a block of code for a specified number of times. While loops are typically used when the number of iterations is not known in advance, while for loops are useful for iterating over a sequence of values.

3. What is the syntax for writing a "Simple While Loop" in MATLAB?

The basic syntax for a simple while loop in MATLAB is:

while (condition)
% code to be executed
end

The condition is a logical expression that is evaluated at the beginning of each iteration. If it is true, the code inside the loop is executed. Once the condition becomes false, the loop ends.

4. Can a "Simple While Loop" be nested within another loop in MATLAB?

Yes, a while loop can be nested within another loop in MATLAB. This means that a while loop can be placed inside another while loop, a for loop, or even a do-while loop. This can be useful for creating more complex programs that require multiple iterations and conditional statements.

5. How do you avoid an infinite loop in a "Simple While Loop" in MATLAB?

An infinite loop occurs when the condition in a while loop is always true, causing the loop to continue indefinitely. To avoid this, it is important to make sure that the condition eventually becomes false. This can be done by carefully selecting the initial condition and ensuring that the code inside the loop changes the value of the condition so that it eventually becomes false.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
841
  • Engineering and Comp Sci Homework Help
Replies
1
Views
972
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
990
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
927
Back
Top