The following challenges build on what you’ve learned in this notebook. You cand find sample solutions in the solutions notebook.
A necessary, but not sufficient, condition for a function to be differentiable at a point is that it must be continuous at that point. That is, the function must be defined at that point and its left-hand limit and right-hand limit must exist and be equal to the value of the function at that point. If $f(x)$ is the function and $x = a$ is the point we are interested in evaluating, this is mathematically stated as
$$\lim_{x\to a^+}f(x) = \lim_{x\to a^-}f(x) = f(a)$$Your challenge here is to write a program that will (1) accept a single-variable function and a value of that variable as inputs and (2) check whether the input function is continuous at the point where the variable assumes the value input.
Here is a sample working of the completed solution:
Enter a function in one variable: 1/x
Enter the variable: x
Enter the point to check the continuity at: 1
1/x is continuous at 1.0
The function $1/x$ is discontinuous at 0, so let’s check that:
Enter a function in one variable: 1/x
Enter the variable: x
Enter the point to check the continuity at: 0
1/x is not continuous at 0.0
In [1]:
import sympy
In [2]:
def check_continuity(f, var, value):
'''
Checks if the function f in the variable var is continuous in value by checking if
right limit = left limit = f(value)
'''
x = sympy.Symbol(var)
left = sympy.Limit(f, x, value, dir='-').doit()
right = sympy.Limit(f, x, value, dir='+').doit()
calc = sympy.sympify(f).subs({x: value})
if left == right == calc:
return True
else:
return False
In [3]:
check_continuity('1/x', 'x', 0)
Out[3]:
In [4]:
# Ask user for inputs
f = input('Enter a function in one variable:')
var = input('Enter the variable:')
value = input('Enter the point to check the continuity at:')
# Check if function is continuous
is_continuous = check_continuity(f, var, value)
if is_continuous:
print('{} is continuous at {}'.format(f, value))
else:
print('{} is not continuous at {}'.format(f, value))