MATLAB Demystified phần 7 docx

33 204 0
MATLAB Demystified phần 7 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

188 MATLAB Demystifi ed Solving Second Order Equations Now let’s see how to solve second order equations numerically. The trick used here is to break the equation down into a system of two equations. So fi rst, let’s see how to solve a system of equations. EXAMPLE 7-2 Solve the system dx dt xy dy dt xxy=− + =− − 2 , with initial conditions x(0) = 0, y(0) = 1. Plot both functions and generate a phase plot. SOLUTION 7-2 First we create a function like before that will implement the right-hand side of the differential equation. This time, of course, we have a system so need a 2 × 1 column vector. The function looks like this: function xdot = eqx(t,x); %allocate array to store data 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 t y(t) Figure 7-6 In the second case examined in Example 7-1, we get a nice plot showing the response lagging the forcing function, with both functions having the same general shape CHAPTER 7 Numerical Solution of ODEs 189 xdot = zeros(2,1); xdot(1) = -x(1)^2 + x(2); xdot(2) = -x(1) - x(1)* x(2); So far so good, pretty simple. Now let’s use ode45 to generate a solution with the given initial conditions: >> [t,x] = ode45('eqx',[0 10],[0,1]); Now let’s generate a plot. We access the fi rst function with x(:,1) and the second by writing x(:,2). The plot command is: >> plot(t,x(:,1),t,x(:,2),' '),xlabel('t'), axis([0 10 –1.12 1.12]) The plot of the two functions is shown in Figure 7-7. Now let’s generate the phase plot. The command is: >> plot(x(:,1),x(:,2)) The phase plot is shown in Figure 7-8. Now that we know how to solve a system, we can use ode45 to solve a second order equation. 0 1 2 3 4 5 6 7 8 9 10 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 t Figure 7-7 The functions x (solid line) and y (dashed line) that solve the system in Example 7-2 190 MATLAB Demystifi ed EXAMPLE 7-3 Find a solution of y′′ + 16y = sin(4.3t) when y(0) = y′(0) = 0. SOLUTION 7-3 We can change this into a system of fi rst order equations. First we set: x 1 = y x 2 = y′ Hence: x 1 ′ = y′ = x 2 x 2 ′ = y′′ = sin(4.3t) − 16x 1 Now we create a function to implement this system: function xdot = eqx2(t,x); %allocate array to store data xdot = zeros(2,1); xdot(1) = x(2); xdot(2) = sin(4.3*t)–16*x(1); −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 x y Figure 7-8 The phase portrait for the system solved in Example 7-2 CHAPTER 7 Numerical Solution of ODEs 191 Let’s call ode45 to get a solution. Since the forcing function is sinusoidal, we choose a time interval 0 ≤ t ≤ 2π: >> [t,x] = ode45('eqx2',[0 2*pi],[0,0]); Now let’s plot the functions that are returned. >> plot(t,x(:,1),t,x(:,2),' '),xlabel('t'), axis([0 2*pi –3 3]) The plot is shown in Figure 7-9. Notice that the amplitudes of the solutions are growing with time. They are also out of phase and the second solution has a much larger amplitude than the fi rst. Let’s generate a phase plot for the system: >> plot(x(:,1),x(:,2)),xlabel('x1'),ylabel('x2') The result is shown in Figure 7-10. For comparison, let’s consider the case where the initial conditions are given by y(0) = y′(0) = 1. Let’s redo the solution and plot it: >> [t,x] = ode45('eqx2',[0 2*pi],[1,1]); >> plot(t,x(:,1),t,x(:,2),' '),xlabel('t'), axis([0 2*pi –4 4]) The plot is shown in Figure 7-11. 0 1 2 3 4 5 6 −3 −2 −1 0 1 2 3 t Figure 7-9 The system solved in Example 7-2. Notice it exhibits beats 192 MATLAB Demystifi ed Notice that by changing the initial conditions, we have increased the amplitude of the oscillations by a large fraction—the functions get bigger earlier. The behavior appears more regular for x 1 . Let’s take a closer look at this and compare x 1 with cos(4.3t) in a single plot, shown in Figure 7-12. −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 −3 −2 −1 0 1 2 3 x1 x2 Figure 7-10 A phase portrait for the system in Example 7-2 with initial conditions given by y(0) = y′(0) = 0 0 1 2 3 4 5 6 −4 −3 −2 −1 0 1 2 3 4 t Figure 7-11 Reworking Example 7-2 by setting the initial condition to y(0) = y′(0) = 1 CHAPTER 7 Numerical Solution of ODEs 193 The two functions start off pretty close, but diverge as time goes on. But compare this to the fi rst case, when y(0) = y′(0) = 0. In that case the solution had little resemblance to something we know well as shown in Figure 7-13. Let’s take a look at the beating phenomenon by solving over a larger time interval: >> [t,x] = ode45('eqx2',[4*pi 20*pi],[0,0]); >> plot(t,x(:,1)),xlabel('t') The result is shown in Figure 7-14, where you can see the familiar form of a system with beats. Let’s return to the case where y(0) = y′(0) = 1. In Figure 7-15, we show the plot of the solution over the same time interval shown in Figure 7-14. Look closely and you will notice that in Figure 7-14 which shows the plot for the case of y(0) = y′(0) = 0, there is a phase reversal each time the system goes down near zero oscillation and starts to build up again. This feature is not present when y(0) = y′(0) = 1. Finally, we generate the phase plot for the case of y(0) = y′(0) = 1. This is shown in Figure 7-12. Notice the difference as compared to Figure 7-10. What does that tell you about the solutions? 0 1 2 3 4 5 6 7 −1 −0.5 0 0.5 1 1.5 t x1 cos(4.3t) Figure 7-12 Comparing the solution x 1 with cos(4.3t) when y(0) = y′(0) = 1 194 MATLAB Demystifi ed 10 20 30 40 50 60 70 −1 −0.8 −0.6 −0.4 −0.2 0.2 0.4 0.6 0.8 1 t 0 Figure 7-14 Beats of the solution for the case of y(0) = y′(0) = 0 0 1 2 3 4 5 6 7 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 t x1 cos(4.3t) sin(4.3t) Figure 7-13 The solid line represents the solution when y(0) = y′(0) = 0. It bears little resemblance to the forcing trig function or its derivative CHAPTER 7 Numerical Solution of ODEs 195 10 20 30 40 50 60 70 −1.5 −1 −0.5 0 0.5 1 1.5 t Figure 7-15 When y(0) = y′(0) = 1, the amplitude of the solution shrinks and then gets larger again—but there is no phase reversal −1 −0.5 0 0.5 1 1.5 −4 −3 −2 −1 0 1 2 3 4 x1 x2 Figure 7-16 Phase portrait for the system in Example 7-2, with y(0) = y′(0) = 1 196 MATLAB Demystifi ed Quiz 1. Find a solution of the system: x 1 ′ = 2x 1 x 2 x 2 ′ = −x 1 2 x 1 (0) = x 2 (0) = 0 2. Find a solution of the system: x 1 ′ = x 2 x 2 ′ = −x 1 x 1 (0) = x 2 (0) = 1 3. Solve: y′ = −2.3y, y(0) = 0 4. Use ode23 and ode45 to solve: y′ = y, y(0) = 1 How many points are returned by ode23 and ode 45? 5. Solve y′ = −ty + 1, y(0) = 1. 6. Solve y′ = t 2 y = 0, y(0) = 1 for 0 ≤ t ≤ 2. 7. What numerical technique is used by ode23 and ode45? 8. Solve dy dt t ty= − −<< = 2 1 1101 2 ,,(). 9. Solve y′′ −2y′ + y = exp(−t), y(0) = 2, y′(0) = 0. 10. Generate a phase portrait for the system in problem 9. CHAPTER 8 Integration This chapter covers methods that can be used to compute integrals. We will begin with integration of symbolic expressions and then consider numerical integration. The Int Command Let f be a symbolic expression in MATLAB. We can derive an expression giving the indefinite integral of f by writing: int(f) The expression f can be entered by creating a variable or reference first or by directly passing a quote-delimited string to int. For example, we can show that: xdx x ∫ = 1 2 2 Copyright © 2007 by The McGraw-Hill Companies. Click here for terms of use. [...]... trapz(x,gauss) in = 1 .76 41 578 48 476 21 MATLAB Demystified 212 The actual result of this integral is 2 ∫−2 e− x dx = 2 π Erf(2) where Erf(2) is the error function MATLAB evaluates this result to: >> ac = sqrt(pi)*erf(2) ac = 1 .76 416 278 152484 We see that the results agree to four decimal places The relative error, as a percentage, is: >> d = ac – in; >> err = abs(d/ac)*100 err = 2 .79 625 479 495 079 5e–004 For the... use 200 evenly spaced points over this range: >> x = linspace(–2.2,2.2,200); >> gauss = exp(–x.^2); Integrating we obtain: >> est = trapz(x,gauss) est = 1 .76 91492 073 6586 MATLAB Demystified 214 The actual, analytic answer is: >> ac = sqrt(pi) ac = 1 .77 245385090552 Our relative error is: >> d = ac – est; >> err = abs(d/ac)*100 err = 0.18644454624112 Now 0.18% isn’t a bad error considering the analytic result... for loop: >> for k = [1:9] x(k+1) = trapz(t(k:k+1),v(k:k+1))+x(k); end The result is: Time 1 2 3 4 5 6 7 8 9 10 Position (in 1,000 feet) 0 0.08000000000000 0.18250000000000 0.31250000000000 0. 472 50000000000 0.66250000000000 0.8 875 0000000000 1.1 375 0000000000 1.40000000000000 1.66850000000000 MATLAB Demystified 216 So the sled has traveled 1668.50 feet This technique gives us the position at each second... linspace(0,1/8,100); >> y = exp(–2*x); >> r = trapz(x,y) r = 0.1105996 672 378 5 Quiz ∫ x e− ax dx dx Compute ∫ 2 x + bx + c Compute ∫ x tan x dx 1 Compute 2 3 MATLAB Demystified 218 ∫ sin 2 (∞yx )dy Compute ∫ e −3 x cos x dx 0 4 Find 5 6 Find the volume of the solid of revolution obtained by rotating the region between the curves x and x 2 for 0 ≤ x ≤ 1 7 Use MATLAB to generate the formulas for the surface area and... int( f, a, b) then MATLAB integrates over the default independent variable and returns: b ∫a f ( x ) dx = F ( b ) − F ( a ) For example: 3 1 1 5 2 ∫2 x dx = 2 x 2 3 = 2 (9 − 4) = 2 would be calculated in MATLAB by writing: >> int('x',2,3) ans = 5/2 Equivalently, if we wanted MATLAB to generate the intermediate expression we could write: 1 2 x, 2 >> F = int('x') F = 1/2*x^2 MATLAB Demystified 202 >> a... = 1 .77 241458438211 You can see that there has been an improvement in accuracy Now the relative error is: >> d = ac – est; >> err = abs(d/ac)*100 err = 0.002215 376 34854 At 0.002 % this estimate is quite a bit more accurate EXAMPLE 8-11 The velocity of a rocket sled on a track is measured once a second for 10 seconds The velocity in ft/s is found to be: t 1 2 3 4 5 6 7 8 9 10 v(t) 65 95 110 150 170 210... a little bit: >> a = double(int(sinc,–50,50)) a = 3.1032 >> b = double(int(sinc_squared,–50,50)) b = 3.12 17 MATLAB Demystified 208 Over the entire real line, this effect washes out and the functions both cover the same area Multidimensional Integration We can compute multidimensional integrals in MATLAB by using nested int statements Suppose that we wanted to compute the indefinite integral: ∫ ∫ ∫ x...198 MATLAB Demystified (leaving out the constant of integration) by writing: >> int('x') MATLAB returns: ans = 1/2*x^2 MATLAB can generate integrals that are entirely symbolic That is, instead of: >> int('x^2') ans = 1/3*x^3 Consider: >> int('x^n') ans = x^(n+1)/(n+1)... the reader to the basics of using MATLAB to work with transforms In this chapter we will introduce the laplace, fourier, and fft commands The Laplace Transform The Laplace transform of a function of time f(t) is given by the following integral: ∞ l{ f (t )} = ∫ f (t ) e − st dt 0 Copyright © 20 07 by The McGraw-Hill Companies Click here for terms of use 220 MATLAB Demystified We often denote the Laplace... 110 150 170 210 240 260 265 272 Find the total distance covered by the sled CHAPTER 8 Integration 215 SOLUTION 8-11 Velocity is related to position by: v= dx dt Hence we can find the position from the velocity by integrating: b x(t ) = ∫ v(t ) dt a To do this numerically, first let’s put the data into two arrays: >> t = [1:1:10]; >> v = [65 95 110 150 170 210 240 260 265 272 ] We can calculate the value . 6 7 8 9 10 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 t Figure 7- 7 The functions x (solid line) and y (dashed line) that solve the system in Example 7- 2 190 MATLAB Demystifi ed EXAMPLE 7- 3 Find. 2*pi –4 4]) The plot is shown in Figure 7- 11. 0 1 2 3 4 5 6 −3 −2 −1 0 1 2 3 t Figure 7- 9 The system solved in Example 7- 2. Notice it exhibits beats 192 MATLAB Demystifi ed Notice that by changing. shown in Figure 7- 12. Notice the difference as compared to Figure 7- 10. What does that tell you about the solutions? 0 1 2 3 4 5 6 7 −1 −0.5 0 0.5 1 1.5 t x1 cos(4.3t) Figure 7- 12 Comparing the

Ngày đăng: 12/08/2014, 21:20

Tài liệu cùng người dùng

Tài liệu liên quan