Lecture 9 Computer Applications 0933201 Chapter 8Potential problem with integration: a function having asingularity in its slope function. <strong>The</strong> <strong>to</strong>p graph shows thefunction y x. <strong>The</strong> bot<strong>to</strong>m graph shows the derivative <strong>of</strong> y.<strong>The</strong> slope has a singularity at x 0. Figure 8.2–2<strong>The</strong> plots <strong>of</strong>y = x ,anddy/dx = 0.5/xSome Slope <strong>of</strong>the integrandsbecomes infinitevalues.<strong>The</strong> slopefunction has asingularity.Z.R.K8-7 More? See pages 468, 475, 476.Although the quad and quadl functions are moreaccurate than trapz, they are restricted <strong>to</strong> computingthe integrals <strong>of</strong> functions and cannot be used when theintegrand is specified by a set <strong>of</strong> points. For suchcases, use the trapz function.Using the trapz function. Compute the integral sin x dx0First use 10 panels with equal widths <strong>of</strong> /10.<strong>The</strong> script file isx = linspace(0,pi,10);y = sin(x);trapz(x,y)<strong>The</strong> answer is 1.9797, which gives a relative error <strong>of</strong>100(2 - 1.9797)/2) = 1%.Z.R.KMore? See pages 471-474.8-8Numerical differentiation: Illustration <strong>of</strong> threemethods <strong>for</strong> estimating the derivative dy/dx. Figure 8.3–1<strong>MATLAB</strong> provides the diff function <strong>to</strong> use<strong>for</strong> computing derivative estimates.Its syntax is d = diff(x), where x is avec<strong>to</strong>r <strong>of</strong> values, and the result is a vec<strong>to</strong>r dcontaining the differences between adjacentelements in x.That is, if x has n elements, d will have n 1elements, whered [x(2) x(1), x(3) x(2), . . . , x(n) x(n 1)].For example, if x = [5, 7, 12, -20], thendiff(x) returns the vec<strong>to</strong>r [2, 5, -32].8-9Z.R.KZ.R.K8-10Commandd = diff(x)b = polyder(p)b = polyder(p1,p2) Returns a vec<strong>to</strong>r b containing the coefficients<strong>of</strong> the polynomial that is the derivative <strong>of</strong> theproduct <strong>of</strong> the polynomials represented byp1 and p2.[num, den] =polyder(p2,p1)8-11Numerical differentiation functions. Table 8.3–1DescriptionReturns a vec<strong>to</strong>r d containing the differencesbetween adjacent elements in the vec<strong>to</strong>r x.Returns a vec<strong>to</strong>r b containing the coefficients<strong>of</strong> the derivative <strong>of</strong> the polynomialrepresented by the vec<strong>to</strong>r p.Returns the vec<strong>to</strong>rs num and den containingthe coefficients <strong>of</strong> the numera<strong>to</strong>r anddenomina<strong>to</strong>r polynomials <strong>of</strong> the derivative <strong>of</strong>the quotient p 2 p 1 , where p1 and p2 arepolynomials.11Z.R.K8-12<strong>MATLAB</strong> provides the polyder function<strong>to</strong> use <strong>for</strong> computing derivative polynomials.Let p 1 = 5x + 2 and p 2 = 10x 2 + 4x -3.<strong>The</strong> results can be obtained as follows:>> p1 = [5, 2]; p2 = [10, 4, -3];>> dp1 = polyder(p1)>> dp2 = polyder(p2)>> prod = polyder(p1, p2)>> [num, den] = polyder(p2, p1)<strong>The</strong> results are:dp1 = [5], dp2 = [20, 4],prod = [150, 80, -7], num = [50, 40, 23],and den = [25, 20, 4].12Z.R.KZ.R.K. 2008 Page 2 <strong>of</strong> 6
Lecture 9 Computer Applications 0933201 Chapter 8Differential equations: Free and <strong>to</strong>tal step response <strong>of</strong> theequation dy/dt+y=f(t), where =0.1, f(t)=10 & y(0)=2. Figure 8.4–11- <strong>The</strong> freeresponsesolution is:y(t)=y(0)e -t/2- <strong>The</strong> <strong>for</strong>cedresponsesolution is:y(t) =M (1 - e -t/ )<strong>The</strong> <strong>to</strong>talsolution are1 + 2.Z.R.K8-13More? See pages 483-485.Numerical Methods <strong>for</strong> Differential EquationsLet dy/dt = –10y, y(0) = 2. and 0 t 0.5.<strong>The</strong> true solution is y(t) = 2 e -10t .<strong>The</strong> following script file solves and plots thesolution by using Euler method.r = -10; delta = 0.02; y(1) = 2; %?!k=0;<strong>for</strong> time = [delta:delta:0.5] %?!k = k + 1;y(k+1) = y(k) + r*y(k)*delta;endt = [0:delta:0.5]; % <strong>for</strong> true solution.y_t = 2*exp(-10*t); % true solution.plot(t,y,’o’,t,y_t),xlabel(‘t’), ...ylabel(‘y’)Z.R.K8-15Euler method solution <strong>for</strong> the free response <strong>of</strong>dy/dt = –10y, y(0) = 2. Figure 8.5–1Let dy/dt = –10y, y(0) = 2. and 0 t 0.5.<strong>The</strong> true solution is y(t) = 2 e -10t .<strong>The</strong> following script file solves and plots thesolution by using Modified Euler method.r = -10; delta = 0.02; y(1) = 2; k=0;<strong>for</strong> time = [delta:delta:0.5] %?!k = k + 1;x(k+1) = y(k) + r*y(k)*delta;y(k+1) = y(k) + r*y(k)*delta/2 ...+ r*x(k+1);endt = [0:delta:0.5]; % <strong>for</strong> true solution.y_t = 2*exp(-10*t); % true solution.plot(t,y,’o’,t,y_t),xlabel(‘t’), ...ylabel(y’)Z.R.K Z.R.K8-158-16More? See pages 490-492.Modified Euler solution <strong>of</strong> dy/dt = –10y, y(0) = 2. Figure 8.5–3Z.R.K8-17<strong>The</strong> ode solvers.When used <strong>to</strong> solve the first order Ordinarydifferential equation dy/dt = f (t, y), the basicsyntax is (using ode23 or ode45 as theexample):[t,y] = ode23(’ydot’, [t_span], y0)where ydot is the name <strong>of</strong> the function filewhose inputs must be t and y and whoseoutput must be a column vec<strong>to</strong>r representingdy/dt; that is, f (t, y). <strong>The</strong> number <strong>of</strong> rows in thiscolumn vec<strong>to</strong>r must equal the order <strong>of</strong> theequation.Z.R.K8-18More? See pages 496-499.Z.R.K. 2008 Page 3 <strong>of</strong> 6