Hi All,

This week a few of our colleagues were grinding through some tough calculus for a homework assignment and bemoaning the fact they’d gotten a bit rusty since undergrad. For help one might turn to Maple or Wolfram, but Matlab is also a useful tool.

Here we want to make use of Matlab’s symbolic variable class. Mathworks has some nice tutorials on how to create symbolic variables etc. and a nice description of functions which can operate on symbolic variables. I’ll go through a really brief tutorial here.

First let’s create a symbolic number. We do this with the sym function. To see the difference between a symbolic number and floating-point number try the following code:

sym(1/3) 1/3

ans =

1/3

ans =

0.3333

Neat, but how are they different? Stealing from Mathwork’s tutorial for a moment consider sin(π). We all learned in middle school that sin(π) = 0. Of course π is irrational so numerically evaluating sin(π) may not return exactly zero. Let’s see what happens when we evaluate sin(π) with both symbolic and floating-point pi’s:

sin(sym(pi)) sin(pi)

ans =

0

ans =

1.2246e-16

Cool! Now, you’ve probably guessed that if we can create symbolic variables, we can also create symbolic functions. Let’s create a simple symbolic function:

syms x a b c f = symfun(3*x^2+2*x+1,x);

f(x) =

3*x^2 + 2*x + 1

Note that we could also use symbolic variables a, b, and c for the coefficients in *f*. For now, let’s stick with 1, 2, and 3.

We can substitute any constant (say 3) or symbolic (say sin(y)) value of x we want (say x=3) and get the resulting value of *f*:

Test1 = f(3) syms y Test2 = f(sin(y))

Test1 =

34

Test2 =

2*sin(y) + 3*sin(y)^2 + 1

We can also multiply our function *f* by a constant or by another variable:

Test3 = 6*f Test4 = symfun(f*y,[x y])

Test3 (x)=

18*x^2 + 12*x + 6

Test4(x, y) =

y*(3*x^2 + 2*x + 1)

Now, getting back to *f*, let’s do some calculus. For starters, let’s take the first derivative of *f* with respect to *x*:

df = diff(f,x)

df(x) =

6*x + 2

And we can integrate as well:

F = int(f,x)

F(x) =

x*(x^2 + x + 1)

Again recall that we can derive the more general form of the expression using symbolic coefficients. Of course, this is a simple example, but I’ve used it for much, much more complicated functions.

If we’re having trouble with calculus, maybe you could use a bit of help on the linear algebra front as well. Let’s start by instantiating a symbolic matrix, *X:*

X = sym('X',[2,2])

X =

[ X1_1, X1_2]

[ X2_1, X2_2]

Alternatively we could instantiate X as a symbolic function with more readable symbolic elements:

syms a b c d X=symfun([a,b;c,d],[a b c d])

X(a, b, c, d) =

[ a, b]

[ c, d]

As with the previous examples, we can operate on *X*. Let’s take the inverse:

Xinv = inv(X)

Xinv(a, b, c, d) =

[ d/(a*d – b*c), -b/(a*d – b*c)]

[ -c/(a*d – b*c), a/(a*d – b*c)]

Break out your linear algebra book to confirm that this is right. Suppose we want to do something a bit more complicated: linear regression! We instantiate a a *Y* vector of observations:

syms y1 y2 y3 Y = [y1;y2;y3]

Y =

y1

y2

y3

Now, suppose we want to fit a constant model, we instantiate a new *X* matrix (or in this case a vector) of ones:

X = sym(ones(3,1))

X =

1

1

1

Now, we fit our model (*y=bx+e*) using the ordinary least squares (OLS) estimator (b=(X^{T}X)^^{-1}X^{T}Y):

b = inv(X'*X)*X'*Y

b =

y1/3 + y2/3 + y3/3

So we have our answer, and we find that the OLS estimator of the constant model is simply the sample mean of the data.

All of these examples were super easy, but should be helpful if you’re stuck on econometrics, statistics, or hydrology homework. If there is interest I can make later posts on taking the partial derivative of piece-wise polynomial structures in Matlab.

Jon