1 Replies - 1937 Views - Last Post: 05 November 2012 - 09:36 PM Rate Topic: -----

#1 ag7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,601
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1