Lesson 1
Defining Functions in Python
Introduction to Functions in Python

Welcome to the first lesson of our course, "Optimization with SciPy." In this lesson, we're going to lay the foundation by learning how to define functions in Python. Functions are crucial in programming as they help you organize and reuse code efficiently. As we progress in the course, you'll see how understanding functions is vital for leveraging the full power of SciPy.

Creating Simple Functions

Let's quickly recall python functions, as we are going to use them a lot in this course.

In Python, you define a function using the def keyword, followed by the function name and parentheses (). Inside the parentheses, you can define parameters, which are inputs to the function. Let's start with a simple function that adds two numbers:

Python
1def add_numbers(a, b): 2 return a + b 3 4print(f"3 + 4 = {add_numbers(3, 4)}")
  • def add_numbers(a, b): defines a function named add_numbers that takes two parameters, a and b.
  • return a + b calculates the sum of a and b and returns the result.
  • When you call this function with add_numbers(3, 4), it will return 7.

Let's now explore more complex functions.

Example: Quadratic Function

Here's how to define f(x)=x2+4x+4f(x) = x^2 + 4x + 4:

Python
1def quadratic_function(x): 2 return x**2 + 4*x + 4 3 4print(f"quadratic_function(2) = {quadratic_function(2)}")
  • This function calculates the value of a quadratic expression x^2 + 4*x + 4.
  • The ** operator is used to denote x raised to the power of 2.
  • When you call this function with quadratic_function(2), it will return 16.
Example: Trigonometric Function

Let's define f(x)=sinx+cosxf(x) = \sin{x} + \cos{x}

Python
1import numpy as np 2 3def trigonometric_function(x): 4 return np.sin(x) + np.cos(x) 5 6print(f"trigonometric_function(π/4) = {trigonometric_function(np.pi/4)}")
  • np.sin(x) and np.cos(x) use NumPy library functions to calculate the sine and cosine of x.
  • The function returns the sum of the sine and cosine of x.
  • When you call this function with trigonometric_function(np.pi/4), it will return approximately 1.4142.
Example: Exponential Function

Let's see another example – a definition of f(x)=ex3xf(x) = e^x - 3x

Python
1def exponential_function(x): 2 return np.exp(x) - 3*x 3 4print(f"exponential_function(1) = {exponential_function(1)}")
  • np.exp(x) computes the exponential of x, e^x, using the NumPy library.
  • The function then subtracts 3*x from the exponential and returns the result.
  • When you call this function with exponential_function(1), it will return approximately -0.2817.
Example: Logarithmic Function

Here is your last example. Let's define f(x)=lnx+xf(x) = \ln{x} + x

Python
1def logarithmic_function(x): 2 if x <= 0: 3 return None 4 return np.log(x) + x 5 6print(f"logarithmic_function(2) = {logarithmic_function(2)}")
  • np.log(x) computes the natural logarithm of x.
  • The function checks if x is positive, as the logarithm is undefined for non-positive values, and if not, it returns None.
  • When you call this function with logarithmic_function(2), it will return approximately 1.6931.
Summary and Practice Preparation

In this lesson, you learned how to define, test, and use functions in Python. You explored different types of mathematical functions and saw how to implement them using Python's syntax. Understanding these principles is crucial as functions will be a cornerstone for optimizing mathematical problems with SciPy.

Now, as you progress to the practice exercises, remember to apply these concepts by experimenting with various function definitions. Test them with different inputs to solidify your understanding. Happy coding, and enjoy the journey of exploring optimization with SciPy!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.