Skip to main content

Tutorial 1

Binder Open In Colab

Interactive Notebooks

Click the badges above to run this tutorial interactively in your browser without installing anything!

  • Binder: Free cloud-based Jupyter environment
  • Colab: Google's free Jupyter notebook environment

Topics Covered​

This tutorial covers the following topics using Python and SymPy:

  • Limits: Computing limits using limit definition
  • Derivatives: First derivatives using differentiation rules
  • Implicit Differentiation: Finding derivatives of implicitly defined functions
  • Parametric Equations: Derivatives of parametric curves

Introduction​

Sympy: SymPy is a Python library for symbolic mathematics. We will use this library to solve all the tutorials.

import sympy as sy
from sympy.abc import x, y, t # variable x
from sympy.solvers import solve # Solving equations
from sympy import sqrt,tan,sin,cos,sec,pi,root,ln
from sympy import log,exp,atan,asinh,atanh,asin # Import all required math function
from sympy import diff, idiff # Solving differentiation
sy.init_printing(use_latex=True) # Show it in natural display

How to use sympy to solve limit questions?​

  1. Declare the symbol to be used in the function. For example, if you wanted to use the symbol xx, just use x as usual because we have import the variable x earlier. Alternatively, you can declare xx such that x = sy.symbols('x'). This will make the variable x a representation of xx in the function to be solved. Note that sy is used because we import sympy as sy.

  2. Type the function. For example, if the function is 1x\frac{1}{x}, type it as 1/x. Note, for power, python uses ** instead of ^.

  3. Solve the limit with sympy library by using sy.limit(function,symbol,limit). sy.limit takes three parameters, which are the function, the symbols used in the function and the limit. The return of this fuction will be the answer.

How to use sympy to solve derivative questions?​

Just diff(function,symbol)!

Some cheat sheet!​

To make sure if you input the equation correctly, you can use the display(function) function. For example,

expr = sqrt(x)
display(expr)

will return:

x\sqrt{x}

The representation of mathematics function in Python:

Mathematics FunctionPython representation
x2x^2x**2
x\sqrt{x}sqrt(x)
x3\sqrt[3]{x}root(x,3)
tan⁡x\tan{x}tan(x)
sec⁡x\sec{x}sec(x)
π\pipi

Note that the sqrt, tan, sec, pi used in this notebook are from sympy library, not from math library.

That's it! Let's use this knowledge and solve them.​

Question 1​

Use the limit definition to evaluate:

a. lim⁡x→5x2−25x2+x−30\lim\limits_{x \to 5} \frac{x^2 - 25}{x^2 + x - 30}

lim = 5
func = (x**2-25)/(x**2+x-30)
sy.limit(func,x,lim)

b. lim⁡x→9x−3x−9\lim\limits_{x \to 9} \frac{\sqrt{x} - 3}{x - 9}

lim = 9
func = (sqrt(x)-3)/(x-9)
sy.limit(func,x,lim)

c. lim⁡x→0x3−x+9\lim\limits_{x \to 0} \frac{x}{3 - \sqrt{x + 9}}

lim = 0
func = x/(3-sqrt(x+9))
sy.limit(func,x,lim)

Question 2​

Find the derivative of log⁡(4+cos⁡x)\log(4+\cos x)

diff(log(4+cos(x),10))

Question 3​

Find dydx\frac{dy}{dx} for cos⁡(x2)=xey\cos(x^2)=xe^y

eqn=cos(x**2)-x*exp(y)
idiff(eqn,y,x)

Question 4​

Find dydx\frac{dy}{dx} for x3y3−2y=xx^3y^3-2y=x

eqn=x**3*y**3-2*y-x
idiff(eqn,y,x)

Question 5​

A curve in the plane is defined parametrically by the equations x=7ln⁡(t)x=7\ln(t) and y=1−4ty=\sqrt{1-4t}. Find dydx\frac{dy}{dx}

dydt = sqrt(1-4*t).diff(t)
dxdt = 7*ln(t).diff(t)
dydt/dxdt

Question 6​

A curve in the plane is defined parametrically by the equations x=t2−1x=t^2-1 and y=2ety=2e^t. Find dydx\frac{dy}{dx}

dydt = 2*exp(t).diff(t)
dxdt = (t**2-1).diff(t)
dydt/dxdt

Question 7​

Find y′y' for each of the following:

a. x2tan⁡(y)+y10sec⁡(x)=2xx^2\tan(y)+y^{10}\sec(x)=2x

eqn=x**2*tan(y)+y**10*sec(x)-2*x
idiff(eqn,y,x)

b. x3y5+3x=8y3+1x^3y^5+3x=8y^3+1

eqn=x**3*y**5+3*x-8*y**3-1
idiff(eqn,y,x)

c. e2x+3y=x2−ln⁡(xy3)e^{2x+3y}=x^2-\ln(xy^3)

eqn=exp(2*x+3*y)-x**2+ln(x*y**3)
idiff(eqn,y,x)

Question 8​

