259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

16
259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files

Transcript of 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

Page 1: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

259 Lecture 16

Numerical Differentiation and Integration in MATLAB; Function M-files

Page 2: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

22

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:

Page 3: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

3

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!

Page 4: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

4

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.

Page 5: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

5

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?

Page 6: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

6

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!

Page 7: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

7

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;

Page 8: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

8

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!

Page 9: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

9

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?

Page 10: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

10

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!

Page 11: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

11

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.

Page 12: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

12

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)

Page 13: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

13

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!

Page 14: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

14

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!

Page 15: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

15

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].

Page 16: 259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.

1616

References

Using MATLAB in Calculus by Gary Jenson