Ask Your Question
0

highlighting specific regions in a plot

asked 2023-01-01 15:29:08 +0100

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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2023-01-01 20:35:33 +0100

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
edit flag offensive delete link more

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 ( 2023-01-02 18:54:55 +0100 )edit

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

achrzesz gravatar imageachrzesz ( 2023-01-02 20:31:25 +0100 )edit
3

answered 2023-01-04 01:41:24 +0100

Juanjo gravatar image

updated 2023-01-04 01:43:51 +0100

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

edit flag offensive delete link more

Comments

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

achrzesz gravatar imageachrzesz ( 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 !

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

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: 2023-01-01 15:29:08 +0100

Seen: 153 times

Last updated: Jan 04 '23