Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
0

highlighting specific regions in a plot

asked 2 years ago

moon gravatar image

I'm working on displayind*g the regions where a specific condition between two functions. As an example I want to color or highlight in a way or another the areas where let's say where x/ f(x)<g(x)< p="">

f(x)=sin(2*x + 3)
g(x)=2*cos(2*x + 2)
plot([f(x),g(x)],[x,-pi,pi])

How can I approach that ? Thx for any kind of hint.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 2 years ago

achrzesz gravatar image

What about

f(x)=sin(2*x + 3)
g(x)=2*cos(2*x + 2)
h=g(x)-f(x)
x0=find_root(h, -3,-1)
x1=find_root(h, -1,0)
p1=plot([f(x),g(x)],[x,x0,x1],fill=True,fillcolor='lightgrey')
p2=plot([f(x),g(x)],[x,x0+pi,x1+pi],fill=True,fillcolor='lightgrey')
p=plot([f(x),g(x)],[x,-pi,pi])
p+p1+p2
Preview: (hide)
link

Comments

Excellent ! That's a very nice starting point for me. In fact we cannot identify where (in this example) f(x)=g(x) therefore a need to tell where to look at. May be making a loop over small intervals could work in simple case. This is by no mean a general solution by def. Thx a lot. appreciated !

moon gravatar imagemoon ( 2 years ago )

Examples like sin(1/x), cos(1/x) are overwhelming.

achrzesz gravatar imageachrzesz ( 2 years ago )
3

answered 2 years ago

Juanjo gravatar image

updated 2 years ago

To complement the answer by @achrzesz, let us point out two additional approaches to fill exactly the region between the curves y=f(x) and y=g(x) where f(x)g(x), with x in a given interval [a,b], that is, the region
{(x,y)R2axb, f(x)yg(x)}. First method:

f(x) = sin(2*x+3)
g(x) = 2*cos(2*x+2)
pf = plot(f(x), (x,-3,3), color="red", fill=max_symbolic(f(x),g(x)), 
          fillcolor="lightgreen")
pg = plot(g(x), (x,-3,3), color="blue")
show(pf+pg)

Second method:

var("x,y")
p = plot([f(x),g(x)], (x,-3,3), color=["red","blue"])
r = region_plot([f(x)<y, y<g(x)], (x,-3,3), (y,-2,2), 
                incol="lightgreen", plot_points=[100,100])
show(r+p)

See this SageMath Cell

Preview: (hide)
link

Comments

If the user doesn't want to know where the intersections are placed, the method by @Juanjo is nicer

achrzesz gravatar imageachrzesz ( 2 years ago )

your method assume that we know roughly where the solutions are. which I fully understanding. the one from from @Juanjo is likely based on numerical aprox. I might be wrong. In any case thx a lot for both !

moon gravatar imagemoon ( 2 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2 years ago

Seen: 317 times

Last updated: Jan 04 '23