Fill area between different curves

조회 수: 10 (최근 30일)
mikymike89
mikymike89 2018년 3월 8일
댓글: Star Strider 2018년 3월 13일
I cannot find a command to fill the area between the maximum and minimum values of these curves:
x1 = [0 1 2.2 4];
y1 = [0.5 2 1.8 1];
x2 = [0 1.2 2 4];
y2 = [0 1 2 1.5];
x3 = [0 1 3 3.5];
y3 = [0 1 2 1.5];
figure
plot(x1,y1);
hold on
plot(x2,y2);
hold on
plot(x3,y3);
I attached an image for better understand my request. Unfortunately, I found a solution just if Y is depending of X (e.g. : https://www.mathworks.com/matlabcentral/answers/180829-shade-area-between-graphs)
  댓글 수: 2
Star Strider
Star Strider 2018년 3월 8일
So your problem is now solved?
mikymike89
mikymike89 2018년 3월 8일
no. My case is different from the e.g. found. I have "y" not depending of "x"

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

채택된 답변

Star Strider
Star Strider 2018년 3월 8일
It’s necessary to create common (x,y) values for all the curves with interpolation. Then the solution is straightforward.
If I understand correctly what you want, this works:
x1 = [0 1 2.2 4];
y1 = [0.5 2 1.8 1];
x2 = [0 1.2 2 4];
y2 = [0 1 2 1.5];
x3 = [0 1 3 3.5];
y3 = [0 1 2 1.5];
xm = [x1; x2; x3]; % Create ‘X’ Matrix
xl = [min(xm(:)) max(xm(:))];
xu = linspace(xl(1), xl(2), 1000); % Create Sorted Interpolation Vector
ym = [y1; y2; y3]; % Create ‘Y’ Matrix
for k1 = 1:size(xm,1)
yi(k1,:) = interp1(xm(k1,:), ym(k1,:), xu, 'linear','extrap'); % Interpolate To Provide Common (x,y) Values
end
ymin = min(yi);
ymax = max(yi);
figure
patch([xu(:)' fliplr(xu(:)')], [ymin fliplr(ymax)], [0.1 0.9 0.1], 'FaceAlpha',0.3) % Plot Filled Background
hold on
plot(x1,y1)
plot(x2,y2)
plot(x3,y3)
  댓글 수: 10
mikymike89
mikymike89 2018년 3월 13일
So, isn't there a specific command to trim data on a certain line (for which I am sure that all data has the same length)? This is because I've more than thousands of data and I would like to avoid checking them manually. Moreover, I'm asking you this because I don't need all the data and I could use less; if there is a difference of vector length is maximum of ten line.
Star Strider
Star Strider 2018년 3월 13일
There’s no one specific command. You would first need to use the size function to determine the vector sizes. Then compare the row size (dimension 1, so size(data,1) if ‘data’ is your array name), then compare them.
One way to do the initial check is:
sz1 = 200; % Row Size Of First Data Set
sz2 = 201; % Row Size Of Second Data Set
sz3 = 211; % Row Size Of Third Data Set
sz_eq = isequal(sz1, sz2, sz3); % If All Are Equal=1, Not Equal = 0
min_rows = min([sz1 sz2 sz3]); % Minimum Row Size
max_rows = max([sz1 sz2 sz3]); % Maximum Row Size
You can of course define other criteria. Then trim your data matrices as you wish, the only constraint being that the ‘trimmed’ row size is less than ‘min_rows’.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by