2

I've just coded my finite difference solver in Python for the heat equation in the unit square, and in order to check an important property of the equation I need to give as initial data a discountinuous function.

My domain in [0,1] x [0,1] and I'd like to have a function like the following, but in 2D,i.e. a piecewise function of the variables (x,y).

The problem is that I tried to use numpy.piecewise, but it doesn't seem to work. Does anyone know how to build such an initial datum?

slamWolfen
  • 43
  • 4

1 Answers1

0

Maybe this:

import numpy as np
import matplotlib.pyplot as plt

number_of_desired_points = 20
x = np.linspace(0,1,number_of_desired_points)

def boxplot(x):
     y = [1 if (x[i] >0.4) and (x[i]<0.6) else 0 for i in range(len(x))]
     return y

y = boxplot(x)

plt.plot(x,y)
plt.show()

data2D = np.array([x,y]).T

enter image description here

EDIT

import numpy as np
import matplotlib.pyplot as plt

number_of_desired_points = 20
x = np.linspace(0,1,number_of_desired_points)

def boxplot(x):
     y = [1 if (x[i] >0.4) and (x[i]<0.6) else 0 for i in range(len(x))]
     return y

z = boxplot(x)
y=x[:]

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter(x,y,z)
plt.show()

enter image description here

seralouk
  • 30,938
  • 9
  • 118
  • 133
  • I was already able to do that... I need a function like this, but in 2d. It has the form of a "staircase" – slamWolfen Nov 23 '19 at 00:13
  • what do you mean when you say function in 2D. This answers gives you the x,y vectors that form a 2D grid – seralouk Nov 23 '19 at 10:40
  • I mean, the input of the function is 2D, i.e. $u(x,y)$ , so that the graph of $u$ is a 3D dimensional object – slamWolfen Nov 23 '19 at 10:41
  • I found this (https://stackoverflow.com/questions/38095362/python-plot-of-a-piecewise-defined-surface) post, which has almost the answer, but I can't adapt it to my case – slamWolfen Nov 23 '19 at 14:50