how to find the area between sinx and sin2x with plot on range [0,pi] as from this code its giving negative area,?
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Paul
on 28 Oct 2022
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;
More Answers (0)
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!