MATLAB Demystified phần 6 pps

33 258 0
MATLAB Demystified phần 6 pps

Đ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

CHAPTER 6 Symbolic Calculus⁄Differential Eqs 155 EXAMPLE 6-4 Does y (t) = 3 sin t + 7 cos 5t solve y'' + y = –5 cos 2t? SOLUTION 6-4 Define our function: >> y = 3*sin(t)+7*cos(5*t); Now let’s create a variable to hold the required result: >> f = –5*cos(2*t); To enter the left-hand side of the differential equation, we create another variable: >> a = diff(y,2)+y; We use isequal to check whether the equation is satisfied: >> isequal(a,f) ans = 0 Since 0 is returned, y (t) = 3 sin t + 7 cos 5t does not solve y'' + y = –5 cos 2t. EXAMPLE 6-5 Find the minima and maxima of the function f (x) = x 3 – 3x 2 + 3x in the interval [0, 2]. SOLUTION 6-5 First let’s enter the function and plot it over the given interval: >> syms x >> f = x^3–3*x^2+3*x; >> ezplot(f, [0 2]) The plot is shown in Figure 6-4. To find local maxima and minima, we will compute the derivative and find the points at which it vanishes. The derivative is: >> g = diff(f) g = 3*x^2–6*x+3 156 MATLAB Demystifi ed As a quick aside, we can use the pretty command to make our expressions look nicer: >> pretty(g) 2 3 x – 6 x + 3 Well slightly nicer, anyway. Returning to the problem, let’s set the derivative equal to zero and find the roots: >> s = solve(g) s = 1 1 We see that there is only one critical point, since the derivative has a double root. We can see from the plot that the maximum occurs at the endpoint, but let’s prove this by evaluating the function at the critical points x = 0, 1, 2. We can substitute a value in a symbolic function by using the subs command. With a single variable this is pretty simple. If we want to set x = c, we make the call Figure 6-4 A plot of f (x) = x 3 – 3x 2 + 3x 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 x x 3 − 3x 2 + 3x CHAPTER 6 Symbolic Calculus⁄Differential Eqs 157 subs(f,c). So let’s check f for x = 0, 1, 2. We can check all three on a single line and have MATLAB report the output by passing a comma-delimited list: >> subs(f,0), subs(f,1), subs(f,2) ans = 0 ans = 1 ans = 2 Since f (2) returns the largest value, we conclude that the maximum occurs at x = 0. For fun, let’s evaluate the derivative at these three points and plot it: >> subs(g,0),subs(g,1),subs(g,2) ans = 3 ans = 0 ans = 3 Where are the critical points of the derivative? We take the second derivative and set equal to zero: >> h = diff(g) h = 158 MATLAB Demystifi ed 6*x–6 >> solve(h) ans = 1 The next derivative is: >> y = diff(h) y = 6 Since g″ > 0 we can conclude that the critical point x = 1 is a local minimum. A plot of g(x) is shown in Figure 6-5. Figure 6-5 A plot of g(x) showing the minimum we found in Example 6-5 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.5 1 1.5 2 2.5 3 x 3x 2 − 6x + 3 CHAPTER 6 Symbolic Calculus⁄Differential Eqs 159 EXAMPLE 6-6 Plot the function f (x) = x 4 –2x 3 and show any local minima and maxima. SOLUTION 6-6 First we define the function and plot it: >> clear >> syms x >> f = x^4–2*x^3; >> ezplot(f,[–2 3]) >> hold on Now compute the first derivative: >> g = diff(f) g = 4*x^3–6*x^2 We find the critical numbers by setting the first derivative equal to zero and solving: >> s = solve(g) s = 3/2 0 0 Next we compute the second derivative: >> h = diff(g) h = 12*x^2–12*x Evaluating at the first critical number which is x = 3/2 we find: >> a = subs(h,s(1)) a = 9 160 MATLAB Demystifi ed Since f ″ (3/2) = 9 > 0, the point x = 3/2 is a local minimum. Now let’s check the other critical number: >> b = subs(h,s(2)) b = 0 In this case, f ″ (0) = 0, which means that we cannot get any information about the point using the second derivative test. It can be shown easily that the point is neither a minimum nor a maximum. Now let’s fix up the plot to show the local minimum. We add the point (c, f (c)) where c = s(1) to the plot using the following command: >> plot(double(s(1)),double(subs(f,s(1))),'ro This puts a small red circle at the point (3/2, f (3/2)) on the plot. We told MATLAB to make the circle red by entering ‘ro’ instead of ‘o’, which would have added a black circle. Now let’s label the point as a local minimum. This can be done using the text command. When you call text, you pass it the x-y coordinates where you want the text to start printing, and then pass the text string you want to appear on the plot: >> text(0.8,3.2,'Local minimum') >> hold off The result is shown in Figure 6-6. Figure 6-6 A plot of f (x) = x 4 – 2x 3 identifying the local minimum found in Example 6-6 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 2.5 3 0 5 10 15 20 25 30 x x 4 − 2x 3 Local minimum CHAPTER 6 Symbolic Calculus⁄Differential Eqs 161 The dsolve Command We can solve differential equations symbolically in MATLAB using the dsolve command. The syntax for calling dsolve to find the solution to a single equation is dsolve(‘eqn’) where eqn is a text string used to enter the equation. This will return a symbolic solution with a set of arbitrary constants that MATLAB labels C1, C2, and so on. We can also specify initial and boundary conditions for the problem. Conditions are specified as a comma-delimited list following the equation as dsolve(‘eqn’,‘cond1’, ‘cond2’,…) as required. When using dsolve, derivatives are indicated with a D. Hence we enter the equation: df dt ft=− +2cos By writing: 'Df = –2*f + cos(t)' Higher derivatives are indicated by following D by the order of the derivative. So to enter the equation: ′′ + ′ =yy x257sin we would write: 'D2y + 2Dy = 5*sin(7*x)' Solving ODE’s Let’s begin by considering some trivial differential equations just to get a feel for using MATLAB to deal with ODE’s. At the most basic level, we can call dsolve and just pass the equation to it. Here we are with the most basic ODE of all: >>s = dsolve('Dy = a*y') s = C1*exp(a*t) 162 MATLAB Demystifi ed Suppose we want to plot the equation for different values of C1 and a. We can do this by specifying values for these variables and then assigning them to a new label using the subs command: >> C1 = 2; a = 4; >> f = subs(s) f = 2*exp(4*t) Initial conditions are entered in quotes after the equation. For example, suppose that: dy dt t t yt y= − = 5 02(), ( ) The call to dsolve in this case is: >> dsolve('Dy = y*t/(t–5)','y(0) = 2') ans = –2/3125*exp(t)*(t–5)^5 Second and higher order equations work analogously. For example: dy dt yy y 2 2 00102−= =− ′ =,() ,() can be entered into MATLAB by writing: >> dsolve('D2y – y = 0','y(0) = –1','Dy(0) = 2') ans = 1/2*exp(t) –3/2*exp(–t) EXAMPLE 6-7 Find a solution to the initial value problem dy dt ty=+ =307,() and plot the result for 0 ≤ t ≤ 10. CHAPTER 6 Symbolic Calculus⁄Differential Eqs 163 SOLUTION 6-7 The equation is solved easily with a single call to dsolve: >> s = dsolve('Dy = t + 3','y(0) = 7') s = 1/2*t^2+3*t+7 Now we add a call to ezplot to generate the plot: >> ezplot(s,[0 10]) The result is shown in Figure 6-7. EXAMPLE 6-8 Find the solution of: dy dt yy== 2 01,() Plot the result showing any asymptotes. Figure 6-7 A plot of the solution of the IVP dy dt ty=+ =307,() 0 1 2 3 4 5 6 7 8 9 10 0 10 20 30 40 50 60 70 80 90 t 1/2t 2 + 3t + 7 164 MATLAB Demystifi ed SOLUTION 6-8 Using MATLAB dsolve will do the dirty work for us. We find the solution is: >> s = dsolve('Dy = y^2','y(0) = 1') s = –1/(t–1) The asymptote is located at: >> d = –1/s d = t–1 >> roots = solve(d) roots = 1 Now let’s plot and hold it: >> ezplot(s) >> hold on Now we plot the asymptote: >> plot(double(roots)*[1 1], [–2 2],' ') >> hold off The result is shown in Figure 6-8. EXAMPLE 6-9 Solve the BVP given by: df dx x xx x x ff 2 222 1 22 002−− ⎛ ⎝ ⎜ ⎞ ⎠ ⎟ −= = ′ sin cos ,(), (() .00= Plot the solution for –50 < x < 50. [...]... The result is shown in Figure 6- 10 IVP Solutions 120 100 80 60 40 20 0 −1 Figure 6- 10 −0.8 −0 .6 −0.4 −0.2 0 t 0.2 0.4 0 .6 0.8 1 Plot generated using for loop to place multiple curves on a single figure MATLAB Demystified 168 EXAMPLE 6- 11 Find a solution of dy = −2 t y 2 dt Plot the solution for different initial values, letting y(0) = 0.2, ,2.0 in increments of 0.2 SOLUTION 6- 11 First we solve the equation:... function as shown in Figure 6- 9 EXAMPLE 6- 10 Find the general solution of: dy y =− dt 1− t2 and plot over –1 < t < 1 for the constant C1 = 0, 10, 20, 30 on the same graph MATLAB Demystified 166 −sin(x)/x + 3 3.1 3.05 3 2.95 2.9 2.85 −50 −40 Figure 6- 9 −30 −20 −10 0 x A plot of the solution to 10 20 30 40 50 d 2 f sin x ⎛ 2 ⎞ 2 cos x − =0 ⎜1 − ⎟ − dx 2 x ⎝ x2 ⎠ x2 SOLUTION 6- 10 We readily obtain the... 3 4 5 6 t Figure 7-1 Plot showing numerical and analytical solutions of dy = cos(t ) dt Now we calculate the relative error at each point by using a for loop to move through the data: >> for i = 1:1:size(y) err(i) = abs((f(i)–y(i))/f(i)); end Let’s look at the result: >> err err = 1.0e–003 * 0 0.0001 0.0091 0.0301 0.0743 0.2115 0.28 76 0.3780 0. 465 9 MATLAB Demystified 182 0.5597 0 .64 80 0 .69 07 0 .60 50 0.4533... phase portrait for the system MATLAB Demystified 172 sin(t) + 2cos(t) 2.5 2 1.5 1 0.5 0 −0.5 −1 −1.5 −2 −2.5 6 Figure 6- 13 −4 −2 0 t 2 4 6 Using ezplot and telling it to use different colors and styles for each curve SOLUTION 6- 12 First we call dsolve to generate a solution for the system: >> s = dsolve('2*D2x+Dx+8*x = 0','Dp = –p – 17*x','x(0)=4',' Dx(0)=0','p(0)=0') MATLAB gives a result of the form:... Demystified 182 0.5597 0 .64 80 0 .69 07 0 .60 50 0.4533 0.3 164 0.2414 0.2129 The errors are quite small in this case The largest error is: >> emax = max(err) emax = 6. 9075e–004 Let’s look at the data points returned by the ode23 solver: y = 2.0000 2.1593 2.5110 2.8080 2.9848 2.9 169 2 .63 44 2.2123 1.7999 1.4512 1.1892 1.0323 1.0115 1.15 36 1.4337 1. 768 9 1.99 96 With the precision used to present these numbers,...CHAPTER 6 Symbolic Calculus⁄Differential Eqs 165 −1/(t − 1) 1.5 1 0.5 0 −0.5 −1 −1.5 6 Figure 6- 8 −4 −2 0 t Plot of the solution of 2 4 6 dy = y 2 , y( 0 ) = 1 with asymptote dt SOLUTION 6- 9 By default, dsolve uses t as the independent variable We can tell it to use something else by tacking the independent... exp(–1/4*t)*(4/21*sin(3/4*7^(1/2)*t)*7^(1/2)+4*cos(3/4*7^ (1/2)*t)) CHAPTER 6 Symbolic Calculus⁄Differential Eqs 173 Position of mass 4 3 2 1 0 −1 −2 −3 0 1 Figure 6- 14 2 3 4 5 t 6 7 8 9 10 The decaying oscillator described by 2x″ + x′ + 8x = 0 For the momentum, we find: >> s.p ans = 68 /9*cos(3/4*7^(1/2)*t)*exp(–1/4*t)–748 /63 *7^(1/2)*exp(– 1/4*t)*sin(3/4*7^(1/2)*t) +68 /9*exp(–t) Now let’s plot the position as a function... it has a similar functional form In both cases we see an exponentially decaying oscillator MATLAB Demystified 1 86 x10−5 2 1 y(t) 0 −1 −2 −3 −4 0 0.01 0.02 0.03 0.04 0.05 0. 06 0.07 0.08 0.09 t Figure 7-3 0.1 Numerical solution for tc = 0.01 and w = 62 8 x10−3 4 3 2 1 0 −1 −2 −3 −4 0 0.01 0.02 0.03 0.04 0.05 t 0. 06 0.07 0.08 0.09 0.1 Figure 7-4 A comparison of the forcing function to the response In this... 1 + + x = 0, = − p− x 2 dt 2 4 dt dt 4 x ( 0 ) = 4, p( 0 ) = 0 SOLUTION 6- 13 We enter the equations and solve: >> s = dsolve('D2x + Dx + (1/4)*x = 0','Dp + (1/2)*p + (1/4)*x = 0','x(0) = 4','Dx(0)=0','p(0)=0'); 175 MATLAB Demystified 1 76 Phase portrait for mass-spring 20 15 10 5 p 0 −5 −10 −15 −20 −25 −3 −2 −1 0 1 2 3 4 x Figure 6- 17 A numerically generated phase portrait The results are: >> s.x ans... over each axis to a desired value: >> axis([–4 4 0 2.5]) >> hold off Don’t forget to call hold off so that MATLAB will stop sending data to the same figure The result of all this is show in Figure 6- 11 CHAPTER 6 Symbolic Calculus⁄Differential Eqs 169 1/(t2 + 1/2) 2.5 2 1.5 1 0.5 0 −4 Figure 6- 11 −3 −2 −1 0 t dy 1 2 3 4 = −2 t y with initial conditions given by A plot of the solution to dt y(0) = 0.2, . shown in Figure 6- 5. Figure 6- 5 A plot of g(x) showing the minimum we found in Example 6- 5 0 0.2 0.4 0 .6 0.8 1 1.2 1.4 1 .6 1.8 2 0 0.5 1 1.5 2 2.5 3 x 3x 2 − 6x + 3 CHAPTER 6 Symbolic Calculus⁄Differential. 6- 6. Figure 6- 6 A plot of f (x) = x 4 – 2x 3 identifying the local minimum found in Example 6- 6 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 2.5 3 0 5 10 15 20 25 30 x x 4 − 2x 3 Local minimum CHAPTER 6. Figure 6- 10. Figure 6- 10 Plot generated using for loop to place multiple curves on a single figure −1 −0.8 −0 .6 −0.4 −0.2 0 0.2 0.4 0 .6 0.8 1 0 20 40 60 80 100 120 t IVP Solutions 168 MATLAB

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