Cumulative If Loop question?
古いコメントを表示
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 件のコメント
Matt J
2012 年 10 月 8 日
Looks like you're missing a tax bracket between $178650 and $388350. What's the rate there?
Teddy Xiong
2012 年 10 月 8 日
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.
採用された回答
その他の回答 (1 件)
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;
カテゴリ
ヘルプ センター および File Exchange で Get Started with MuPAD についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!