MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Function Basics

Contents

Functions

There are two different ways of thinking of functions in calculus and hence in Matlab. One is as a symbolic expression, meaning a bunch of symbols. The other is as a function, meaning a rule. The difference between these can be captured by looking at the difference between the expressions f=x^2 and f(x)=x^2. In the former we're saying that x is a symbol and f is just the square of that symbol. In the latter we're saying f is a function into which x is plugged.

Matlab treats these quite differently. Each has advantages and disadvantages.

Symbolic Expressions

We start by defining a letter as a symbolic variable and then the function as a symbolic expression. Something like:

syms x;
f=x^4+7*x^2+x+exp(2*x)
 
f =
 
x + exp(2*x) + 7*x^2 + x^4
 

At this point it's important to note that x is to be treated symbolically (as a symbol) and so is f. In other words Matlab specifically treats x as a symbol and f as just another symbol which happens to be equal to x^4+7*x^2+x+exp(2*x).

In any case we can now do some things with f. We can substitute a constant for the symbol x:

subs(f,x,3)
ans =

  550.4288

We can find the first derivative using either of these:

diff(f,x)
 
ans =
 
14*x + 2*exp(2*x) + 4*x^3 + 1
 

or the following where the 1 means the first derivative.

diff(f,x,1)
 
ans =
 
14*x + 2*exp(2*x) + 4*x^3 + 1
 

And the second derivative with either of these:

diff(diff(f,x),x)
 
ans =
 
4*exp(2*x) + 12*x^2 + 14
 

Which is, as defined, the derivative of the derivative, or:

diff(f,x,2)
 
ans =
 
4*exp(2*x) + 12*x^2 + 14
 

Which is conceptually straight to the second derivative.

We can easily take the third derivative then plug in 0.1:

subs(diff(f,x,3),x,0.1)
ans =

   12.1712

We can integrate from 1 to 3 by:

int(f,x,1,3)
 
ans =
 
exp(6)/2 - exp(2)/2 + 1696/15
 

Or just find the standard antiderivative as follows. Note that Matlab does not bother with the +C but you should keep in mind that really it should be there.

int(f,x)
 
ans =
 
exp(2*x)/2 + x^2/2 + (7*x^3)/3 + x^5/5
 

Inline Functions

The most basic way to define a function (in a non-symbolic sense) is what's called an inline function. This is straightforward but as we'll see it has its weaknesses. For example to define the function f(x)=x+e^x we simply do:

f=inline('x+exp(x)','x')
f =

     Inline function:
     f(x) = x+exp(x)

Here the apostrophes around the x+exp(x) denote the function as a string of characters. The x off by itself indicates the variable we're using. In certain cases we can get away with not putting this variable but we won't delve into that now.

If we already have a symbolic expression defined we can convert it into an inline function as follows. The first two lines are just defining the symbolic function in case you don't have one defined.

syms x;
f=x+exp(x);
fin=inline(char(f),'x')
fin =

     Inline function:
     fin(x) = x + exp(x)

The char is necessary here. What's going on is that f is a symbolic expression. The command char converts it to a string and inline requires a string argument.

At this point we can do some things with the function which are intuitive. For example we can plug things in:

fin(3)
ans =

   23.0855

Or we can find a root near 0:

fzero(fin,0)
ans =

   -0.5671

However you might get excited and try to differentiate it using diff(fin) at which point Matlab tells you that diff does not work on inline functions. What gives? The answer is that differentiation is a symbolic operation, meaning it's something that Matlab does to expressions which are symbolic, meaning you can differentiate a symbolic expression but not a function. That's really odd, we agree, but it's how things work. To see how to differentiate an inline function see below the next part of the tutorial.

Function Handles

The preferred way to define functions is with the @ operator. Technically speaking this is creating a function handle but for many practical purposes we can gloss over the difference. Consider the following example:

f = @(x) x^2-x
f = 

    @(x)x^2-x

This tells Matlab to create a function for which x is the variable and for which the rule is x^2-x. This function can be treated in some ways very much like an inline function. For example the following do as you'd expect:

f(3);
fzero(f,2);

Function M-Files

This last way of defining functions will be introduced later.

Self-Test

  1. Write the Matlab command which will find the derivative of f(x)=sin(x^2) using symbolic expressions only.
  2. Write the Matlab command which will find the area under the curve f(x)=x^2sin(x) from 0 to pi/2 using symbolic expressions only.
  3. Write the Matlab command which will find the slope of the line tangent to f(x)=xe^x+x^3 at x=2 using symbolic expressions only.
  4. Write the series of Matlab commands which will define the f(x)=x^2-3x as an inline function and then find f(-2).
  5. Write the series of Matlab commands which will define a file handle for h(x)=x^2-7 and then find a zero of h(x) near x=2;

Answers to Self-Test

Next Topic:   More with Functions