22
CMPSC 200 Spring 2013 Lecture 38 November 18 or 20 , 2013 (depending on section)

CMPSC 200 Spring 2013 Lecture 38 November 18 or 20, 2013 (depending on section)

Embed Size (px)

Citation preview

CMPSC 200 Spring 2013

Lecture 38

November 18 or 20 , 2013(depending on section)

ODE ExamplesThere are times when you may need to solve

ODE for systems of equations or higher order ODEs.

Create a function that accepts two variables as inputs (often t and y) and returns a column vector as output.

Higher Order Differential EquationsReduce to a system of first order equations.See example on pages 529 - 531 in your text

book

Example of Higher Order ODEProblem 13.22 in your textbook gives the Blasius

equation for laminar flow as = 0

Let h1 = f, h2 = df/dn, and h3 = d2f/dn2 thendh1/dn = h2,

dh2/dn = h3

Substitute into the equation and solve for dh3/dn

2 dh3/dn + h1h3 = 0 dh3/dn = -0.5 h1h3

Solution to Problem 13.22 (p 544)Break into system of equations and put into

functionfunction dhdn = Blasius(n,h)dhdn(1) = h(2); % dh/dn = h2dhdn(2) = h(3); % d2h/dn2 = h3dhdn(2) = -0.5*h(1)*h(3); % d3h/dn3 = -0.5h1h3dhdn= dhdn’

Questions

Roots of Equations Find value(s) of the independent variable when the

dependent variable is 0. (y = X2 – 1 for what values of X is y = 0)

Use graphical methods and ginput. Use “Bracketing Methods”

Need two “guesses” where the values of the of the dependent variable change signs. Therefore you know that a root is inside range

Slow, but usually work Use “Open Methods”

Need one or more guessesFast, do not always work

Bracketing MethodsIncremental

Move from first guess to second in a series of steps.Find positions where result of function changes signMay find a number of ranges for roots

BisectionEvaluate function at midpoint between two guesses.If evaluation at midpoint = 0 (or close), then doneIf first guess and midpoint have same sign, then repeat

with midpoint and second guess. Otherwise repeat with first guess and midpont.

Continue until reach desired precision

Bracketing Methods (cont)False Position (linear interpolation).

Similar to bisectionDraw a straight line between two guesses.Use where the line crosses the x-axis as a possibility.Evaluate function at that possibility.If evaluation at possibility is 0 or close to 0 stopRepeat with guess that has opposite sign as

possibility.

Algorithm for Incremental SearchCreate a vector for x from low guess to high guess

with number of incrementsCalculate values for f(x) using vectorSet number of roots to 0With a loop that goes from k =1 to k = next to last

element.Compare signs f(xk) to f(xk+1)

If signs are different then add 1 to the number of roots and store values of x that bracket that root.Sign function – returns -1, 0 or 1

No change in sign

Change in sign – record points

Function for Incremental Search

Pass function, low value, high value and number of increments to function.

Create a function handle, then use it in the function call.myfun = @(x) x.^3 – x.^2 – 4*x +4 incsrch(myfun, -3, 3, 300)

Use the function in the function callincsrch(@(x) x.^3 – x.^2 – 4*x +4, -3, 3, 300)

Questions ???

Algorithm for Bisection Search

Determine the midpoint between the low and high guesses for the independent variable.

Evaluate the dependent value at this midpoint.If zero, or within tolerance, record and stop.Else if same sign as f(xlow), change low to the midpoint

and repeatElse change high guess to midpoint and repeat

xhigh

xmidxlow

f(xmid) is not to 0 and is the same sign as f(xlow), reset xlow to xmid

xlow

F(xmid is not zero and is the same sign as f(xhigh), reset xhigh to xmid

xhigh

xmid

xmid

Function for Binary Search

Pass function, low value, high value and tolerance

Create a function handle, then use it in the function call.myfun = @(x) x.^3 + 2*x.^2 – x - 2 bisrch(myfun, -3, 3, 300)

Use the function in the function callbisrch(@(x) x.^3 + 2*x.^2 – x - 2, -3, 3, 300)

Questions

Bracketing Methods (cont)False Position (linear interpolation).

Similar to bisectionDraw a straight line between two guesses.Use where the line crosses the x-axis as a possibility.Evaluate function at that possibility.If evaluation at possibility is 0 or close to 0 stopRepeat with guess that has opposite sign as

possibility.

Algorithm for False Position Search

Evaluate function at low and high guess and draw a line between f(xlow) and f(xhigh) to determine the point (xnew), where this line crosses thex-axis

Evaluate the dependent value at xnew If zero, or within tolerance, record and stop.Else if same sign as f(xlow), change low guess to the

xnew and repeat

Else change high guess to xnew and repeat

xlow

xhigh

xhigh

xnew

F(xnew) is not zero and is the same sign as xhigh, reset xhigh to xnew

xnew

Function for False Position Search

Pass function, low value, high value and tolerance

Create a function handle, then use it in the function call.myfun = @(x) x.^3 + 2*x.^2 – x - 2 falsepos(myfun, -3, 3, 300)

Use the function in the function callfalsepos(@(x) x.^3 + 2*x.^2 – x - 2, -3, 3, 0.001)

Questions ???