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.
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:
Python1def 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 namedadd_numbers
that takes two parameters,a
andb
.return a + b
calculates the sum ofa
andb
and returns the result.- When you call this function with
add_numbers(3, 4)
, it will return7
.
Let's now explore more complex functions.
Here's how to define :
Python1def 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 denotex
raised to the power of 2. - When you call this function with
quadratic_function(2)
, it will return16
.
Let's define
Python1import 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)
andnp.cos(x)
use NumPy library functions to calculate the sine and cosine ofx
.- 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 approximately1.4142
.
Let's see another example – a definition of
Python1def 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 ofx
,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
.
Here is your last example. Let's define
Python1def 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 ofx
.- The function checks if
x
is positive, as the logarithm is undefined for non-positive values, and if not, it returnsNone
. - When you call this function with
logarithmic_function(2)
, it will return approximately1.6931
.
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!