Shading area between 2 curves (x function in terms of y)
13 ビュー (過去 30 日間)
古いコメントを表示
Hello, I'm trying to shade an area between two functions (x depending on y), I shade the correct area but I get a reflection of that shaded region possibly about the line y=x. I don't want this reflection in my graph, I only want the shaded region. Here's my code:
syms x y
x1=y^2; x2=y^3; y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
x1 = fp1.XData;
y1 = fp1.YData;
x2 = fp2.XData;
y2 = fp2.YData;
patch([y1 fliplr(y2)], [x1 fliplr(x2)], 'g'); hold off;
0 件のコメント
採用された回答
Paul
2025 年 6 月 25 日
移動済み: Paul
2025 年 6 月 25 日
Here are the plots of the functions. The independent variable is y, which spans the x-axis on the fplot
syms x y
x1=y^2; x2=y^3; y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
xlabel('y')
ylabel('x')
To shade the region in between we have to note that the xdata is actually the YData of the plot and the ydata is actually the XData of the plot, because we are plotting x = f(y);
figure
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
xlabel('y')
ylabel('x')
%x1 = fp1.XData;
%y1 = fp1.YData;
%x2 = fp2.XData;
%y2 = fp2.YData;
y1 = fp1.XData;
x1 = fp1.YData;
y2 = fp2.XData;
x2 = fp2.YData;
patch([y1 fliplr(y2)], [x1 fliplr(x2)], 'g'); hold off;
8 件のコメント
Paul
2025 年 6 月 30 日
I don't understand what you want to do for the three-curve case. How should y3 = 2 influence the result?
その他の回答 (2 件)
the cyclist
2025 年 6 月 25 日
編集済み: the cyclist
2025 年 6 月 25 日
I think you got yourself -- and me -- confused by swapping the definitions of what are conventionally called the X- and Y-axis.
Is this what you want? Or did you want the lower one?
syms x y
% Note that I changed the function definitions in this line
x1=y^(1/2); x2=y^(1/3); y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
ylim('padded')
x1 = fp1.YData; % In these lines, you need to be careful that MATLAB calls
y1 = fp1.XData; % the horizontal axis "X", but that is what you call "Y".
x2 = fp2.YData;
y2 = fp2.XData;
patch([y1 fliplr(y2)], [x1 fliplr(x2)], 'g'); hold off;
0 件のコメント
the cyclist
2025 年 6 月 29 日
編集済み: the cyclist
2025 年 6 月 30 日
Another (I think simpler) method is to change the "view" of the axes.
This way the XData and YData properties of line up with what you expected. If you label your axes -- which you should -- then those do need to be swapped (because of the view change).
syms x y
x1=y^2; x2=y^3; y1=0; y2=1; % y1 & y2 are the boundaries
fp1 = fplot(x1, [y1 y2]); hold on; grid on;
fp2 = fplot(x2, [y1 y2]);
xlim('padded')
x1 = fp1.XData;
y1 = fp1.YData;
x2 = fp2.XData;
y2 = fp2.YData;
patch([x1 fliplr(x2)],[y1 fliplr(y2)],'g');
xlabel("y")
ylabel("x")
view([90 -90])
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