Solve for y′y' if y=ln⁡(cos⁡x2)y=\ln(\cos x^2)

diff(ln(cos(x**2)))

Question 9​

Find y′y' for 10e2xy=e15y+e13x10e^{2xy}=e^{15y}+e^{13x}

eqn=10*exp(2*x*y)-exp(15*y)-exp(13*x)
idiff(eqn,y,x)

Question 10​

Solve f′(x)f'(x) if f(x)=2x(arctan⁡5x)2+6tan⁡(cos⁡6x)f(x)=2x(\arctan 5x)^2+6\tan(\cos 6x)

diff(2*x*(atan(5*x))**2+6*tan(cos(6*x)))

Question 11​

Solve y′y' if y=4xsinh⁡−1(x6)+tanh⁡−1(cos⁡10x)y=4x\sinh^{-1}(\frac{x}{6})+\tanh^{-1}(\cos 10x)

diff(4*x*asinh(x/6)+atanh(cos(10*x)))

Question 12​

A closed-ended pipe must hold 200200 litres of fluid. Determine the radius and height (in cm) that minimise the amount of material used.

The volume of a closed cylinder is πr2h=200,000 cm3\pi r^2 h = 200{,}000\ \text{cm}^3, so h=200000πr2h=\frac{200000}{\pi r^2}. Substituting into the surface area gives A(r)=2πr2+400000rA(r)=2\pi r^2+\frac{400000}{r}. Because this is an applied problem, we declare r as a positive symbol so that solve only returns the physically meaningful root.

r = sy.Symbol('r', positive=True)      # radius must be positive
A = 2*pi*r**2 + 400000/r # A(r) after eliminating h
display(A.diff(r))

r_crit = solve(A.diff(r), r) # critical number: A'(r) = 0
display(r_crit, [float(s) for s in r_crit])

# Second derivative test: A''(r) > 0 means concave up, i.e. a minimum
display(A.diff(r, 2).subs(r, r_crit[0]))

h = 200000/(pi*r_crit[0]**2) # back-substitute to find the height
display(h, float(h))

So r≈31.69 cmr \approx 31.69\ \text{cm} and h≈63.38 cmh \approx 63.38\ \text{cm}. Since A′′(r)>0A''(r)>0, the surface is concave upward and the critical point is indeed a minimum.

Question 13​

Three copper cones have a total volume of 15 m315\ \text{m}^3, so each cone has volume 5 m35\ \text{m}^3. Only the lateral surface A=πrsA=\pi r s is to be minimised, where s=r2+h2s=\sqrt{r^2+h^2} and h=15πr2h=\frac{15}{\pi r^2}.

r = sy.Symbol('r', positive=True)
h = 15/(pi*r**2) # height from the volume constraint
A = pi*r*sqrt(r**2 + h**2) # lateral surface, A = pi*r*s

r_crit = solve(sy.simplify(A.diff(r)), r)
r_star = [s for s in r_crit if s.is_real and s > 0][0]
display(sy.simplify(r_star), float(r_star))

h_star = 15/(pi*r_star**2)
s_star = sqrt(r_star**2 + h_star**2)
display(float(h_star), float(s_star))

Hence the optimal dimensions of each cone (for the requested lateral area) are

  • Radius, r≈1.5002 mr \approx 1.5002\ \text{m}
  • Height, h≈2.1216 mh \approx 2.1216\ \text{m}
  • Slant length, s≈2.5984 ms \approx 2.5984\ \text{m}
note

The calculation minimises the lateral surface only, matching the final sentence of the question. If the flat base is also counted (A=πr2+πrsA=\pi r^2+\pi r s), replace the area above with A = pi*r**2 + pi*r*sqrt(r**2 + h**2); the optimum is then approximately r=1.1907 mr=1.1907\ \text{m}, h=3.3678 mh=3.3678\ \text{m}, and s=3.5721 ms=3.5721\ \text{m}.


Extra Learning Resources​

Key Concepts to Master​

  1. Limit Laws: Sum, product, quotient rules for limits
  2. Differentiation Rules: Power rule, product rule, quotient rule, chain rule
  3. Trigonometric Derivatives: Derivatives of sin, cos, tan, sec, etc.
  4. Implicit Differentiation: Finding derivatives without solving for y explicitly
  5. Parametric Derivatives: Using dydx=dy/dtdx/dt\frac{dy}{dx} = \frac{dy/dt}{dx/dt}

SymPy Resources​

Practice Problems​

  • Try solving the same problems using different methods (e.g., algebraic simplification before limits)
  • Experiment with display() function to verify your input expressions
  • Use simplify() to clean up complex derivative expressions
  • Practice with one-sided limits using limit(f, x, a, '+') or limit(f, x, a, '-')

Common Pitfalls​

  • Remember to use ** for exponents, not ^
  • Import necessary functions from SymPy (sin, cos, log, etc.)
  • For implicit differentiation, use idiff(equation, y, x) not diff()
  • Logarithm base: log(x, base) - e.g., log(x, 10) for log₁₀(x)