Python Forum

Full Version: fsolve over meshgrid
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Consider the equation given in the function eq. The aim is to solve the equation for z (which becomes a surface in 3-space) and plot it in a 3d plot. How would one solve for values of z over the meshgrid X,Y? The code below only gives the values of z along the line y=x.

import numpy as np
from mpl_toolkits import mplot3d
from scipy.optimize import fsolve

def eq(z,x,y):
    return x+2*y+z+np.exp(2*z)-1
X=Y=np.linspace(0,2,3)
X,Y=np.meshgrid(X,Y)
Z=[]
for x,y in zip(X,Y):
    Z.append(fsolve(eq,1,args=(x,y))[0])
    
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, np.asarray(Z), alpha=0.5);