필터 지우기
필터 지우기

Fill Area in matlab plot

조회 수: 18 (최근 30일)
Szczepan Letkiewicz
Szczepan Letkiewicz 2021년 12월 10일
댓글: Szczepan Letkiewicz 2021년 12월 10일
Hello. Im trying to fill an area between two curves in Matlab, from a certain x-value to another. The problem im encountering is that my fill area is being compressed, I think im going about this wrong. My idea was to also make two fill areas so that I could change their colors but I encountered this problem before hand. I have added my code below and a picture of what im trying to recreate for referance.
clear; clc; close all;
x = 0:0.01:5;
Y1= -0.2*x+4;
Y2= -0.2*x+2;
Y3= (nthroot((x-2.5),3))+2.5;
idx = find(Y2 - Y3 < eps, 1);
px = x(idx);
py = Y2(idx);
x_1 = linspace(0,px,501);
plot(x,Y1,'b',x,Y2,'r',x,Y3,'k');
hold on
fill([x_1 fliplr(x_1)],[Y3 fliplr(Y2)],'c')
My end goal is to replace the function "Y3" with a set of data that and have matlab plot the two areas. The lines in my code lablled as "Y1" and "Y2" are just example for now my plan is to calculate them once I get my data. If there is a way to make Matlab extrapolate them from the data that I will get then that would be amazing.
Thank you for the help in advance.

채택된 답변

KSSV
KSSV 2021년 12월 10일
x = 0:0.01:5;
Y1= -0.2*x+4;
Y2= -0.2*x+2;
Y3= (nthroot((x-2.5),3))+2.5;
idx = find(Y2 - Y3 < eps, 1);
px = x(idx);
py = Y2(idx);
x_1 = linspace(0,px,501);
x1 = 2.5*ones(1,10);
y1 = linspace(1,4,10) ;
plot(x,Y1,'b',x,Y2,'r',x,Y3,'k',x1,y1,'g');
hold on
A = InterX([x;Y2],[x;Y3]) ;
B = InterX([x;Y2],[x1;y1]) ;
C = InterX([x1;y1],[x;Y3]) ;
D = InterX([x1;y1],[x;Y1]);
E = InterX([x;Y1],[x;Y3]) ;
xi = linspace(A(1),E(1)) ;
yi = interp1(x,Y3,xi) ;
Rx = [A(1) B(1) D(1) E(1) fliplr(xi)] ;
Ry = [A(2) B(2) D(2) E(2) fliplr(yi)] ;
patch(Rx,Ry,'y')
  댓글 수: 1
Szczepan Letkiewicz
Szczepan Letkiewicz 2021년 12월 10일
Thank you so much!!
Works like a charm

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by