Finding area between 2 curves

조회 수: 8 (최근 30일)
Sacchin Sundar
Sacchin Sundar 2020년 11월 28일
댓글: Sacchin Sundar 2020년 12월 4일
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

답변 (1개)

Shubham Rawat
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!
  댓글 수: 1
Sacchin Sundar
Sacchin Sundar 2020년 12월 4일
Hey Shubham This code does work for 2 curves which intersect 3 times but the problem is it would not work if the curves intersect more. I was not able to figure out how to fill inside a for loop

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by