Ali
Ali

Reputation: 243

How to find the y-intercept using sympy?

x = Symbol('x')
f = x**2-3

def return_y_intercept(f):
   return [the y-intercepts]

How is it possible using something like the structure above write a function that returns the y-intercepts of it's argument?

Upvotes: 1

Views: 1970

Answers (2)

asmeurer
asmeurer

Reputation: 91580

The y-intercept just means that you substitute 0 for x, so just do f.subs(x, 0).

Upvotes: 2

user1786283
user1786283

Reputation:

Try using sympy.coeff, here, so like this:

Y-intercept as Coordinates

from sympy import Symbol

x = Symbol('x')
f = x**2-3

def return_y_intercept(f):
   return [0,f.coeff(x,0)] #return coordintes of y-intercept

print return_y_intercept(f)

Output:

0,-3

Y-intercept:

from sympy import Symbol

x = Symbol('x')
f = x**2-3

def return_y_intercept(f):
   return [f.coeff(x,0)] #return just the y-intercept

print return_y_intercept(f)

Output:

-3

try it on the online sympy interpreter here

Upvotes: 2

Related Questions