5

I want to plot the following field equations:

  • dx/dt = x*(4*y+3*x-3)
  • dy/dt = y*(4*y+3*x-4)

but I do not know how can I restrict the boundary to a triangle: x>=0, y>=0, x<=1-y:

enter image description here

# stream plot with matplotlib
import numpy as np
import matplotlib.pyplot as plt
def velocity_i(x,y):
    vx = x*(3*x+4*y-3)
    vy = y*(3*x+4*y-4)
    return vx, vy
n=100
x = np.linspace(0, 1, n)
y = np.linspace(0, 1, n)
X, Y = np.meshgrid(x, y)
Ux, Uy = velocity_i(X, Y)
vels = (Ux**2+Uy**2)**0.5
plt.figure(figsize=(5,4))
stream = plt.streamplot(X, Y,
              Ux,Uy,
              arrowsize=1,
              arrowstyle='->',
              color= vels,
              density=1,
              linewidth=1,
                       )
plt.xlabel(r"$\Omega_{\rm m}$",fontsize='14')
plt.ylabel(r"$\Omega_{\rm r}$",fontsize='14')
plt.colorbar(stream.lines)

plt.xlim((-.05,1.05))
plt.ylim((-.05,1.05))
plt.show()
3
  • Please select the posted answer Commented Jan 2, 2019 at 13:30
  • @MadPhysicist: I have reminded the OP couple of times as he seemed a new user. But he asked me a new question on 3d which is a completely different task. So I quit asking :) Commented Jan 2, 2019 at 13:34
  • 1
    @Bazingaa. Positive reinforcement doesn't always work :) Commented Jan 2, 2019 at 13:40

1 Answer 1

4

This is quite straightforwardly achievable using NumPy masking and np.where function. I am only showing the relevant two lines of code (highlighted by a comment) needed to get the job done.

Explanation: X<=1-Y checks your required boundary condition and then at all those indices where this condition holds True, it assigns the actual computed value of Ux (or Uy) and at indices where the condition is False, it assigns 0. Here X<=1-Y acts as kind of a conditional mask.

Ux, Uy = velocity_i(X, Y)
Ux = np.where(X<=1-Y, Ux, 0) # <--- Boundary condition for Ux
Uy = np.where(X<=1-Y, Uy, 0) # <--- Boundary condition for Uy
vels = (Ux**2+Uy**2)**0.5

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Then, how can we plot 3d: dx/dt = x*(4*y+3*x+2*z-3) , dy/dt = y*(4*y+3*x+2*z-4) , dz/dt = z*(4*y+3*x+2*z-2) , for between x,y,z>=0 and x+y+z=1 ?
That's a new problem. You wanted to get the plot similar to what you posted in the question. I helped you with that. Post a new question for a 3d. Show in your new question, what is the expected 3d output. On Stack overflow you ask one question at a time. Plotting 3d would require some thoughts and efforts. People will help you if you first show them your attempt. No one will write a code from scratch for you.
Would it be useful to select X and Y before you compute Ux and Uy to begin with?
@MadPhysicist: I tried that using Ux, Uy = velocity_i(X[X<=1-Y], Y[X<=1-Y]) but then X and Y are meshgrid and so for me the straightforward solution was to simply assign 0 values later. Choosing X, Y = np.meshgrid(x[x<1-y], y[x<1-y]) would be wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.