Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
hendri54 committed Dec 1, 2015
1 parent 64f8f1d commit 00aa2ed
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 45 deletions.
Binary file added figures/growth_value_iter.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/white_noise.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 4 additions & 8 deletions matlab_exercises.mmd
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{{../markdown_header.txt}}

# Matlab Exercises

Try the exercises at [Quantitative Economics](www.http://quant-econ.net).

Some of the code for these exercises is in the `examples` folder.
Some of the code for these exercises is in the `examples` folder on `github`.

## Read the Matlab documentation on the following subjects:

Expand Down Expand Up @@ -32,8 +30,8 @@ Some of the code for these exercises is in the `examples` folder.

Output: mean and standard deviation for each column

My solution: `stats_lh.std_w`

My solution: `statsLH.std_w`
1. Compute the cdf for weighted data.

Input: data and weights
Expand Down Expand Up @@ -78,9 +76,7 @@ Steps:

Along the way, plot the value functions to show how they converge to the true one. Like this:

![GrowthValueIter][]

[GrowthValueIter]: figures/growth_value_iter.png "Value function iteration" height=400px
![GrowthValueIter](figures/growth_value_iter.png "Value function iteration")

See [QuantEcon](http://quant-econ.net/jl/dp_intro.html).

Expand Down
40 changes: 20 additions & 20 deletions matlab_functions.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Now try `open zeros`. This opens `zeros.m`.

* this means: `zeros` is not written in Matlab (it's truly built-in)

## The Matlab Path [mpath] ##
## The Matlab Path <a name="mpath"></a> ##

Matlab commands are stored in text files with the extension ''m''.

Expand Down Expand Up @@ -86,19 +86,19 @@ There are 2 ways of ensuring this:
For this to work, the parent dir of `+plot_821` must be on the `path`.


##Example of a Function
## Example of a Function

Imagine we want to compute the sum of 3 variables.

Type the following code in a text editor:

````matlab
```matlab
function xSum = sum3(x1, x2, x3);
% This function sums 3 variables
sum1 = x1 + x2;
xSum = sum1 + x3;
end
````
```

Save it as `sum3.m` in a directory Matlab knows to find.

Expand All @@ -125,13 +125,13 @@ Matlab complains that variable `sum1` does not exist.

`sum1` was visible inside of sum3.m, but not outside of it.

##More on Private Variables
## More on Private Variables

Variables inside of sum3.m are also not visible in functions called from within sum3.m
Variables inside of `sum3.m` are also not visible in functions called from within `sum3.m`.

**Example**

Modify sum3.m to read:
Modify `sum3.m` to read:

```matlab
function xSum = sum3(x1, x2, x3);
Expand All @@ -149,15 +149,15 @@ Then create the function:
end
```

Running sum3.m results in an error: sum1 is not found.
Running `sum3.m` results in an error: sum1 is not found.

##Global Variables
## Global Variables

To make a variable visible from anywhere, define it as a global (in sum3.m and sum3sub.m):
To make a variable visible from anywhere, define it as a `global` (in `sum3.m` and `sum3sub.m`):

global sum1;

Now running sum3.m does not yield an error message.
Now running `sum3.m` does not yield an error message.

>Remark: Avoid globals where possible.

Expand All @@ -177,9 +177,9 @@ Still, this can be very inefficient. If you pass a gigantic array to a function

There is no way of passing by reference.

# Examples
## Examples

## Example: Cobb-Douglas production function
### Example: Cobb-Douglas production function

This is a general purpose [Cobb-Douglas function](https://github.com/hendri54/shared/blob/master/%2BeconLH/cobb_douglas.m)

Expand All @@ -193,7 +193,7 @@ Notable features:

* self-test code governed by debugging switch `dbg`

##Example: Finding the Zero of a Function
### Example: Finding the Zero of a Function

Task: Find the solution to the equation \\(f(x)=\ln(x)-5=0 \\).

Expand All @@ -213,7 +213,7 @@ Use the built-in function [`fzero`](http://www.mathworks.com/help/matlab/ref/fze
7.3891
```

#Example: Two period household
## Example: Two period household

Household solves \\(\max u\left(c,g\right)\\)

Expand All @@ -227,15 +227,15 @@ that solve 2 budget constraints and Euler equation

Assume \\(u\left(c,g\right)=\frac{c^{1-\sigma}}{1-\sigma}+\beta\frac{g^{1-\sigma}}{1-\sigma} \\)

###Pseudo code
### Pseudo code

This is not a trivial program to write. So we break it down into trivial steps.

See [Tips on programming](programming.html)

We design top-down.

####Level 1:
#### Level 1:

Task: Find optimal \\(c\\).

Expand All @@ -245,7 +245,7 @@ Task: Find optimal \\(c\\).

> Note: Usually one would not restrict \\(c\\) to lie on a grid.

####Level 2:
#### Level 2:

Task: Calculate deviation from Euler equation.

Expand All @@ -254,14 +254,14 @@ Given: guess for \\(c\\), parameter values
1. Use budget constraints to calculate \\(s,g\\)
2. Return deviation: \\( dev=u_{c}-u_{g}R \\)

####Level 3:
#### Level 3:

Utility function.

* Return \\(u_{c} \\) and \\(u_{g} \\)
* for given \\(c,g\\) and parameters.

###Code
### Code

We write the code bottom up.

Expand Down
2 changes: 2 additions & 0 deletions matlab_intro.mmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Matlab: Introduction #

[link](matlab_exercises.html#growth-model)

## Installation and Configuration

Instructions for installation from [ITS](http://software.sites.unc.edu/software/)
Expand Down
18 changes: 8 additions & 10 deletions matlab_oddities.mmd
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
{{../markdown_header.txt}}

# Matlab Oddities and Pitfalls

### Matrix Indexing
## Matrix Indexing

Matlab is perfectly fine with invalid matrix indices.

The following code should produce a syntax error:

```matlab
```matlab
x =
1 2 3
4 5 6

>> x(5)
ans =
3
```
```

This is a very common source of bugs.

### Load and Save
## Load and Save

The `load` command is insane. It writes variables to the workspace that cannot be seen in the code. Always use load as a function as in `m = load(filename)`.

Even then `load` is insane. To retrieve the contents of `m` you need to know its name. Example:

```matlab
```matlab
x = 10;
save('test.mat', 'x');
m = load('test.mat');
disp(m.x);
10
```
```

It's best to write a wrapper for `load` and `save` so that `load2('test.mat')` returns `10` instead of `m`.

### Numeric Precision
## Numeric Precision

Matlab "downgrades" numeric precision to the lowest common denominator in calculations.

Expand All @@ -52,6 +50,6 @@ uint8

One solution: convert everything to `double` as soon as you load it.

Always check that intermediate results are still `double` (using `validateattributes`).
Always check that intermediate results are still `double` (using [validateattributes](http://www.mathworks.com/help/matlab/ref/validateattributes.html)).

---------
10 changes: 3 additions & 7 deletions matlab_plots.mmd
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{{../markdown_header.txt}}

# Plotting in Matlab

Matlab contains a large number of functions that plot data in 2D and 3D.

The most important is `plot`.

### Example: Plotting a white noise process
## Example: Plotting a white noise process

````matlab
% Seed the random number generator. For reproducability
Expand All @@ -26,12 +24,10 @@ ylabel('White noise')

The result: a poorly formatted figure (saved here by hand as `png`):

![figWhiteNoise][]

[figWhiteNoise]: figures/white_noise.png "White noise" height=400px
![figWhiteNoise](figures/white_noise.png "White noise")


# Saving Plots #
## Saving Plots #

Now for the bad news: Generating reasonably formatted plots in Matlab is surprisingly hard.

Expand Down

0 comments on commit 00aa2ed

Please sign in to comment.