Shading with vertical lines

조회 수: 3 (최근 30일)
Ikenna Iwudike
Ikenna Iwudike 2022년 4월 24일
댓글: Ikenna Iwudike 2022년 4월 25일
I'm trying to shade the region over which the integral extends with vertical lines. This is my integral:
F = @(x,y) x.*y;
ymin = @(x) x.^2;
ymax = @(x) x;
q = integral2(F,0,1,ymin,ymax)
The code below produces a graph with vertical line shading over its region. This is an example of what i'm trying to do.
f = @(x) sin(x + 1); g = @(x) x.^3 - 3*x + 1;
fplot(f, [-3, 3]), hold on
fplot(g, [-3, 3], 'LineWidth', 2)
>> x1 = fzero(@(x) f(x) - g(x), -2);
x2 = fzero(@(x) f(x) - g(x), 0);
x3 = fzero(@(x) f(x) - g(x), 2);
>> xcoord = linspace(x1, x3, 10);
ycoord = [f(xcoord); g(xcoord)];
plot([xcoord;xcoord], ycoord), hold off
  댓글 수: 5
Ikenna Iwudike
Ikenna Iwudike 2022년 4월 25일
Yea, let me see how a surface plot would look. Also yes I was talking about the first block.
Rik
Rik 2022년 4월 25일
See if my edited answer works for you. The loop can be optimized to plot all stems as a single object, but I first wanted to create it like this.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2022년 4월 25일
편집: Rik 2022년 4월 25일
I have put some inefficient code at the start to integrate over y so you end up with a function of x alone. I then used the same g function as you used in your second block. Is this what you mean?
F = @(x,y) x.*y;
ymin = @(x) x.^2;
ymax = @(x) x;
f_base =@(x) integral(@(y) F(x,y),ymin(x),ymax(x));
f=@(x) arrayfun(f_base,x);
g = @(x) x.^3 - 3*x + 1;
fplot(f, [-2, 2]), hold on
fplot(g, [-2, 2], 'LineWidth', 2)
x1 = fzero(@(x) f(x) - g(x), -2);
x2 = fzero(@(x) f(x) - g(x), 0);
x3 = fzero(@(x) f(x) - g(x), 2);
xcoord = linspace(x1, x3, 100);
ycoord = [f(xcoord); g(xcoord)];
plot([xcoord;xcoord], ycoord,'k'), hold off
If you want to plot a surface, you can use surf. plot3 will allow you to plot a line in a 3D axes.
F = @(x,y) x.*y;
ymin = @(x) x.^2;
ymax = @(x) x;
[X,Y]=ndgrid(linspace(0,1,100));
Y(Y<ymin(X))=NaN;
Y(Y>ymax(X))=NaN;
Z=F(X,Y);
surf(X,Y,Z)
Az=-5;El=65;view(Az,El) % set azimuth and elevation
hold on
for n=1:numel(Z)
if isnan(Z(n)),continue,end
plot3(X(n)*[1 1],Y(n)*[1 1],[0 Z(n)],'k')
end
  댓글 수: 1
Ikenna Iwudike
Ikenna Iwudike 2022년 4월 25일
This is great. thanks so much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by