The above is my code for area under two curves but it does not fill all the area but only the one below x axis. why? also why did we take x = [xv xv]
why full area not filled? area under the curve
5 次查看(过去 30 天)
显示 更早的评论
clc
clear all
close all
syms x
y1 = 7-2*x^2;
y2 = x^2+4;
t = solve(y1-y2);
t = double(t);
f = int(y1-y2,min(t),max(t));
D = [(min(t)-2),(max(t)+2)];
p1 = ezplot(y1,D)
set(p1,'color','r')
hold on
p2 = ezplot(y2,D)
set(p2,'color','b')
xv = linspace(min(t),max(t));
y1v = subs(y1,x,xv);
y2v = subs(y2,x,xv);
x = [xv,xv];
y = [y1v,y2v];
fill(x,y,'g')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1192858/image.png)
采纳的回答
Star Strider
2022-11-14
For the fill (and patch) functions it is necessary to create a closed area. One way to do that is to flip the second vector in ‘x’ and ‘y’ vectors.
Then, it works —
% clc
% clear all
% close all
syms x
y1 = 7-2*x^2;
y2 = x^2+4;
t = solve(y1-y2);
t = double(t);
f = int(y1-y2,min(t),max(t));
D = [(min(t)-2),(max(t)+2)];
p1 = ezplot(y1,D)
set(p1,'color','r')
hold on
p2 = ezplot(y2,D)
set(p2,'color','b')
xv = linspace(min(t),max(t));
y1v = subs(y1,x,xv)
y2v = subs(y2,x,xv)
x = [xv,flip(xv)];
y = [y1v,flip(y2v)];
fill(x,y,'g')
Depending on the vectors, it may be necessary to use fliplr or flipud andif the vectors are column vectors, vertically concatenate them with the flipped second vectors.
.
3 个评论
Star Strider
2022-11-14
As always, my pleasure!
It is necessary to create a closed contour. The first set (x1,y1) creates the lower half of this figure, and the the second set (x2,y2) creates the second half. It is necessary to first trace out the first half, and then return by starting at the end of the first set and then going back to the origin by reversing the elements of the vectors in the second set,
Consider this —
x1 = [1 2 3 4 5];
y1 = [4 2 1 2 4];
x2 = [1 2 3 4 5];
y2 = [4 8 16 8 4];
figure
fill(x1, y1, 'g')
title('Lower Half')
figure
fill(x2, y2, 'r')
title('Upper Half')
figure
fill([x1 flip(x2)], [y1 flip(y2)], 'b')
g1 = gradient(y1) ./ gradient(x1);
g2 = gradient(flip(y2)) ./ gradient(flip(x2));
figure
plot([x1 flip(x2)], [y1 flip(y2)], '-k')
hold on
quiver(x1, y1, gradient(x1), g1, 0.5, '-g', 'LineWidth',1)
quiver(flip(x2), flip(y2), -gradient(x2), -(g2), 0.5,'-r', 'LineWidth',1)
hold off
The quiver plot sort of illustrates the approach to create a closed curve.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!