# Problem with calculations in MATLAB while loop

Page 1 of 1

## 1 Replies - 3062 Views - Last Post: 05 November 2012 - 09:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=298377&amp;s=6ab20a8e2dd8e62220d4641682b3e4d8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ag7

Reputation: 0
• Posts: 2
• Joined: 01-November 12

# Problem with calculations in MATLAB while loop

Posted 02 November 2012 - 02:28 PM

I am trying to calculate how many years it would take to save a million dollars given certain expenses and wage. Also, my rent increases \$200 every 5 years and my salary increases 2% annually. Here is my code:

% input statements
monthlyWageAfterTaxes = 2700
savingsGoal= 1E6

%Expenses
rent=800;
utilities=300;
insurance=200;
auto=250;
food_misc=325;
totalSavings=0;
numMonths=0;

while totalSavings < savingsGoal

if rem(numMonths,60)==1;
rent = rent + 200;
end

if rem(numMonths,12)==1;
monthlyWageAfterTaxes = monthlyWageAfterTaxes * 1.02;
end

monthlyBills= rent+utilities+insurance+auto+food_misc;
totalSavings = totalSavings + monthlyWageAfterTaxes - monthlyBills

numMonths= (numMonths+1)

end
numYears=(numMonths/12)

I worked out the problem by hand and came up with 48.1667 years, but the code gives me 49.8333. Can anyone see what might be the problem because I have been trying to debug it for 3 days now. Thanks.

Is This A Good Question/Topic? 0

## Replies To: Problem with calculations in MATLAB while loop

### #2 #define

• Duke of Err

Reputation: 1743
• Posts: 6,200
• Joined: 19-February 09

## Re: Problem with calculations in MATLAB while loop

Posted 05 November 2012 - 09:36 PM

Should there be a semicolon after the if expessions?