220 likes | 306 Views
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.
E N D
CMPSC 200 Spring 2013Lecture 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. • Create a function that accepts two variables as inputs (often t and y) and returns a column vector as output.
Higher Order Differential Equations • Reduce to a system of first order equations. • See example on pages 529 - 531 in your text book
Example of Higher Order ODE • Problem 13.22 in your textbook gives the Blasius equation for laminar flow as = 0 • Let h1 = f, h2 = df/dn, and h3 = d2f/dn2then • dh1/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 functionfunctiondhdn = 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’
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 guesses • Fast, do not always work
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
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.
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(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
Change in sign – record points No change in sign
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)
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 repeat Else change high guess to midpoint and repeat
xlow f(xmid) is not to 0 and is the same sign as f(xlow), reset xlow to xmid xmid xhigh xmid xmid xhigh xlow F(xmid is not zero and is the same sign as f(xhigh), reset xhigh to 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 call • bisrch(@(x) x.^3 + 2*x.^2 – x - 2, -3, 3, 300)
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.
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
F(xnew) is not zero and is the same sign as xhigh, reset xhigh to xnew xhigh xlow xnew xnew xhigh
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)