Lecture 06 - Calculus With Matlab PDF
Lecture 06 - Calculus With Matlab PDF
2
Two Types of Operations
(Continued)
3
Symbolic Variables
4
Symbolic Differentiation
5
Example 1. Use MATLAB to determine
derivative of function below.
y 4x 5
>> syms x
>> y = 4*x^5
y=
4*x^5
>> yprime = diff(y)
yprime =
20*x^4
6
Example 1. Continuation.
Second Approach:
7
Example 1. Continuation.
Third Approach:
>> y = '4*x^5'
y=
4*x^5
>> yprime = diff(y)
yprime =
20*x^4
8
Example 1. Continuation.
9
Example 2. Use ezplot to plot y and
yprime of Example 1.
The simplest form is ezplot(y), but the
domain will be from -2 to 2. The domain
can be changed by
>> ezplot(y, [x1 x2])
In this example,
ezplot(y, [-1 1])
ezplot(yprime,[-1 1])
The plots after labeling are shown on the
next two slides.
10
11
12
Symbolic Integration
>> syms x
>> y = x^2*exp(x)
y=
x^2*exp(x)
>> z = int(y)
z=
x^2*exp(x)-2*x*exp(x)+2*exp(x)
14
Example 3. Continuation.
15
16
17
Numerical Integration and Differentiation
Quadratures, double and triple integrals, and multidimensional derivatives
Functions
del2 Discrete Laplacian
diff Differences and Approximate Derivatives
gradient Numerical gradient
polyder Polynomial derivative
dy y
dx x
19
Diff Command for Numerical Data
Assume that x and y have been defined
at N+1 points. A vector u with N points is
defined as
u1 y2 y1
u2 y3 y2
u3 y4 y3
u N yN 1 yN
20
Diff Command for Numerical Data.
Continuation.
The following command forms u:
>> u = diff(y)
To approximate derivative,
>> yprime = diff(y)/delx
Because x and y have one more point
than yprime, they can be adjusted.
>> x = x(1:N)
>> y = y(1:N)
>> plot(x, y, x, yprime)
21
Example 5(a) For y = sin x, determine
numerical derivative based on 11
points in one cycle.
>> x = linspace(0, 2*pi, 11);
>> y = sin(x);
>> delx = 2*pi/10;
>> yprime = diff(y)/delx;
>> x = x(1:10);
>> plot(x, yprime, x, cos(x),’o’)
The plots are shown on the next slide.The
approximation is “crude” as expected.
22
23
Example 5(b) For y = sin x, determine
numerical derivative based on 101
points in one cycle.
>> x = linspace(0, 2*pi, 101);
>> y = sin(x);
>> delx = 2*pi/100;
>> yprime = diff(y)/delx;
>> x = x(1:100);
>> plot(x, yprime, x, cos(x),’o’)
The plots are shown on the next slide.The
approximation is much better.
24
25
Numerical Integration
26
Zero-Order Integration
z1 y1x
z2 z1 y2 x ( y1 y2 )x
z3 z2 y3x ( y1 y2 y3 )x
k
zk zk 1 yk x yn x
n 1
27
Two Zero-Order MATLAB Commands
>> z = delx*cumsum(y)
28
First-Order Integration
0 y1
z1 x
2
y1 y2
z2 z1 x
2
y2 y3
z3 z2 x
2
yk 1 yk
zk zk 1 x
2
29
Two First-Order MATLAB Commands
>> z = delx*cumtrapz(y)
30
Example 6(a). Determine exact area
A for following integral:
2
A 4 x dx
3
0
4
4x 4 2
A x (2) (0) 16
4 4
4 0
31
Example 6(b). Determine approximate
area A1 with zero-order integration
algorithm and step-size of 0.05.
>> delx = 0.05;
>> x = 0:delx:2;
>> y = 4*x.^3;
>> A1 = delx*sum(y)
A1 =
16.8100
32
Example 6(c). Determine approximate
area A2 with first-order integration
algorithm and step-size of 0.05.
33
Example 7(a). Determine the exact
running integral for the following
function:
x
z sin xdx
0
z cos x 0
x
cos x ( cos 0)
1 cos x
34
Example 7(b) and (c). Determine first-
order approximation with 100 points per
cycle and plot the two functions.
>> delx = 2*pi/100;
>> x = 0:delx:2*pi;
>> y = sin(x);
>> z1 = delx*cumtrapz(y);
>> plot(x, z1, x, 1 - cos(x), ‘o’)
The plots are shown on the next slide.
35
36
Example 8
37
Acceleration versus Time Data
1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0
36.79 36.62 36.14 35.43 34.52 33.47 32.30 31.06 29.75 28.42 27.07
38
MATLAB Analysis
>> delt = 0.1;
>> t = 0:delt:2;
>> a = [ the 21 values of a];
>> v = delt*cumtrapz(a);
>> y = delt*cumtrapz(v);
>> plot(t,a,t,v,t,y)
Additional labeling was provided and the
curves are shown on the next slide.
39
40