MATLAB Answers

how to fill between 2 graphs

2 views (last 30 days)
halil hasan
halil hasan on 10 Apr 2020
Commented: Les Beckham on 14 Apr 2020
Hi eveybody
I could not fill the gap betwween 2 curves. it is shown in below figure.
can anyone help me?
thanks
The code I used is:
plot(KI1,KP1);
xlabel('K_I');
ylabel('K_P');
grid on
hold on
x2=zeros(1,150)
y=linspace(-20,40,numel(x2))
plot(x2,y,'linewidth',2)

  6 Comments

Show 3 older comments
halil hasan
halil hasan on 12 Apr 2020
thanks drova
I have given the code above.
darova
darova on 12 Apr 2020
I have an error here
Do you know why?
halil hasan
halil hasan on 13 Apr 2020
KI1 and KP1 are vectors. they can have any value.

Sign in to comment.

Accepted Answer

Les Beckham
Les Beckham on 11 Apr 2020
What do you mean by "fill the gap"? Do you want to connect the start and end points so that this becomes a closed curve? If so, this should do it:
plot([KI1(:); KI1(1)], [KP1(:); KP1(1)]);
xlabel('K_I');
ylabel('K_P');
grid on

  7 Comments

Show 4 older comments
Les Beckham
Les Beckham on 13 Apr 2020
Maybe this will do what you want:
KI1 = [0 -190 -100 0 90 80];
KP1 = [40 5 -18 -16 -5 -2];
idx = find(KI1 <= 0); % find indices of non-positive KI1 values
fill([KI1(idx) KI1(find(idx,1))], [KP1(idx) KP1(find(idx,1))], 'b', 'EdgeColor', 'None')
hold on
plot([KI1(:); KI1(1)], [KP1(:); KP1(1)], 'k', 'LineWidth', 2)
xlabel('KI1')
ylabel('KP1')
halil hasan
halil hasan on 14 Apr 2020
Thanks alot for your kind replies, Beckham
Les Beckham
Les Beckham on 14 Apr 2020
You are welcome. Glad I could help.

Sign in to comment.

More Answers (1)

darova
darova on 13 Apr 2020
Here is my shot
y = 0:0.1:pi;
x1 = -sin(y);
x2 = y*0-0.3;
[xc,yc] = polyxpoly(x1,y,x2,y);
xx = [xc(1) x1(x1<x2) xc(end)];
yy = [yc(1) y(x1<x2) yc(end)];
plot(x1,y,x2,y)
patch(xx,yy,'r')
line(xc,yc,'marker','o')
success?

  1 Comment

halil hasan
halil hasan on 14 Apr 2020
Thanks alot for your kind reply, Darova

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by