How to color an area at plot surface...

How can i color the area between 2 functions like
f(x)=x^2
g(x)=-x^2+1

 採用された回答

Thomas
Thomas 2013 年 4 月 18 日

1 投票

I found a better way... try
x=1:10;
f=x.^2;
g=x.^2+1;
patch([x x(end:-1:1)], [f g(end:-1:1)],'y')
This sets up x=1...10, g=x² and f=x²+1 and then plots the area between the curves as a yellow polygon/patch. Compare the matlab help for patch.
Here i simply draw all points forf first in positive x direction and then jump to the last point of g and draw the points of g backwards from x=10...1. All in all a complete polygon. Btw, 'y' is the color yellow.

3 件のコメント

Masoud Ghanbari
Masoud Ghanbari 2013 年 4 月 18 日
Hi
It worked but i cant understand the code
for example what if we want to color the area like f-g<2 ???
Thomas
Thomas 2013 年 4 月 18 日
i dont understand what you mean by f-g<2. You have to e.g. put the vector with the upper border curve values in f and the lower border data into g (or the other way round, it doesnt matter) and then call the patch command.
Masoud Ghanbari
Masoud Ghanbari 2013 年 4 月 19 日
Thanks... Hope to support me at my further problems

サインインしてコメントする。

その他の回答 (3 件)

Thomas
Thomas 2013 年 4 月 17 日

0 投票

You could use sth like
area(g);
hold on;
area(f);
and then select f's fill color to be the backgroud color.
Of coures this only works as long as f<g for all x.
If this is not the case you could first do
upperCurve = max(f,g)
lowerCurve = min(f,g)
and then plot upperCurve and lowerCurve as stated above. If they are sufficiently high-res compared to your plot resolution it might be sufficient, however it wouldn't be "clean". Maybe there is another solution anyone can bring up...
Masoud Ghanbari
Masoud Ghanbari 2013 年 4 月 18 日

0 投票

hi
It does not works
would you please bring me the full code
Muruganandham Subramanian
Muruganandham Subramanian 2013 年 4 月 18 日

0 投票

hi,
for e,g.
x=[1 2 3 4 5];
for i=1:numel(x)
f(i)=x(i)^2;
g(i)=-x(i)^2+1;
end
plot(x,f,x,g)
hold on;
area(f-g)
hold on;
area(g-f)
Do you expecting like this?

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by