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

CMPSC 200 Spring 2013 Lecture 38

  • Upload
    yan

  • View
    23

  • Download
    2

Embed Size (px)

DESCRIPTION

CMPSC 200 Spring 2013 Lecture 38. November 18 or 20 , 2013 (depending on section). ODE Examples. There are times when you may need to solve ODE for systems of equations or higher order ODEs. - PowerPoint PPT Presentation

Citation preview

Page 1: CMPSC 200  Spring 2013 Lecture 38

CMPSC 200 Spring 2013

Lecture 38

November 18 or 20 , 2013(depending on section)

Page 2: CMPSC 200  Spring 2013 Lecture 38

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.

Page 3: CMPSC 200  Spring 2013 Lecture 38

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

book

Page 4: CMPSC 200  Spring 2013 Lecture 38

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

Page 5: CMPSC 200  Spring 2013 Lecture 38

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’

Page 6: CMPSC 200  Spring 2013 Lecture 38

Questions

Page 7: CMPSC 200  Spring 2013 Lecture 38

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

Page 8: CMPSC 200  Spring 2013 Lecture 38

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

Page 9: CMPSC 200  Spring 2013 Lecture 38

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.

Page 10: CMPSC 200  Spring 2013 Lecture 38

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

Page 11: CMPSC 200  Spring 2013 Lecture 38

No change in sign

Change in sign – record points

Page 12: CMPSC 200  Spring 2013 Lecture 38

Function for Incremental SearchPass 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)

Page 13: CMPSC 200  Spring 2013 Lecture 38

Questions ???

Page 14: CMPSC 200  Spring 2013 Lecture 38

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

Page 15: CMPSC 200  Spring 2013 Lecture 38

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

Page 16: CMPSC 200  Spring 2013 Lecture 38

Function for Binary SearchPass 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)

Page 17: CMPSC 200  Spring 2013 Lecture 38

Questions

Page 18: CMPSC 200  Spring 2013 Lecture 38

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.

Page 19: CMPSC 200  Spring 2013 Lecture 38

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 repeatElse change high guess to xnew and repeat

Page 20: CMPSC 200  Spring 2013 Lecture 38

xlow

xhigh

xhigh

xnew

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

xnew

Page 21: CMPSC 200  Spring 2013 Lecture 38

Function for False Position SearchPass 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)

Page 22: CMPSC 200  Spring 2013 Lecture 38

Questions ???