You are on page 1of 4

Calculus with MATLAB

Functions and Symbolic Differentiation


There are two distinct but related notions of function that are important in Calculus. One is a
symbolic expression such as sin(x) or x^2. The other is a rule (algorithm) for producing a
numerical output from a given numerical input or set of numerical inputs. This definition is
more general; for example, it allows us to define a function f(x) to be x^2 if x is negative or
0, and sin(x) if x is positive. On the other hand, any symbolic expression implies a rule for
evaluation. That is, if we know that f(x) = x^2, we know that f(4) = 4^2 = 16. In
MATLAB, the fundamental difference between a function and a symbolic expression is that a
function can be called with arguments and a symbolic expression cannot. However, a
symbolic expression can be differentiated symbolically, while a function cannot. MATLAB
functions can be created in three ways:

as inline functions,

as anonymous functions or function handles,

and as function M-files.

A typical way to define a symbolic expression is as follows:

syms x
f = x^2 - sin(x)

f =

x^2 - sin(x)

The next line shows we can differentiate f in the obvious way. MATLAB recognizes what the
"variable" is. In the case of several symbolic variables, we can specify the one with respect to
which we want to differentiate.

diff(f)

ans =

2*x - cos(x)

However, we cannot evaluate it, at least in the obvious way. If we type f(4)

we get an error message:

??? Error using ==> mupadmex Error in MuPAD command: Index exceeds matrix
dimensions.

Error in ==> sym.sym>sym.subsref at 1366 B = mupadmex('mllib::subsref',A.s,inds{:});


We can evaluate f(4) by substituting 4 for x using the subs command:

subs(f,x,4)
ans =

16.7568

We can also turn f into an inline function with the command:

fin=inline(char(f))
fin =

Inline function:
fin(x) = x^2 - sin(x)

What's going on here is that the inline command requires a string as an input, and char
turns f from a symbolic expression to the string 'x^2-sin(x)'. (If we had simply typed
fin=inline(f) we'd get an error message, since f is not a string.) The inline function fin
now accepts an argument:

fin(4)
ans =

16.7568

The best way to make f into function is with the @ operator, creating an anonymous
function. We can create such a function with

fanon = @(t) subs(f, x, t)


fanon(4)
fanon =

@(t)subs(f,x,t)

ans =

16.7568

Similarly we can construct a function, either inline or anonymous, from the


derivative of f:

fxin=inline(char(diff(f)))
fxin =

Inline function:
fxin(x) = 2*x - cos(x)

Here the matlab function char replaces its argument by the string that represents it, thereby
making it available to functions such as inline that demand strings as input.

Alternatively, we can try:


fxanon = @(t) subs(diff(f), x, t)
fxanon(4)
fxanon =

@(t)subs(diff(f),x,t)

ans =

8.6536

The other way to create a function that can be evaluated is to write a function M-file. This is
the primary way to define a function in most applications of MATLAB, although we will not
use it oftern. The M-file can be created with the MATLAB editor, and you can print out its
contents with the type command.

type fun1
function y = fun1(x)
y = x.^2 - sin(x)

fun1(4)
y =

16.7568

ans =

16.7568

Problem 1

Let g(x) = x(e^x - x - 1).


a) Enter the formula for g(x) as a symbolic expression.

b) Obtain and name a symbolic expression for g'(x).

c) Evaluate g(4) and g'(4), using subs.

d) Evaluate g(4) and g'(4), by creating anonymous functions.

e) Evaluate g(4) by creating an M-file.

Problem 2

1. Let f(x) = x^5 + 3x^4 - 3x + 7.

a) Plot f between x = -1 and x = 1.

b) Compute the derivative of f.

c) Use solve to find all critical points of f.

d) Find the extreme values of f on the interval [-1,1]. Hint: solve will store the
critical points in a vector, which you cannot use unless you name it. Remember that
you also need the values of f at 1 and -1.

2. Find all real roots of the equation x = 4 sin(x). You will need to use fzero.

3. Evaluate each of the following integrals, using both int and quadl.

a)

b)

You might also like