Presentation is loading. Please wait.

Presentation is loading. Please wait.

ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical calculus and differential equations.

Similar presentations


Presentation on theme: "ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical calculus and differential equations."— Presentation transcript:

1 ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical calculus and differential equations

2 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 2 Review of integration and differentiation Engineering applications  Acceleration and velocity:  Velocity and distance:  Capacitor voltage and current:  Work expended: A ab x f(x)

3 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 3 Integrals Properties of integrals Definite integrals Indefinite integrals This week's content handles definite integrals only the answers are always numeric see chapter 9

4 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 4 Derivatives Integration differentiation example : product rule : quotient rule : chain rule 1) 2) 3)

5 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 5 Numerical integration Rectangular integration trapezoidal integration Numerical integration functions CommandDescription 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. y x a b y=f(x) y x a b ··· (exact solution) (use of the trapz function)

6 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 6 Rectangular, Trapezoidal, Simpson Rules Rectangular rule  Simplest, intuitive  wi = 1  Error=O(h) Mostly not good enough! Trapezoidal rule  Two point formula, h=b-a  Linear interpolating polynomial Simpson’s Rule  Three point formula, h=(b-a)/2  Quadratic interpolating polynomial  Lucky cancellation due to right-left symmetry  Significantly superior to Trapezoidal rule  Problem for large intervals -> Use the extended formula x 0 =a x N =b f(x) x1x1 x2x2 f(x 1 ) f(x 2 ) x 0 =a x 1 =b f(x) f0f0 f1f1

7 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Matlab's Numerical Integration Functions trapz(x,y) When you have a vector of data  trapezoidal integration method  not as accurate as Simpson's method  very simple to use quad('fun',a,b,tol)When you have a function file  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) Alternative to quad  adaptive Lobatto integration  integral of ’fun’ from a to b  tol is the desired error tolerance and is optional 7

8 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 8 Comparing the 3 integration methods The answer is A1=A2=0.6667, A3=0.6665 test case: Trapezoid Method: x=linspace(0,pi, 10); y=sin(x); trapz(x,y); (exact solution) ( Simpson's Method: quad('sin',0,pi); Lobatto's Method quadl('sin',0,pi);

9 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 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; % x vector will go from 0 to pi sine(k)=quad('cos',0,x(k)); % this calculates the integral from 0 to x end plot(x, sine) % this shows the first half of a sine wave % calculated by integrating the cosine 9

10 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Using quad( ) on homemade functions 10 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.

11 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 11

12 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 12 Numerical differentiation x1x1 x2x2 x3x3 y1y1 y2y2 y3y3 ΔxΔx ΔxΔx y=f(x) True slope A B C Δx 0 : backward difference : central difference : forward difference

13 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 13 The diff function The diff Function d= diff (x) Backward difference & central difference method example example x=[5, 7, 12, -20]; d= diff(x) d = 2 5 -32

14 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

15 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 15 Analytical solutions to differential equations(1/6) Solution by Direct Integration  Ordinary differential equation (ODE) example  Partial differential equation (PDE) -- not covered

16 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 16 Analytical solutions to differential equations(2/6) Oscillatory forcing function A second-order equation Forcing function example

17 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 17 Analytical solutions to differential equations(3/6) Substitution method for first-order equations Characteristic equation Characteristic root ( ), to find A Solution, Free response ∵ The solution y(t) decays with time if τ>0 τ is called the time constant. suppose for M at t=0 Such a function is the step function ( ), Forced response Natural (by Internal Energy) v.s. Forced Response (by External Energy) Transient (Dynamics-dependent) v.s. Steady-state Response (External Energy-dependent)

18 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations solve on paper then plot (next week we solve with Matlab) Use the method in previous slide 18

19 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 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

20 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 20 Analytical solutions to differential equations(4/6) Nonlinear equations Substitution method for second order equations(1/3) example () general solution suppose that solution 1)

21 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 21 Analytical solutions to differential equations(5/6) Substitution method for second order equations(2/3) ( ) general solution Euler’s identities period frequency 2) 3) () 1. Real and distinct: s 1 and s 2. 2. Real and equal: s 1. 3. Complex conjugates:

22 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 22 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. 2. complex roots: m=1,c=10,k=601 characteristic roots 3. unstable case, complex roots: m=1,c=-4,k=20 characteristic roots 4. unstable case, real roots: m=1,c=3,k=-10 characteristic roots s=2,-5.

23 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations 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 your solution 23

24 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical calculus and differential equations Next Week: we learn how to solve ODE's with Matlab 24


Download ppt "ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical calculus and differential equations."

Similar presentations


Ads by Google