** Creating your own functions
:PROPERTIES:
:date: 2013/02/27 14:49:18
:updated: 2013/03/06 18:29:24
:categories: python
:END:
We can combine operations to evaluate complex equations. Consider the value of the equation $x^3 - \log(x)$ for the value $x=4.1$.
#+BEGIN_SRC python
import numpy as np
x = 3
print x**3 - np.log(x)
#+END_SRC
#+RESULTS:
: 25.9013877113
It would be tedious to type this out each time. Next, we learn how to express this equation as a new function, which we can call with different values.
#+BEGIN_SRC python
import numpy as np
def f(x):
return x**3 - np.log(x)
print f(3)
print f(5.1)
#+END_SRC
#+RESULTS:
: 25.9013877113
: 131.02175946
It may not seem like we did much there, but this is the foundation for solving equations in the future. Before we get to solving equations, we have a few more details to consider. Next, we consider evaluating functions on arrays of values.