Highlight specific area of graph.

71 ビュー (過去 30 日間)
Mughees Asif
Mughees Asif 2019 年 3 月 5 日
編集済み: Star Strider 2019 年 3 月 5 日
I have the following code:
syms x y
y = -x;
fplot(x, y, 'k')
hold on
y = x.^2-4;
fplot(x, y, 'k')
hold off
xlim([-5 5])
ylim([-5 5])
box on
grid on
axis=gca;
axis.XAxisLocation = 'origin';
axis.YAxisLocation = 'origin';
title('Graph of y = xe^x')
xlabel('x')
ylabel('y')
which generates the following graph. Is there a way of colouring or highlighting the shaded area? Thank you.
graph_Mathworks.png

採用された回答

Star Strider
Star Strider 2019 年 3 月 5 日
編集済み: Star Strider 2019 年 3 月 5 日
You have to change the code slightly to do this:
x = linspace(-5, 5, 1000);
y1 = -x;
y2 = x.^2-4;
lidx = y1 >= y2;
figure
plot(x, y1, 'k')
hold on
plot(x, y2, 'k')
patch([x(lidx) fliplr(x(lidx))], [y1(lidx), fliplr(y2(lidx))], 'g', 'FaceAlpha', 0.5) % <— ADD THIS LINE
hold off
xlim([-5 5])
ylim([-5 5])
box on
grid on
axis=gca;
axis.XAxisLocation = 'origin';
axis.YAxisLocation = 'origin';
title('Graph of y = xe^x')
xlabel('x')
ylabel('y')
Experiment to get the result you want.
EDIT — (5 Mar 2019 at 00:39)
Added plot image:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by