highlighting specific regions in a plot

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.

edit retag close merge delete

Sort by ยป oldest newest most voted

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

more

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 !

( 2023-01-02 18:54:55 +0100 )edit

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

( 2023-01-02 20:31:25 +0100 )edit

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)\leq g(x)$, with $x$ in a given interval $[a,b]$, that is, the region
$$\{(x,y)\in\mathbb{R}^2 \mid a\leq x\leq b,\ f(x)\leq y\leq g(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

more

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

( 2023-01-04 10:12:37 +0100 )edit

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 !

( 2023-01-04 13:52:17 +0100 )edit