Function M

advertisement
259 Lecture 16
Numerical Differentiation and
Integration in MATLAB;
Function M-files
Derivatives and Integrals
 We can use MATLAB to numerically
differentiate or integrate functions!
 The key is to remember the
definitions of derivative and definite
integral:
2
Numerical Differentiation
 For small x, we have the following
estimate for f’(x):
 Using the MATLAB commands
“linspace” and “diff”, we can find
reasonable approximations to f’(x),
provided x is small and f is
differentiable!
3
Numerical Differentiation
 Example 1:
 Use MATLAB to find the numerical
derivative of y = sin(x) on the
interval [0, 2].
 Compare this estimate for dy/dx to
the actual derivative of y.
4
Numerical Differentiation
 Example 1 (cont.):
 Try each of the
following commands
with a = 0, b = 2*pi,
and n = 50.
 linspace(a, b, n)
 a:(b-a)/(n-1):b
 What do you notice?
 Try “linspace(a,b)”.
 What happens in this
case?
5
Numerical Differentiation
 Example 1 (cont.):
 Next, try these
commands:


x = linspace(1, 10, 10)
diff(x)
 What happens?
 In general, for x =
[a, b, c, d], diff(x) =
[b-a, c-b, d-c].
 Now we are ready to
estimate dy/dx!
6
Numerical Differentiation
 Example 1 (cont.):
 Enter the following
commands to create
an estimate for
dy/dx, which we’ll
call yprime:





x = linspace(0, 2*pi, 1000);
y = sin(x);
deltax = diff(x);
deltay = diff(y);
yprime = deltay./deltax;
7
Numerical Differentiation
 Example 1 (cont.)
 To compare our estimate
to the actual derivative,
let’s look at a table of
the first 10 values of
yprime and cos(x), via
concatenation:

[yprime(1:10); cos(x(1:10))]’.
 Note the use of the colon
(:) and transpose (‘)
commands!
8
Numerical Differentiation




Example 1 (cont.)
Let’s also compare graphically!
One way to do this is with the
“subplot” command.
Try the following commands:







subplot(1,2,1)
plot(x(1:999), yprime, 'r‘)
title(‘yprime = \Deltay/\Deltax’)
subplot(1,2,2)
plot(x(1:999), cos(x(1:999)),‘b')
title(‘dy/dx = cos(x)’)
Why can’t we just use “x,
yprime”, etc. in our plot
commands?
9
Numerical Integration
 For integrable function f(x), choosing xi =
(b-a)/n and xi* to be the right endpoint of the
ith subinterval, i.e. xi*=a+i*(b-a)/n, we get the
following estimate for large n:
 Using the “sum” command, we can find an
estimate for definite integrals via Riemann
sums!
10
Numerical Integration
 Example 2:
 Use MATLAB to numerically estimate
the definite integral
 Compare this estimate as n gets
larger to the actual value for the
integral.
11
Numerical Integration
 Example 2 (cont.)
 Enter the following
commands to compute
Riemann sums for our
integral!






a = 0;
b = 1;
n = 50;
deltax = (b-a)/n;
xstar = a+deltax:deltax:b;
Rn = sum((xstar.*xstar)
*deltax)
12
Function M-Files
 In addition to using M-files to run
scripts, we can use them to create
functions!
 Function M-files can accept input and
produce output. One example of a
function defined by a function M-file
is “linspace”.
 Let’s make a function via an M-file!
13
Function M-Files
 Within the M-file script
editor, type the
following:




function y = Sample1(x)
%Here is where you put
in information about how
the function is used.
%The syntax and
variables can be outlined
here as well.
y = x + x.^2 – x.^4;
 Save the file as
Sample1.m on the
Desktop and make sure
the Path is set to see the
file!
14
Function M-Files
 To use the new
function, for example
to find the value of
Sample1(x) at x = 3,
type:
 Sample1(3)
 Find the numerical
derivative of
Sample1(x) and plot
both y = Sample1(x)
and its numerical
derivative on [-1,1].
15
References
 Using MATLAB in Calculus by Gary
Jenson
16
Download