why full area not filled? area under the curve

4 visualizaciones (últimos 30 días)
Dhairya
Dhairya el 14 de Nov. de 2022
Comentada: Dhairya el 14 de Nov. de 2022
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')
  1 comentario
Dhairya
Dhairya el 14 de Nov. de 2022
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]

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 14 de Nov. de 2022
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)
p1 =
Line with properties: Color: [0 0.4470 0.7410] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [-3 -2.9866 -2.9731 -2.9597 -2.9462 -2.9328 -2.9193 -2.9059 -2.8924 -2.8790 -2.8655 -2.8521 -2.8386 -2.8252 -2.8117 -2.7983 -2.7848 -2.7714 -2.7579 -2.7445 -2.7310 -2.7176 -2.7042 -2.6907 -2.6773 -2.6638 -2.6504 -2.6369 -2.6235 … ] YData: [-11 -10.8390 -10.6787 -10.5191 -10.3603 -10.2022 -10.0448 -9.8881 -9.7322 -9.5770 -9.4225 -9.2687 -9.1156 -8.9633 -8.8117 -8.6608 -8.5106 -8.3612 -8.2125 -8.0645 -7.9172 -7.7707 -7.6249 -7.4798 -7.3354 -7.1918 -7.0488 -6.9066 … ] Show all properties
set(p1,'color','r')
hold on
p2 = ezplot(y2,D)
p2 =
Line with properties: Color: [0.8500 0.3250 0.0980] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [-3 -2.9866 -2.9731 -2.9597 -2.9462 -2.9328 -2.9193 -2.9059 -2.8924 -2.8790 -2.8655 -2.8521 -2.8386 -2.8252 -2.8117 -2.7983 -2.7848 -2.7714 -2.7579 -2.7445 -2.7310 -2.7176 -2.7042 -2.6907 -2.6773 -2.6638 -2.6504 -2.6369 -2.6235 … ] YData: [13 12.9195 12.8394 12.7596 12.6802 12.6011 12.5224 12.4441 12.3661 12.2885 12.2112 12.1343 12.0578 11.9816 11.9058 11.8304 11.7553 11.6806 11.6062 11.5323 11.4586 11.3853 11.3124 11.2399 11.1677 11.0959 11.0244 10.9533 10.8826 … ] Show all properties
set(p2,'color','b')
xv = linspace(min(t),max(t));
y1v = subs(y1,x,xv)
y1v = 
y2v = subs(y2,x,xv)
y2v = 
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 comentarios
Star Strider
Star Strider el 14 de Nov. de 2022
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.
.
Dhairya
Dhairya el 14 de Nov. de 2022
ohh thanks a lot sir, i understand it now
also if possible one last request, i have an exam tomorrow so please tell me about this
https://uk.mathworks.com/matlabcentral/answers/1850773-not-able-to-understand-code-for-volume-of-solid-by-revolution

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Particle & Nuclear Physics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by