Fill Area in matlab plot

16 vues (au cours des 30 derniers jours)
Szczepan Letkiewicz
Szczepan Letkiewicz le 10 Déc 2021
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.

Réponse acceptée

KSSV
KSSV le 10 Déc 2021
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 commentaire
Szczepan Letkiewicz
Szczepan Letkiewicz le 10 Déc 2021
Thank you so much!!
Works like a charm

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by