How do I plot multiple lines of the same figure as if they are one?
3 ビュー (過去 30 日間)
古いコメントを表示
I am trying to create a figure with four components on a set range and I cannot figure out how to divvy up the x axis among the equations without error. This is what I have so far:
x = 0:200000;
%Calculate first tax bracket
if x < 12000
y_1 = 0.1 .* x;
end
%Calculate second tax bracket
if (x > 12000)&&(x < 46000)
y_2 = 1200 + 0.15 .* x;
end
%Calculate third tax bracket
if (x > 46000)&&(x < 120000)
y_3 = 6300 + .25 .* x;
end
%Calculate fourth tax bracket
if x > 120000
y_4 = 24800 + .33 .* x;
end
%Plot
plotyy(x, y_1, x, y_2, x, y_3, x, y_4);
xlabel('Income (Dollars)');
ylabel('Taxed amount (Dollars)');
title('Income v. Taxed amount');
0 件のコメント
採用された回答
David Sanchez
2014 年 11 月 4 日
You can get your results also like this:
x = 0:200000;
%Calculate first tax bracket
y_1 = 0.1 .* x(x < 12000);
%Calculate second tax bracket
y_2 = 1200 + 0.15 .*x((x > 12000)&(x < 46000));
%Calculate third tax bracket
y_3 = 6300 + .25 * x((x > 46000)&(x < 120000));
%Calculate fourth tax bracket
y_4 = 24800 + .33.*x(x > 120000);
0 件のコメント
その他の回答 (1 件)
Star Strider
2014 年 11 月 4 日
Here’s a relatively simple way to do it:
x = 0:200000;
ytax = @(x) [(x<12000).*(0.1 .* x) + ((x >= 12000)&(x < 46000)).*(1200 + 0.15 .* x) + ((x >= 46000)&(x < 120000)).*(6300 + .25 .* x) + (x >= 120000).*(24800 + .33 .* x)];
figure(1)
plot(x, ytax(x))
xlabel('Income (Dollars)');
ylabel('Taxed amount (Dollars)');
title('Income v. Taxed amount');
My ‘ytax’ function creates an implicit if block, taking advantage of the fact that when a particular logical condition is 'true', it equates numerically to 1, and when 'false' to 0. Adding them together in the matrix in the function will produce a continuous variable that has the appropriate values over the appropriate regions of ‘x’.
I copied and pasted the ‘if’ conditions and equations directly from your code into ‘ytax’.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Dialog Boxes についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!