Matlab contour plot going to infinity for finite function $f(x_1,x_2)=e^x_1-(x_1x_2)+x_2^2$

graphing-functionsMATLABnonlinear optimizationoptimization

The following function that we wanted to plot was not appearing on the plot properly. The function is as follows:
$$f(x_1,x_2)=e^{x_1}-(x_1x_2)+x_2^2$$
Since the function is parabolic, exponential, and rectangular hyperbolic the function should have some curves associated with it and not straight lines. What could be the possible error and here is the matlab code.

i=linspace(-200,200);
j=linspace(-200,200);
[X,Y]= meshgrid(i,j);
Z = exp(X)-(X*Y)+(Y^2);
contour(X,Y,Z)

Contour plot for the above mentioned function

Best Answer

The problem is the range of your function, e.g. take the first term, it will change from $e^{-200}$ to $e^{200}$ that is a factor $e^{400} \sim 10^{173}$, which obviously is hard to resolve.

Here's an example with a smaller range

enter image description here

EDIT This is the code I used to generate the plot above, it is python's matplotlib which provides a MATLAB-like interface

import numpy as np
import matplotlib.pyplot as plt

xmin, xmax = -2.0, 2.0
ymin, ymax = -2.0, 2.0

x, y = np.meshgrid(np.linspace(xmin, xmax, 50, endpoint = True), np.linspace(ymin, ymax, 50, endpoint = True))
z = np.exp(x) - x * y + y * y
plt.contour(x, y, z, color = 'k')
plt.imshow(z, cmap='jet', extent=(xmin, xmax, ymin, ymax), origin = 'lower', alpha = 0.3)

plt.show()