MATLAB 입문 CHAPTER 8 Numerical calculus and differential equations ACSL, POSTECH 1 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Review of integration and differentiation Engineering applications f(x) A Acceleration and velocity: b v(b) a(t )dt v(0) 0 Velocity and distance: b x(b) v(t ) x(a) a a b x Capacitor voltage and current: v(b) b A f ( x)dx a 1 b i (t )dt Q (a ) C a Work expended: d W f ( x)dx 0 d W kxdx 0 2 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Integrals Properties of integrals b b a a [cf ( x) dg ( x)]dx c b a a c b a c f ( x)dx f ( x)dx f ( x)dx Definite integrals b x n dx a b a x n 1 x b b n 1 a n 1 n 1 x a n 1 n 1 n 1 x b 1 dx ln x ln b ln a x a x 2 b f ( x)dx d g ( x)dx sin xdx cos x x 2 [cos 2 cos ] 2 x This week's content handles definite integrals only the answers are always numeric Indefinite integrals cos xdx sin x see chapter 9 3 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Derivatives Integration g ( x) f ( x)dx differentiation f ( x) dg dx h( x ) f ( x ) / g ( x ) dh dg df f ( x) g ( x) dx dx dx df dg g ( x) f ( x) dh dx dx 2 dx g z f ( y ) y g (x) dz dz dy dx dy dx h( x ) f ( x ) g ( x ) : product rule : quotient rule : chain rule example dx n nx n 1 1) dx d ln x 1 dx x d sin x cos x dx d cos x sin x dx 2) d ( x 2 sin x) x 2 cos x 2 x sin x dx 3) d (sin 2 x) dy 2y 2 sin x cos x dx dx 4 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Numerical integration Rectangular integration trapezoidal integration y y 0 y=f(x) ··· 0 y=f(x) sin dx cos x 0 cos 0 cos 2 (exact solution) ··· ··· a sin dx x b a ··· x (use of the trapz function) b Numerical integration functions Command Description quad (‘function’,a,b,tol) Uses an adaptive Simpson’s rule to compute the integral of the function ‘function’ with a as the lower integration limit and b as the upper limit. The parameter tol is optional. Tol indicates the specified error tolerance. quadl (‘function’, a,b,tol) Uses Lobatto quadrature to compute the integral of the function ‘function’. The rest of the syntax is identical to quad. trapz (x,y) Uses trapezoidal integration to compute the integral of y with respect to x, where the array y contains the function values at the points contained in the array x. 5 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Rectangular, Trapezoidal, Simpson Rules Rectangular rule N 1 I h f i f(x2) f(x1) f(x) Simplest, intuitive i 0 wi = 1 Error=O(h) Mostly not good enough! x0 =a x1 x2 Trapezoidal rule Two point formula, h=b-a Linear interpolating polynomial I h( f 0 f1 ) / 2 O(h 3 ) f0 f(x) xN=b f1 Simpson’s Rule Three point formula, h=(b-a)/2 Quadratic interpolating polynomial Lucky cancellation due to right-left symmetry x0 =a 1 4 1 I h( f 0 f1 f 2 ) O(h 5 ) 3 3 3 Significantly superior to Trapezoidal rule Problem for large intervals -> Use the extended formula x1=b 6 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Matlab's Numerical Integration Functions trapz(x,y) trapezoidal integration method not as accurate as Simpson's method very simple to use quad('fun',a,b,tol) When you have a vector of data adaptive Simpson’s rule integral of ’fun’ from a to b tol is the desired error tolerance and is optional quad1('fun',a,b,tol) When you have a function file Alternative to quad adaptive Lobatto integration integral of ’fun’ from a to b tol is the desired error tolerance and is optional 7 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Comparing the 3 integration methods test case: 0 sin dx cos x 0 cos 0 cos 2 (exact solution) Trapezoid Method: x=linspace(0,pi, 10); y=sin(x); trapz(x,y); Simpson's Method: quad('sin',0,pi); ( Lobatto's Method quadl('sin',0,pi); The answer is A1=A2=0.6667, A3=0.6665 8 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech To obtain a vector of integration results: For example, sin(x) is the integral of the cosine(z) from z=0 to z=x In matlab we can prove this by calculating the quad integral in a loop: for k=1:101 x(k)= (k-1)* pi/100; sine(k)=quad('cos',0,x(k)); end plot(x, sine) % x vector will go from 0 to pi % this calculates the integral from 0 to x % this shows the first half of a sine wave % calculated by integrating the cosine 9 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Using quad( ) on homemade functions 1) Create a function file: function c2 = cossq(x) % cosine squared function. c2 = cos(x.^2); Note that we must use array exponentiation. 2) The quad function is called as follows: quad('cossq',0,sqrt(2*pi)) 3) The result is 0.6119. 10 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech 11 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Numerical differentiation dy y lim dx Δx 0 x True slope y3 y=f(x) y2 B A y1 Δx x1 Δx x2 y 2 y1 y 2 y1 x 2 x1 x y3 y2 y3 y2 mB x3 x 2 x mA C x3 mC : backward difference : forward difference mA mB 1 y 2 y1 y 3 y 2 y 3 y1 : central difference 2 2 x x 2x 12 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations The diff function The diff Function d= diff (x) example x=[5, 7, 12, -20]; d= diff(x) d=2 5 -32 Backward difference & central difference method example 13 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Try that: Compare Backward vs Central Construct function with noise x=[0:pi/50:pi]; n=length(x); % y=sin(x) +/- .025 random error y=sin(x) + .05*(rand(1,51)-0.5); td=cos(x); % true derivative Backward difference using diff: d1= diff(y)./diff(x); subplot(2,1,1) plot(x(2:n),td(2:n),x(2:n),d1,'o'); xlabel('x'); ylabel('Derivative'); axis([0 pi -2 2]) title('Backward Difference Estimate') Central Difference d2=(y(3:n)-y(1:n-2))./(x(3:n)-x(1:n-2)); subplot(2,1,2) plot(x(2:n-1),td(2:n-1),x(2:n-1),d2,'o'); xlabel('x'); ylabel('Derivative'); axis([0 pi -2 2]) title('Central Difference Estimate'); 14 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Analytical solutions to differential equations(1/6) Solution by Direct Integration Ordinary differential equation (ODE) example dy 2 t dt t dy t3 t t3 2 dt t dt 0 dt 0 3 0 3 t y (t ) y (0) t3 3 dy dt d2y y(t ) 2 dt y (t ) Partial differential equation (PDE) -- not covered 15 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Analytical solutions to differential equations(2/6) Oscillatory forcing function dy f (t ) dt Forcing function example f (t ) sin wt t dy cos wt t 1 cos wt dt sin wtdt 0 dt 0 w 0 w t 1 cos wt y (t ) y (t ) y (0) 0 w 1 cos wt y (t ) y (0) w A second-order equation t d2y 3 t dt 2 t d2y dy t4 3 0 dt 2 dt dt y (0) 0 t dt 4 t dy t 4 y (0) dt 4 4 t t dy t5 dt y ( t ) y ( 0 ) y ( 0 ) dt ty (0) 0 dt 0 4 20 t y(t ) t 5 / 20 ty (0) y(0) 16 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Analytical solutions to differential equations(3/6) Substitution method for first-order equations dy y f (t ) ( y(t ) Aest , dy / dt sAe st ) dt f (t ) 0 dy y sAe st Ae st 0 dt s 1 0 s 1/ y(0) Ae0 A y(t ) y(0)e t / Characteristic equation Characteristic root to find A suppose f (t ) 0 t 0 for M at t=0 Such a function is the step function y (t ) Ae st B B y (0) A y(t ) Aest y(0) A ( s 1 0 , A y (0) M ) y(t ) y(0)e t / M (1 e t / ) Forced response Solution, Free response ∵The solution y(t) decays with time if τ>0 τ is called the time constant. Natural (by Internal Energy) v.s. Forced Response (by External Energy) Transient (Dynamics-dependent) v.s. Steady-state Response (External Energy-dependent) 17 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech solve on paper then plot (next week we solve with Matlab) Use the method in previous slide 18 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech solve on paper then plot (next week we solve with Matlab) Re-arrange terms: mv' + cv = f , OR (m/c) v' + v = f/c now it's in the same form as 2 slides back 19 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Analytical solutions to differential equations(4/6) Nonlinear equations example yy 5 y y 0 y sin y 0 y y 0 Substitution method for second order equations(1/3) 1) y c 2 y 0 ( y(t ) Aest ( s 2 c 2 ) Ae st 0 s c y(t ) A1ect A2e ct suppose that ) general solution c 2, y (0) 6, y (0) 4 y(0) 6 A1 A2 y (0) 2 A1 2 A2 4 solution A1 4, A2 2 20 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Analytical solutions to differential equations(5/6) Substitution method for second order equations(2/3) 2) y 2 y 0 ( y(t ) Aest ) y(t ) A1eit A2e it Euler’s identities general solution e it cos t i sin t y(t ) B1 sin t B2 cos t B1 y (0) / , B2 y(0) y (t ) period 3) y (0) P sin t y (0) cos t 2 frequency my cy ky f (t ) ( f 1/ P f (t ) 0, y(t ) Aest ) (ms2 cs k ) Aest 0 1. Real and distinct: s1 and s2. y(t ) A1e s1t A2e s2t 2. Real and equal: s1. y(t ) ( A1 tA2 )e s1t 3. Complex conjugates: s a i y(t ) B1e sin t B2e at cos t at 21 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Analytical solutions to differential equations(6/6) Substitution method for second order equations(3/3) 1. real, distinct roots: m=1,c=8, k=15. characteristic roots s=-3,-5. y(t ) A1e 3t A2e 5t 2. complex roots: m=1,c=10,k=601 characteristic roots s 5 24i y(t ) B1e5t sin 24t B2e5t cos 24t P 2 / 24 / 12 3. unstable case, complex roots: m=1,c=-4,k=20 s 2 4i y(t ) B1e 2t sin 4t B2e 2t cos 4t P 2 / 4 / 2 characteristic roots 4. unstable case, real roots: m=1,c=3,k=-10 characteristic roots s=2,-5. y(t ) A1e 2t A2e 5t 22 MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Problem 22 Just find the roots to the characteristic equation and determine which form the free response will take (either 1, 2, 3 or 4 in previous slide) This alone can be a helpful check on matlab's solutions—if you don't get the right form, you can suspect there is an error somewhere in 23 your solution MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Acsl, Postech Next Week: we learn how to solve ODE's with Matlab 24