Skip to content

Commit

Permalink
Update 3
Browse files Browse the repository at this point in the history
  • Loading branch information
hendri54 committed Jan 11, 2016
1 parent 3b77129 commit 4a41218
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Contents:
matlab_plots
matlab_exercises
project_code_organization

oop


Indices and tables
Expand Down
34 changes: 22 additions & 12 deletions matlab_datatypes.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ The elements are accessed by name:
37
```

One can have [structure arrays](http://www.mathworks.com/help/matlab/ref/struct.html), but they are tricky because each element must be a structure with the same fields.

Often, [cell arrays](#cell-arrays) are more useful.

#### Where structures are useful

Use a structure to pass a large number of arguments to a function.
Expand Down Expand Up @@ -264,7 +268,24 @@ Example:

## Cell Arrays ##

(http://www.mathworks.com/help/matlab/cell-arrays.html)
A [cell array](http://www.mathworks.com/help/matlab/cell-arrays.html) is an n-dimensional array of mixed data.

Each cell can hold a different data type.

```matlab
>> x = {'abc', 17; [3,4,5], {1, 2}}
x =
'abc' [ 17]
[1x3 double] {1x2 cell}
>> disp(x{1,1})
abc
>> disp(x{2,1})
3 4 5
```

Uses:

* most common: replacement for structure array when one is not sure that all elements have the same fields.

## Numeric Precision ##

Expand Down Expand Up @@ -321,15 +342,4 @@ uint8

> Rule of thumb: Store large datsets in low precision. Make everything double the moment you load it.

## User Defined Data Types #

It is possible to define new data types. See the Matlab documentation on [object oriented programming](http://www.mathworks.com/help/matlab/object-oriented-programming.html).

`classdef` really defines a `struct` with certain required fields.

Benefits:

* automatically ensures that each object has the right fields
* can define `methods` (functions) that operate on the object.

-----------
41 changes: 41 additions & 0 deletions oop.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Object Oriented Programming (OOP) #

## The Idea ##

OOP takes structured programming to the next level. Structured programming encapsulates local data in a function. The user does not need to know anything about the function other than the interface (inputs and outputs).

OOP recognizes that some groups of functions "hang together" because they operate on the same object. One idea is to group these functions together.

The second idea is that certain persistent data "belong to" an object. They should only be manipulated by functions that also "belong to" the object.

OOP therefore bundles data (called `properties`) and functions (called `methods`) together.

### Example: Utility function ###

$u(c,l) = c ^ (1-\sigma) / (1-\sigma) + \phi \log(l)$

Persistent data include: parameters ($\sigma, \phi$).

Methods include: compute $u_{c}$, $u(c,l)$, inverse marginal utility, indifference curves

## Benefits ##

There is nothing that OOP can do that could not be done without OOP. The benefits lie in code organization.

The programmer sees all methods that operate on the object in one place. That makes it easier to

* test the code
* modify the code
* ensure consistency

Since all code is in one place, it is easy to swap out.

Imagine you want to compute a model with different utility functions. With OOP, all you need to do is swap out the utility function object. Ideally, the other code remains unchanged.

## References ##

Matlab documentation on [object oriented programming](http://www.mathworks.com/help/matlab/object-oriented-programming.html).

---------


0 comments on commit 4a41218

Please sign in to comment.