0

I have a 2D sinusoidal function and I want to plot its boundary threshold of t=0. Could someone give me a hint on how to do it?

f (x, y) = sin(10x) + cos(4y) − cos(3xy)

x ∈ [0, 1], y ∈ [0, 2], with a boundary threshold of t = 0

The expected plot should look like this: Plot A dashed lines

Actually the function I am referring to is a toy one from paper "Active Learning For Identifying Function Threshold Boundaries"(https://papers.nips.cc/paper/2005/file/8e930496927757aac0dbd2438cb3f4f6-Paper.pdf) Page 4 of that paper

Update: I tried the following code but apparently it does not give what I want. The top view is a straight line from (0,0) to (1,2), instead of some curves...

ax = plt.axes(projection='3d')

# Data for a three-dimensional line
xline = np.linspace(0, 1, 1000)
yline = np.linspace(0, 2, 1000)
zline = np.sin(10*xline)+np.cos(4*yline)-np.cos(3*xline*yline)
ax.plot3D(xline, yline, zline, 'gray')

1 Answers1

1

Welcome to stackoverflow. Your math is wrong. Your function f is a function of two variables, f(x, y). Hence you need to evaluate it on a grid (all combinations of valid x and y values), if you want to find the solutions for f = 0 computationally. Your code is currently evaluating f only on the y = 2x axis (hence the "straight line from (0,0) to (1, 2) in top-down view").

import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    return np.sin(10*x)+np.cos(4*y)-np.cos(3*x*y)

x = np.arange(0, 1, 1e-3)
y = np.arange(0, 2, 1e-3)

XX, YY = np.meshgrid(x, y)
ZZ = f(XX, YY)

plt.contour(XX, YY, ZZ, levels=[0.])
plt.show()

enter image description here

Paul Brodersen
  • 11,221
  • 21
  • 38
  • Thank you so much for your help! The graph looks pretty good to me. I am going to have a closer look at your answers. – Haney Malloc Mar 12 '21 at 00:28