Finding area between 2 curves
10 次查看(过去 30 天)
显示 更早的评论
So I am trying to figure out a code which does the following:
Gets 2 functions from the user
finds out the area of intersection no matter how many finite intersections
you do not have to specify what is the upper and lower curve
fills in the area with a specific colour
Code: What it doesnt do is figure out how to find out upper and lower curve on its own plus doesnt do it for multiple intersections
clc
clear
syms x
y1=input('enter the upper curve as a function of x : ')
y2=input('enter the lower curve as a function of x : ')
t=solve(y1-y2);
t=double(t);
a=int(y1-y2,t(1),t(2))
d=[t(1)-0.2 t(2)+0.2]
ez1=ezplot(y1,d);
set(ez1,'color','r')
hold on
ez2=ezplot(y2,d);
legend('y1','y2')
xv=linspace(t(1),t(2));
y1v=subs(y1,x,xv)
y2v=subs(y2,x,xv)
x=[xv,xv];
y=[y1v,y2v];
fill(x,y,'g')
grid on
0 个评论
回答(1 个)
Shubham Rawat
2020-12-2
Hi Sacchin,
I have reproduce your code and I have made some changes such that it work for all intersections. For upper and lower functions they may vary. So i used fill command 2 times to overcome that.
syms x
y1 = input("enter upper function of x: "); %here i used x^3
y2 = input("enter lower function of x: "); %here i used x
t = solve(y1 - y2);
t = double(t);
%changes below
d = t;
d(1) = d(1)-0.2; %subtract 0.2 in first
d(end) = d(end)+0.2; %add 0.2 in last
ez1 = ezplot(y1,d);
set(ez1, 'color', 'r')
hold on
ez2 = ezplot(y2,d);
legend('y1','y2');
xv = linspace(t(1),t(end)); %changes here first to last
y1v = subs(y1,x,xv);
y2v = subs(y2,x,xv);
%fill command using twice to fill all areas
x = [xv,xv];
y = [y1v,y2v];
fill(x,y,'g');
y = [y2v,y1v];
fill(x,y,'g');
grid on;
Hope this Helps!
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!