Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CMPSC 200 Spring 2013 Lecture 38 November 18 or 20, 2013 (depending on section)"— Presentation transcript:

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

2 ODE Examples  There 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.

3 Higher Order Differential Equations  Reduce to a system of first order equations.  See example on pages 529 - 531 in your text book

4 Example of Higher Order ODE

5 Solution to Problem 13.22 (p 544)  Break into system of equations and put into function function dhdn = Blasius(n,h) dhdn(1) = h(2); % dh/dn = h2 dhdn(2) = h(3); % d2h/dn2 = h3 dhdn(2) = -0.5*h(1)*h(3); % d3h/dn3 = -0.5h1h3 dhdn= dhdn’

6 Questions

7 Roots of Equations  Find value(s) of the independent variable when the dependent variable is 0. (y = X 2 – 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 guesses  Fast, do not always work

8 Bracketing Methods  Incremental  Move from first guess to second in a series of steps.  Find positions where result of function changes sign  May find a number of ranges for roots  Bisection  Evaluate function at midpoint between two guesses.  If evaluation at midpoint = 0 (or close), then done  If 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

9 Bracketing Methods (cont)  False Position (linear interpolation).  Similar to bisection  Draw 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 stop  Repeat with guess that has opposite sign as possibility.

10 Algorithm for Incremental Search  Create a vector for x from low guess to high guess with number of increments  Calculate values for f(x) using vector  Set number of roots to 0  With a loop that goes from k =1 to k = next to last element.  Compare signs f(x k ) to f(x k+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

11 No change in sign Change in sign – record points

12 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 call  incsrch(@(x) x.^3 – x.^2 – 4*x +4, -3, 3, 300)

13 Questions ???

14 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(x low ), change low to the midpoint and repeat   Else change high guess to midpoint and repeat

15 xhigh xmid xlow 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

16 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 call  bisrch(@(x) x.^3 + 2*x.^2 – x - 2, -3, 3, 300)

17 Questions

18 Bracketing Methods (cont)  False Position (linear interpolation).  Similar to bisection  Draw 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 stop  Repeat with guess that has opposite sign as possibility.

19 Algorithm for False Position Search   Evaluate function at low and high guess and draw a line between f(x low ) and f(x high ) to determine the point (x new ), where this line crosses the x-axis   Evaluate the dependent value at x new   If zero, or within tolerance, record and stop.   Else if same sign as f(x low ), change low guess to the x new and repeat   Else change high guess to x new and repeat

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

21 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 call  falsepos(@(x) x.^3 + 2*x.^2 – x - 2, -3, 3, 0.001)

22 Questions ???


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

Similar presentations


Ads by Google