Difference between revisions of "Software tutorial/Functions as objects"

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
m
m
Line 1: Line 1:
From this point onward in the course we will often have to deal with an arbitrary function, such as finding the function's zeroor integrating the function between two points.  It is helpful if we can write MATLAB or Python code that can operate on '''any''' function, not just a specific function, \(f(x)\).   
From this point onward in the course we will often have to deal with an arbitrary function, such as finding the function's zeros, or integrating the function between two points.  It is helpful if we can write MATLAB or Python code that can operate on '''any''' function, not just a specific function, \(f(x)\).   


For example, if we write a Python function to find the zero of \(f(x) = 3x - \frac{2}{x}\), then we would like to send that function, let's call it <tt>my_function</tt>, into another Python function that will find the zero of that function.
For example, if we write a Python function to find the zero of \(f(x) = 3x - \frac{2}{x}\), then we would like to send that function, let's call it <tt>my_function</tt>, into another Python function that will find the zero of that function.
Line 14: Line 14:
</syntaxhighlight>
</syntaxhighlight>
   
   
Python (or MATLAB) will see that <tt>my_function</tt> isn't an ordinary variable, it is (Python) function that will return the value of \(f(x)\) when given a value \(x\).  This means that when the code inside the <tt>bisection_method</tt> routine wants to evaluate \(f(x)\), it can do so.  If you want to change which function is being operated on, then you just call <tt>bisection_method</tt>, but change the first input to point to a different function.


 
== MATLAB: inline and anonymous functions ==
 


<syntaxhighlight lang="matlab">
<syntaxhighlight lang="matlab">
Line 22: Line 22:
fzero(f,1)
fzero(f,1)
roots(...)
roots(...)
</syntaxhighlight>
== Python: functions are objects ==
In Python, everything is an object.  All four of these variables, <tt>my_string, my_integer, my_float</tt> and <tt>my_function</tt> are objects. 
<syntaxhighlight lang="python">
my_string = 'abc'
my_integer = 123
my_float = 45.6789
def my_function(x):
    return 3*x - 2/x
</syntaxhighlight>
The first is a string object, the next an integer object, then a floating point object, and lastly, <tt>my_function</tt> is a function object.  You can use the <tt>type(...)</tt> function to determine a variable's type:
<syntaxhighlight lang="python">
>>> type(my_string)
<type 'str'>
>>> type(my_integer)
<type 'int'>
>>> type(my_float)
<type 'float'>
>>> type(my_function)
<type 'function'>
</syntaxhighlight>
</syntaxhighlight>

Revision as of 22:10, 17 October 2010

From this point onward in the course we will often have to deal with an arbitrary function, such as finding the function's zeros, or integrating the function between two points. It is helpful if we can write MATLAB or Python code that can operate on any function, not just a specific function, \(f(x)\).

For example, if we write a Python function to find the zero of \(f(x) = 3x - \frac{2}{x}\), then we would like to send that function, let's call it my_function, into another Python function that will find the zero of that function.

def my_function(x):
    """
    Returns the value of f(x) = 3x - x/2, at the given x
    """
    return 3*x - 2/x

lower = 0.1
upper = 3.0
root = bisection_method(my_function, lower, upper)

Python (or MATLAB) will see that my_function isn't an ordinary variable, it is (Python) function that will return the value of \(f(x)\) when given a value \(x\). This means that when the code inside the bisection_method routine wants to evaluate \(f(x)\), it can do so. If you want to change which function is being operated on, then you just call bisection_method, but change the first input to point to a different function.

MATLAB: inline and anonymous functions

f = inline('1/x - (x-1)')
fzero(f,1)
roots(...)

Python: functions are objects

In Python, everything is an object. All four of these variables, my_string, my_integer, my_float and my_function are objects.

my_string = 'abc'
my_integer = 123
my_float = 45.6789
def my_function(x):
    return 3*x - 2/x

The first is a string object, the next an integer object, then a floating point object, and lastly, my_function is a function object. You can use the type(...) function to determine a variable's type:

>>> type(my_string)
<type 'str'>
>>> type(my_integer)
<type 'int'>
>>> type(my_float)
<type 'float'>
>>> type(my_function)
<type 'function'>