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 Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by