Module # ONE - Wayne State University



Module # ZERO

CALCULUS

Symbolic & Numeric

using

MatLab

By Professor Mohamad H. Hassoun

Department of Electrical & Computer Engineering

Wayne Satate University

Detroit, MI 48202

October 22, 1996

Contents:

I. Functions: Graphs, Extreme Points & Roots

II. Differentiation

III. Integration

IV. Series

clear % Just to make sure that no MatLab variables are present from a previous session.

NOTE on executing commands (green colored text) in this document: place the cursor on the command/MatLab expression you wish to execute and then type Ctrl-Enter. Make sure that variables/array definitions commands are executed before executing any following commands which depend on such definitions. Definitions in bold blue font execute automatically when you open this document; i.e., you nedd not re-execute them over.

You can get full explanation (help) of any MatLab command in this document by highlighting that command and clicking on the '?' in the bar menu above (or type Ctrl-F1).

(This Page is Left Blank for Breif Notes)

I. Functions: Graphs, Roots, Extreme Points, & More

Graphs

Consider the single variable function:

f = 'sin(x)/x' % defining a symbolic function

f =

sin(x)/x

x=1;

eval(f) % evaluting f at x = 1

ans =

0.8415

A simple plot may now be generated by using the fplot command:

fplot(f , [-20 20 -.4 1.2]) % The vector argument specifies the x & y axis ranges

[pic]

One may want to add titles and axis labels as follows

title('plot of f(x) = sin(x)/x')

xlabel('x')

ylabel('f(x)')

[pic]

Alternatively, one may evaluate the function f(.) over a range of values of its argument and the plot the data using the plot command:

z = linspace(-20, 20, 60);

y = sin(z) ./ z; % here,component-wise division (./) must be used

plot(z,y)

[pic]

Here is the same plot but without connecting the 'blue' plot points

plot(z,y,'b.')

[pic]

Extreme Points: Numerical Search for Minima & Maxima

The MatLab function fmin searches for the minima of a one-dimensional function:

xmin = fmin(f,0,5) % search for the minimum over the range 0 < x < 5.

xmin =

4.4934

x = xmin;

eval(f) % evaluate the function at the minimum

ans =

-0.2172

Similarly, the minimum point inside 10 < x < 15 is

fmin(f,10,15)

ans =

10.9041

fmin may also be used to find the maxima of f(x), by noting that the local minima of -f(x) correspond to local maxima of f(x). Let us find the maximum of f(x) in the range 5 < x < 10:

xmax = fmin('-sin(x)/x',5,10)

xmax =

7.7252

The value of f(x) at this extreme point is

x = xmax;

eval(f)

ans =

0.1284

Finding Roots: f(x) = 0

solve(f) % solves for roots of f(x) near x = 0. Symbolic solutions are attempted.

ans =

3.141592653589793

Another example (multiple solutions)

t = solve('tan(2*x) = sin(x)')

t =

[ 0]

[acos(1/2+1/2*3^(1/2))]

[acos(1/2-1/2*3^(1/2))]

numeric(t)

ans =

0

0 + 0.8314i

1.9455

II. Differentiation

MatLab allows for the differentiation of symbolic functions using the diff command. Let us try differentiating the function f(x) = sin(x)/x which was defined earlier.

g = diff(f)

g =

cos(x)/x-sin(x)/x^2

We can generate a "pretty" form of the above answer using the pretty command:

pretty(g)

cos(x) sin(x)

------ - ------

x 2

x

The second derivative of f(x) can now be evaluated as

diff(g)

ans =

-sin(x)/x-2*cos(x)/x^2+2*sin(x)/x^3

pretty(ans)

sin(x) cos(x) sin(x)

- ------ - 2 ------ + 2 ------

x 2 3

x x

The second derivative may also be evaluated directly from f(x) by

diff(f,2)

ans =

-sin(x)/x-2*cos(x)/x^2+2*sin(x)/x^3

pretty(ans)

sin(x) cos(x) sin(x)

- ------ - 2 ------ + 2 ------

x 2 3

x x

Here is how to differentiate a function h(b) = 3a -2b2 with respect to b:

diff('3*a - 2*b^2', 'b')

ans =

-4*b

In fact, the same answer will be generated even if we do not declare b to be the variable of differentiation in the diff expression; it is because diff defaults to the symbolic variable closest to 'x' as the variable of differentiation in the expression to be differentiated:

diff('3*a - 2*b^2')

ans =

-4*b

We conclude this section by generating a plot for the function h(x) = tanh(x) and its derivative over the range -4 < x < 4:

h = 'tanh(x)';

hprime = diff(h);

fplot(h,[-3,3,-1,1])

hold % this command holds current plot so that multiple % plots may be displayed.

fplot(hprime,[-3,3,-1,1],'b') % b for blue

hold % this second execution of 'hold' releases the plot

Current plot held

Current plot released

[pic]

Alternatively, we may display h and its derivative side by side using the subplot command

subplot(1,2,1) % pick the left of two subplots

fplot(h,[-3,3,-1,1]), title('tanh(x)')

subplot(1,2,2) % pick the right of two subplots

fplot(hprime,[-3,3,-1,1], 'b--'), title('Derivative of tanh(x)')

[pic]

III. Integration

Symbolic Integration

The command int(f) attempts to integrate the symbolic function f. It may be used for definate as well as indefinate integration. The following are some examples.

