필터 지우기
필터 지우기

Cumulative If Loop question?

조회 수: 17 (최근 30일)
Teddy Xiong
Teddy Xiong 2012년 10월 8일
I am trying to write a function that expresses the tax rate. For example, for an income of $40,000, the first $35,350 is taxed at 15%. The remaining $4,650 is taxed at 28%. I am not quite sure how to express this. I have this code written up, but it only shows the tax rate for a specific income, but it doesn't calculate the difference between the first and the second tax rate and account for the difference in the tax rate. How do i go about fixing this?
  댓글 수: 3
Teddy Xiong
Teddy Xiong 2012년 10월 8일
36% sorry!
Walter Roberson
Walter Roberson 2012년 10월 8일
There is no such thing as an "if loop" ! There are "if statements" and there are "for loops" and "while loops", but "if" only executes the body once not repeatedly.
I have removed "ifloop" from the tags.

댓글을 달려면 로그인하십시오.

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 10월 8일
elseif 35350 <= income & income < 85660
taxrate= .28
taxpaid= (taxrate .* income) + income
The math part of this should read something like
taxpaid = 0.15*35350+0.28*(income-35350);
%Apply 15% to the 35359 and 28% to the remainder
Now you can take all of those fixed values and turn them into vectors that could be traversed with a for-loop or fancy multiplication
taxrate = [0.15 0.28 0.31 0.396];
taxlim = [35350 etc.]
  댓글 수: 1
Teddy Xiong
Teddy Xiong 2012년 10월 8일
so i edited my code this way and i'm not getting quite the right output I put in
x1 = 50000; x2 = 200000;
[taxr1 taxp1] = TaxCalc(x1)
[taxr2 taxp2] = TaxCalc(x2)
[taxr3 taxp3] = TaxCalc([x1 x2])
and i'm supposed to get:
= 50000; x2 = 200000;
[taxr1 taxp1] = TaxCalc(x1)
taxr1 =
0.1881
taxp1 =
9.4045e+003
[taxr2 taxp2] = TaxCalc(x2)
taxr2 =
0.2795
taxp2 =
5.5902e+004
[taxr3 taxp3] = TaxCalc([x1 x2])
taxr3 =
0.1881 0.2795
taxp3 =
1.0e+004 *
0.9405 5.5902

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2012년 10월 8일
편집: Matt J 2012년 10월 8일
function [taxrate, taxpaid] = TaxCalc(income)
rates=[.15,.28,.31,.36,.396];
brackets=[0, 35350, 85660, 178650,388350];
bracketRevenues =[0, cumsum( diff(brackets).*rates(1:end-1))];
[~,n]=histc(income,[brackets,inf]);
taxrate = rates(n);
taxpaid = bracketRevenues(n) + (income-brackets(n))*taxrate;

카테고리

Help CenterFile Exchange에서 Get Started with MuPAD에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by