how to find the area between sinx and sin2x with plot on range [0,pi] as from this code its giving negative area,?
3 次查看(过去 30 天)
显示 更早的评论
clear all
clc
syms x
f=input('enter the upper curve f(x):');
g=input('enter the lower curve g(x):');
L=input('enter the limits of integration for x [a,b]:');
gt0 = y>0;
posArea = trapz(x(gt0), y(gt0));
negArea = trapz(x(~gt0), y(~gt0));
a=L(1);b=L(2);
area1=int(f-g,x,a,b);
disp(['area bounded by the curves f(x) and g(x) is:',char(area1)]);
x1=linspace(a,b,20);y1=subs(f,x,x1);
x2=x1;y2=subs(g,x,x1);
plot(x1,y1);hold on;plot(x2,y2);hold off;
xlabel('x-axis');ylabel('y-axis');
legend ('f(x)','g(x)');grid on;
0 个评论
采纳的回答
Paul
2022-10-28
Hi Aashay,
After commenting out the code for pos and negArea that wouldn't run because y is not defined (and even if was defined, it's not clear what the trapz() commands are trying to do by indexing into an sym variable), we get that area between the curves is 2.
clear all
clc
syms x
f=sin(x);
g=sin(2*x);
L=[0 sym(pi)];
%gt0 = y>0;
%posArea = trapz(x(gt0), y(gt0));
%negArea = trapz(x(~gt0), y(~gt0));
a=L(1);b=L(2);
area1=int(f-g,x,a,b);
disp(['area bounded by the curves f(x) and g(x) is:',char(area1)]);
But because the curves cross, isn't the formula supposed to be:
area1=int(abs(f-g),x,a,b);
disp(['area bounded by the curves f(x) and g(x) is:',char(area1)]);
% x1=linspace(a,b,20);y1=subs(f,x,x1);
% x2=x1;y2=subs(g,x,x1);
% plot(x1,y1);hold on;plot(x2,y2);hold off;
% xlabel('x-axis');ylabel('y-axis');
fplot([f g],double(L))
legend ('f(x)','g(x)');grid on;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Algebra 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!