Hello,
I'd like to fill the gap between two a curve and a line, but only when the curve is above the line.
This is my actual code:
curve = F(:,2)';
line = 13;
plot(curve,'k');
hold on;
area(x,curve,line,'FaceColor','c','LineWidth',1);
yline(line, '--r',LineWidth=2)
Thankyou for your help!

 채택된 답변

Cris LaPierre
Cris LaPierre 2024년 1월 8일

1 개 추천

I think you could accomplish this by modifying the data you pass into area so that y is lever less that line.
max(curve,line)
For example:
x = 0:0.1:10;
curve = 5*sin(x) + 10;
line = 13;
plot(x,curve,'k');
hold on;
area(x,max(curve,line),line,'FaceColor','c','LineWidth',1);
hold off
yline(line, '--r',LineWidth=2)

댓글 수: 4

Cora Boriga
Cora Boriga 2024년 1월 8일
이동: Matt J 2024년 1월 8일
Thanks for your quick answer! When i try this, as you can see, the curve is choppy at the red dotted line
Admittedly, this is a pretty crude approach. It will be influenced by the resolution of your data. Consider this modification to my original example
% increased the step size
x = 0:0.5:10;
curve = 5*sin(x) + 10;
line = 13;
plot(x,curve,'k');
hold on;
area(x,max(curve,line),line,'FaceColor','c','LineWidth',1);
hold off
yline(line, '--r',LineWidth=2)
Note that this approach does not do any interpolation to find the crossing point. You may want to try upsampling your data as a way to decrease the error. Otherwise, you will need a method to approxmiate the (x,y) values of each crossing.
Cora Boriga
Cora Boriga 2024년 1월 11일
You were right! I tried to upsample my data and now it works much better. Thankyou very much.
Image Analyst
Image Analyst 2024년 1월 11일
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

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

추가 답변 (0개)

질문:

2024년 1월 8일

댓글:

2024년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by