Q:

How to create a 3D object from a specific equation

I realize this has been asked before, so I apologize for the "repeat". I saw this answer, and it mostly solved my issue. However, I can't figure out how to generate my specific equation. This is my equation:

This works from the range -1 <= x <= 1. For the really big equation, the "x" should actually be z, and the "h" should be x (I had to use those to graph it on Desmos). In any case, I tried inputting the first half of the equation with the Blender Add Mesh: Extra Objects > Math Function > Z Math Surface by typing this:
(sqrt(-16*fabs(x)+40*sqrt(fabs(x))*y-16*pow(x,2)-25*pow(y,2)+16)+15*x)/15

The result is an error evaluating the expression. Do I have to set the range somehow? Would it be better to use XYZ Math Surface? (and if so, how?). Or am I just doing something wrong? Thank you in advance.

A:

The problem that prevents your equation from working with the Add Mesh: Extra Objects > Math Function > Z Math , is that you're using a sqrt function which is undefined for negative values.
When the math function operator tries to evaluate different values for X and Y, it raises an exception the first time that evaluation fails due to an attempt to sqrt a negative value.
Another option is to write a script of your own to generate the mesh only where the equation provides valid results. I wrote a quick script to do that below. It's incomplete as it only calculates vertices coordinates (result in the image above), and does not generate edges and faces, but it's a start:
import bpy, bmesh
import numpy     as np
from   math      import sqrt
from   itertools import product

# Define X and Y values lower bound, upper bound and number of points
lb, ub, n = ( -30, 30, 1000 )

# Calculate all the possible X-Y combinations and convert to a numpy array
xy = np.array( list( product( np.linspace( lb, ub, n ), np.linspace( lb, ub, n ) ) ) )

# Split X and Y to different arrays and initialize Z array
xs = xy[:,0]
ys = xy[:,1]
zs = []

# Iterate over every x and y value, and try to calculate the z.
# Append result if successful, and nan if not
for x, y in zip( xs, ys ):
    try:
        zs.append(
            ( 
                sqrt( 
                    -16 * abs(x) + 40 * sqrt( abs(x) ) * y - 16 * pow( x, 2 ) - 25 * pow( y, 2 ) + 16 
                ) + 15 * x 
            ) / 15 
        )
    except:
        zs.append( np.nan )

# Drop nan values from all axes
mask = np.logical_not(np.isnan(zs))
xs = xs[ mask ]
ys = ys[ mask ]
zs = np.array( zs )[ mask ]

# Generate mesh
bm = bmesh.new()

# Add vertices
for x, y, z in zip( xs, ys, zs ):
    bm.verts.new( (x,y,z) )

m = bpy.data.meshes.new( "equation" )

bm.to_mesh( m )

o = bpy.data.objects.new( "equation", m )
bpy.context.scene.objects.link( o )

This script uses external, non built-in libraries (namely numpy). You can achieve the same result without numpy, but it makes some of the calculations quicker and easier.

A:

As @TLousky quite rightly points out, the problem is due to the evaluation of the equation throwing an exception due to the sqrt(...) function attempting to find the square root of a negative number (which doesn't have a 'real' answer).
To avoid the Z Equation from hitting this problem you can use a conditional assignment.
In Python you can assign a variable based on whether a certain condition is met as follows :
myvar = <value> if <condition> else <othervalue>

Where value is the value to assign if the condition is True and othervalue is the value to assign if the condition is False. The same style of conditional assignment can be used in Blender for the Z Equation.
In your example, the failure occurs because the contents of the initial sqrt(...) function evaluates below zero, so the condition to identify 'valid' values should be :
(-16*fabs(x)+40 * sqrt(fabs(x)) * y - 16 * pow(x,2) - 25 * pow(y,2) + 16)>=0.0 

The value to assign if this condition is True would be :
( sqrt(-16*fabs(x)+40 * sqrt(fabs(x)) * y - 16 * pow(x,2) - 25 * pow(y,2) + 16)  + 15 * x)/15

And the value to assign if the condition is False would be :
(0 + 15 * x)/15

(ie, for values < 0 assume that sqrt(...) is zero)
Putting this all together, the Z Equation should be :
( sqrt(-16*fabs(x)+40 * sqrt(fabs(x)) * y - 16 * pow(x,2) - 25 * pow(y,2) + 16)  + 15 * x)/15 if (-16*fabs(x)+40 * sqrt(fabs(x)) * y - 16 * pow(x,2) - 25 * pow(y,2) + 16)>=0.0 else (0 + 15 * x)/15

And this should produce the following result :

