Try this script. Notice that your $f(x)$ function is in the get_graph_y
definition which accepts an x
value input in the range $2-8$ and computes its corresponding y
value from the function. You can change the range and the function to anything as long as its of the form $y=y(x)$ and the script will plot the graph.
import bpy
def get_object(name):
objects = bpy.context.scene.objects
if name in objects:
return objects[name]
m = bpy.data.meshes.new(name + "-mesh")
o = bpy.data.objects.new(name, m)
#o.modifiers.new(name, 'SKIN')
bpy.context.collection.objects.link(o)
return o
def get_range(start, end, step = 2):
return [v * 0.1 for v in range(start * 10, end * 10, step)]
def get_graph_y(x):
return 0*x**3 - 0.09*x**2 + 0.42*x + 0.49
def draw_graph():
verts = []
for px in get_range(2, 8):
py = get_graph_y(px)
pz = 0
verts.append([px, py, pz])
edges = []
for i in range(len(verts)-1):
edges.append((i, i+1))
o = get_object("graph")
m = o.data
m.clear_geometry()
m.from_pydata(verts, edges, ())
draw_graph()
Take note that you can also easily plot this using Geometry Nodes. I'll update my answer with a GN solution later.