how to find the area between sinx and sin2x with plot on range [0,pi] as from this code its giving negative area,?

8 views (last 30 days)
clear all
clc
syms x
f=input('enter the upper curve f(x):');
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
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;

Accepted Answer

Paul
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)]);
area bounded by the curves f(x) and g(x) is:2
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)]);
area bounded by the curves f(x) and g(x) is:5/2
% 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)

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by