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):');
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;

采纳的回答

Paul
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)]);
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;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by