Download presentation
Presentation is loading. Please wait.
Published byMaurice Carson Modified over 8 years ago
1
CMPSC 200 Fall 2013 Lecture 37 November 18, 2013
2
Numerical Differentiation Suppose you have a set of measured data, T and P and you want to find the first derivative of the using this data. First derivative is just the slope so you could calculate the slope ( P/ T) for each pair of points (P 2 – P 1 )/(T 2 - T 1 ), (P 3 – P 2 )/(T 3 – T 2 ), (P 4 – P 3 )/(T 4 – T 3 ), etc. Use MATLAB diff function: slope = diff(P)./ diff(T)
3
Plotting the slope If you want to plot the slope as a function of T Cannot plot slope vs. T because number of points are different If plot slope vs. T(2: length(T)) or slope vs T(1:length(T)-1) would not position the slope correctly Plot slope vs. middle of T pairs T(1:length(T) -1) + diff(T)/2 or T(2:length(T)) – diff(T)/2 or T(2:end) – diff(T)/2
4
Questions ???
5
Numerical Integration Integration is often referred to as the area under a curve and can represent entities that you may want to measure. If you do not have a function that meets a set of points or you have a function that is not easily integrated, you can use numerical methods and a set of points that you have collected.
7
A = y 1 (X 2 -X 1 )A = 0.5(y 2 +y 1 )(X 2 -X 1 ) A = y 1 (X 2 -X 1 ) + 0.5(y 2 -y 1 )(x 2 -x 1 ) A = y 2 (X 2 -X 1 ) Over the entire range: sum(diff(x).* (y(1:length(y)-1) + diff(y)/2))
8
quad and quadl functions MATLAB has two built-in functions accept a function or function handle and two points to calculate the area. The quad function uses Simspon’s quadrature to estimate the area. The quadl function uses Lobatto’s quadrature to estimate the area.
9
Reference for this portion Much of this portion of the lecture is based on material from Applied Numerical Methods with MATLAB for Engineers and Scientists by Steven Chapra.
10
Simpson’s 1/3 rule Use a point midway between the values that you want to integrate. Then fit the three points with a parabola and estimate the area under the parabola. Example: Let x o be the first point x 2 the second point and x 1 be half way between the two. Then the area would be x 2 - x 0 2 f(x 0 ) + 4f(x 1 ) + f(x 2 ) 3 = (x 2 - x 0 )(f(x 0 ) + 4f(x 1 ) + f(x 2 )) 6 h 3 f(x 0 ) + 4f(x 1 ) + f(x 2 )where h = x 2 - x 0 2 b - a 2 or *
11
Graphical Example a f(a) b f(b)
12
MATLAB Example Use function from 2x 2 + 3x + 5 from x =1 to x = 3 as a demo Use ginput if you do not know function.
13
Simpson’s 3/8 rule Use 2 points equal distance between the values that you want to integrate. Then fit the four points with a parabola and estimate the area under the parabola. Example: Let x o be the first point and x 3 the second point and x 1 be 1/3of the way between the two and x2 would be 2/3 of the way. Then the area would be (x 3 – x o )(f(x 0 ) + 3f(x 1 ) + 3f(x 2 ) + f(x3)) 8 where h = x 3 - x 0 3 b - a 3 or 3h 8 f(x 0 ) + 3f(x 1 ) + 3f(x 2 ) + f(x 3 )
14
Composites If you have more than 3 or 4 points you can combine techniques. Examples Suppose we knew f(x) for 5 points. Use Simpson’s 1/3 rule for x 0 – x 2 then add to Simpson’s 1/3 rule for x 2 – x 4. (Note x 2 is used twice) Suppose we knew f(x) for 6 points. Use Simspon’s 1/3 rule for x 0 – x 2 and Simpson’s 3/8 rule for x 2 – x 5. For 7 points we could use Simpson’s 3/8 rule for x 0 – x 3 and again for x 3 – x 6.
15
Example – know 5 Points (x 0 – x 4 ) Use first 3 points to calculate one area, use last 3 points to calculate second area and sum the two. Note the 3 point is used for both areas. (x 2 -x 0 )(f(x 0 ) + 4f(x 1 ) + f(x 2 ))/6 + (x 4 -x 2 )(f(x 2 ) + 4f(x 3 ) + f(x 4 ))/6
16
Questions
17
Differential Equations There are a number of functions (solvers) built into MATLAB to solve ordinary differential equations. (See page 512 of your textbook) Each of these solvers require a function handle to describe the function in terms of 2 variables, a range of interest for first variable, and an initial condition of the second variable. Example dy/dt = f(t,y)
18
Function Handle A nickname for a function or reference to a function *.m file Examples: my_fun = @(t,y) 4*t could be used specifying dy/dt = 4*t Or @function name if you refer to *.m function file
19
Using ODE functions [t,y] = odefunction(func. handle, t-range, init y val) [t,y] =ode45(my_fun, [-1,1], 2); % calculates 45 points from -1 to 1 for t % calculates 45 points for y solving y = 2t 2 + c % where c is a constant which is 0 for this % example. [t,y] = ode45(my_fun,[-1,2],2); % calculates 45 points t = -1 to 2 and y = 2t 2 + 0 [t,y] = ode45(my_fun, [-1,1], 3) % calculates 45 points t = -1 to 1 and y = 2t 2 + 1
20
Higher Order Differential Equations Reduce to a system of first order equations. See example on pages 529 - 531 in your text book
21
Questions
22
Roots of Equations Find value(s) of the independent variable when the dependent variable is 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
23
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
24
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.
25
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
26
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)
27
Questions ???
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.