h = '1/(x^2 + a^2)';

pretty(h)

1

-------

2 2

x + a

int(h)

ans =

1/a*atan(x/a)

pretty(ans)

atan(x/a)

---------

a

We may also integrate w.r.t. 'a', as follows

int(h,'a')

ans =

1/x*atan(a/x)

pretty(ans)

atan(a/x)

---------

x

Other examples:

pretty(int('exp(a*x)*sin(b*x)'))

b exp(a x) cos(b x) a exp(a x) sin(b x)

- ------------------- + -------------------

2 2 2 2

a + b a + b

Next, we integrate the standard normal distribution: N(0,1) = (2p)-1exp(-.5x2)

t = int('1/sqrt(2*pi)*exp(-0.5*x^2)')

t =

.5000000000000000*erf(.7071067811865475*x)

which may also be written as 0.5erf(2-1/2 x). erf is itself an integral with no closed form expression. It is a MatLab defined function.

fplot(t,[-3,3,-.5,.5])

[pic]

Next we integrate N(0,1) over the interval [0 inf]. This should give 0.5 as answer (this is standard knowledge from probability theory!).

int('1/sqrt(2*pi)*exp(-0.5*x^2)',0,inf)

ans =

.6266570686577500*2^(1/2)/pi^(1/2)

numeric(ans)

ans =

0.5000

Here is a function with no antiderivative

int('log(x)/exp(x^2)')

ans =

int(log(x)/exp(x^2),x)

pretty(ans)

/

| log(x)

| ------- dx

| 2

/ exp(x )

Numeric Integration

If symbolic integration is not possible, one may still obtain a numerical result if the integration is defined over a numerical range (say from 1 to 100). The MatLab commands trapz or quad or quad8 may be used. quad and quad8 generally leads to more accurate integrations. Here is an example involving the above integration.

x = 1:.1:100;

y = log(x) ./ exp(x.^2);

result = trapz(x,y)

result =

0.0356

As another example, let us integrate the normal distribution suing the trapz command (we will use 500 to approximate inf)

x = 0:.1:500;

y = 1/sqrt(2*pi)*exp(-0.5*x.^2);

result = trapz(x,y)

result =

0.5000

The Laplace Integral (Laplace Transform)

One very important application of integration is in finding the Laplace Transformation of a function. This transformation is very helpful in analysing linear systems; it is also useful in solving linear ordinary differential equations (refer to Module Three on Differential Equations for details). The Laplace integral of a function f(x) takes the form

int('f(t)*exp(-s*t)', 't', 0, inf);

pretty

inf

/

|

| f(t) exp(- s t) dt

|

/

0

The following are Laplace transforms for some basic functions:

Example 1: f(t) = c, where c is a constant

int('c*exp(-s*t)','t',0,inf)

ans =

limit(-1/s*exp(-s*t)*c+1/s*c,t = inf,left)

pretty

exp(- s t) c

limit - ------------ + c/s

t -> inf- s

Here, s is a complex variable. Evaluating the above limit as t approaches infinity eliminates the first term in the limit. Thus the answer is c/s. Matlab has a build-in command for finding the Laplace integral directly. The command is laplace(f) where f is a symbolic function of the variable t (not x). If f is a constant (i.e., t does not appear in the expression for f) then the command laplace(f,'s','t') must be used. Here is the Laplace transform for the function f(t) = c

pretty(laplace('c','s','t'))

c/s

Example 2: f(t) = ce-at,

pretty(laplace('c*exp(-a*t)'))

c

-----

s + a

Example 3: f(t) = cos(w*t),

pretty(laplace('cos(w*t)'))

s

-------

2 2

s + w

IV. Series

The MatLab command symsum returns the sum of its symbolic function f(n) from a lower limit to a higher limit on n. Here is an example:

f = '(2*n-1)^2';

symsum(f,1,'n') % summing f(n) from 1 to n

ans =

11/3*n+8/3-4*(n+1)^2+4/3*(n+1)^3

factor(ans)

ans =

1/3*n*(2*n-1)*(2*n+1)

pretty(ans)

1/3 n (2 n - 1) (2 n + 1)

Another example:

f = 'x^k/k!';

symsum(f ,'k',0, inf) % summation over k.

ans =

exp(x)

This can be shown to be the correct answer by using MatLab's Taylor series expansion command (taylor):

taylor('exp(x)')

ans =

1+1*x+1/2*x^2+1/6*x^3+1/24*x^4+1/120*x^5+O(x^6)

Other examples:

Geometric series:

taylor('1/(1-x)')

ans =

1+1*x+1*x^2+1*x^3+1*x^4+1*x^5+O(x^6)

symsum('x^n', 'n',0,inf)

ans =

-1/(x-1)

Power series expansion of cos(x)

taylor('cos(x)')

ans =

1-1/2*x^2+1/24*x^4+O(x^6)

The mth term in this answer takes the general form -1mx2m/(2m)!. Let us sum the series from 0 to infinity:

symsum('((-1)^m)*x^(2*m)/((2*m)!)', 'm',0,inf)

ans =

cos(x)

YOU ARE NOW READY TO MOVE ON TO OTHER EXCITING COMPUTATIONS

WITH MATLAB !!!

(Other Modules Available:

Linear Algebra, Complex Numbers, & Differential Equations)

who % Please go to page 1 and read the small print!!

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download