How can I shade an interval in a calculus function with the area() function?
26 ビュー (過去 30 日間)
古いコメントを表示
Alfonso Rodriguez
2024 年 12 月 9 日 21:39
コメント済み: Star Strider
2024 年 12 月 10 日 19:31
I want to shade the area between an interval in a math function like the example below. This is for a calculus class. I was thinking on using the area() function. Thanks.
0 件のコメント
採用された回答
Star Strider
2024 年 12 月 9 日 22:10
編集済み: Star Strider
2024 年 12 月 9 日 22:15
Try this —
x = linspace(0, 1);
y = 1 + 0.5*sin(2*pi*x);
figure
plot(x, y)
xlim([0 1.25])
ylim([0 2])
Lv = x >= 0.2 & x <= 0.6;
A = trapz(x(Lv), y(Lv));
hold on
patch([x(Lv) flip(x(Lv))], [zeros(size(y(Lv))) flip(y(Lv))], [1 1 1]*0.75)
hold off
text(0.2, -0.15, 'x = a')
text(0.6, -0.15, 'x = b')
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim;
yl = ylim;
pos = gca().Position;
annotation('arrow', xapf([0.4 0.5],pos, xl), yapf([0.8 1.4],pos, yl))
text(0.5, 1.4, sprintf('A = %.2f', A), 'Vert','bottom')
text(x(end), y(end), 'y = f(x)', 'Horiz','left')
EDIT —
Forgot the ‘y = f(x)’ text object. Now added.
.
2 件のコメント
Star Strider
2024 年 12 月 10 日 19:31
As always, my pleasure!
A slightly more informative version —
x = linspace(0, 1);
y = 1 + 0.5*sin(2*pi*x);
figure
plot(x, y, LineWidth=2)
xlabel('$x$', Interpreter='LaTeX', FontSize=20, FontWeight='bold')
ylabel('$y$', Interpreter='LaTeX', FontSize=20, FontWeight='bold')
xlim([0 1.25])
ylim([0 2])
Lv = x >= 0.2 & x <= 0.6;
A = trapz(x(Lv), y(Lv));
hold on
patch([x(Lv) flip(x(Lv))], [zeros(size(y(Lv))) flip(y(Lv))], [1 1 1]*0.5, EdgeColor='none', FaceAlpha=0.5)
hold off
text(0.2, -0.15, '$x = a$', Interpreter='LaTeX', FontSize=12)
text(0.6, -0.15, '$x = b$', Interpreter='LaTeX', FontSize=12)
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim;
yl = ylim;
pos = gca().Position;
annotation('arrow', xapf([0.4 0.6],pos, xl), yapf([0.8 1.1],pos, yl))
text(0.6, 1.1, sprintf('$A = %.2f$', A), 'Vert','bottom', Interpreter='LaTeX', FontSize=14)
text(x(end)+0.05, y(end), '$y = f(x)$', 'Horiz','left', 'Vert','middle', Interpreter='LaTeX', FontSize=12)
text(0.5,1.6, '$$\int_a^bf(x)\ dx$$', Interpreter='LaTeX', FontSize=20)
.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!