41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

17
41 - 4/24/2000 AME 150L 1 AME 150 L Solving Engineering Problems, Integrating Equations

Transcript of 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

Page 1: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 1

AME 150 L

Solving Engineering Problems, Integrating

Equations

Page 2: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 2

HW 16 - “Sinc Function”

• The sinc function is common in signal processing (and the signal processing libraries on the student unix cluter have sinc defined)

Page 3: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 3

SINC Sin(pi*x)/(pi*x) function

SINC(X) returns a matrix whose elements are the sinc of the elements of X, i.e.

y = sin(pi*x)/(pi*x) if x ~= 0

= 1 if x == 0

where x is an element of the input matrix and y is the resultant output element.

Page 4: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 4

Fortran-ish sinc

function y=sinc(x)

for k=1:length(x);

if x(k)==0

y(k)=1

else

y(k)=sin(pi*x(k))/(pi*x(k))

end

end

Page 5: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 5

Dirty Matlab sinc

function y=sinc(x)

y=sin(pi*(x+2*eps))./(pi*(x+2*eps))

||||| || |||||

||||| note ./ ||||| Small positive number

Page 6: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 6

Elegant Matlab sinc

function y=sinc(x)

y=ones(size(x))

index=find(x)

y(index)=sin(pi*x(index))./ ... (pi*x(index))

This is used in the signal processing toolbox

Page 7: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 7

FIND Find indices of nonzero elements.

I = FIND(X) returns the indices of the vector X that are non-zero. For example, I = FIND(A>100), returns the indices of A where A is greater than 100. See RELOP.

[I,J] = FIND(X) returns the row and column indices of the nonzero entries in the matrix X. This is often used with sparse matrices.

[I,J,V] = FIND(X) also returns a column vector of the nonzero entries in X. Note that find(X) and find(X~=0) will produce the same I and J, but the latter will produce a V with all 1's.

Page 8: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 8

Integrals of sin(x)/x

quad('sync',0,2,eps)*pi 1.41815157613256

quad8('sync',0,2,eps)*pi 1.41815157613263

dx=x(2)-x(1);y=sync(x); trapz(y)*dx*pi

dx=.1 1.4194624019

dx=.01 1.4181646662

dx=.001 1.4181517070

dx=1/8192=.000122.. 1.41815157808342

smallest possible size in student edition (16384 elements)

Page 9: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 9

Sounding Rocket

• A table of altitude vs. time for a sounding rocket is available

• Requirement: calculate the velocity and acceleration from the data

• Use the Matlab function diff

Page 10: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 10

DIFF Difference & approximate derivative

DIFF(X), for a vector X, is [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)].

DIFF(X), for a matrix X, is the matrix of column differences, [X(2:n,:) - X(1:n-1,:)].

DIFF(X), for an N-D array X, is the difference along the first non-singleton dimension of X.

Page 11: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 11

diff for arrays

DIFF(X,N) is the N-th order difference along the first non-singleton dimension (denote it by DIM). If N >= size(X,DIM), DIFF takes successive differences along dimension DIM until size(X,DIM) == 1. DIFF then takes any successive differences along the (M+1) dimension.

DIFF(X,N,DIM) is the Nth difference function along dimension DIM. If N >= size(X,DIM), DIFF returns an empty array.

Page 12: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 12

Diff - examples

h = .001; x = 0:h:pi;

diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x

diff((1:10).^2) is 3:2:19

If X = [3 7 5

0 9 2]

then diff(X,1,1) is [-3 2 -3], diff(X,1,2) is [4 -2

9 -7],

diff(X,2,2) is the 2nd order difference along the dimension 2, and diff(X,3,2) is the empty matrix.

Page 13: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 13

Interpolation

• Linear - table look-up

• Spline Interpolation– A Spline is an instrument draftsmen use

• Spline \Spline\, n. 1. … 2. A long, flexible piece of wood sometimes used as a ruler. Source: Webster's Revised Unabridged Dictionary, © 1996, 1998 MICRA, Inc.

Page 14: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 14

Cubic Spline Interpolation

• Fit a Cubic (4 adjustable constants) to three points, maintaining the slope as continuous as you pass to successive triads.– The curve passes through all of the data– The constants are fixed only over the triad of

points– The slope (derivative) varies smoothly

Page 15: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 15

SPLINE Cubic spline interpolation

YI = SPLINE(X,Y,XI) uses cubic spline interpolation to find a vector YI corresponding to XI. X and Y are the given data vectors and XI is the new abscissa vector XI.

PP = SPLINE(X,Y) returns the pp-form of the cubic spline interpolant instead, for later use with ppval, etc.

Page 16: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 16

PPVAL Evaluate piecewise polynomial

V = PPVAL(PP,XX) returns the value of the piecewise polynomial PP at the points XX. The piecewise polynomial form (pp-form) is returned by SPLINE.

Page 17: 41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.

41 - 4/24/2000 AME 150L 17

Example of a Spline

• Fit a cubic spline to a coarsely sampled sine» x = 0:10; y = sin(x);

» xi = 0:.25:10;

» yi = spline(x,y,xi);

» plot(x,y,'o',xi,yi),grid,hold

» dy=diff(yi)./diff(xi);» for k=1:length(xi)-1;xx(k)=(xi(k+1)+xi(k))/2;end;

» plot(xx,dy,'r')