MA 282-Assignment #1: First Order Differential Equations -Part-1 The standard form for a first-order differential equation is ππ¦ ππ‘ = π(π‘, π¦) . A solution to this equation is a differentiable function π¦(π‘)of the independent variable π‘ that satisfies π¦ ′ (π‘) = π(π‘, π¦(π‘)). Analytic Techniques MATLAB can solve most of the differential equations that can be solved with the standard techniques encountered in this course. MATLAB's differential equations solver dsolve provides symbolic solutions to first-order differential equations although they not always explicit solutions. Example Find the general solution to the differential equation ππ¦ ππ‘ = π‘ 3 + π¦. Notice that MATLAB produces the answer with an arbitrary constant πΆπ ; in this example with a constant πΆ2 . To solve the initial value problem ππ¦ ππ‘ = π‘ 3 + π¦, π¦(0) = 7: The solution S is a symbolic expression that is, you cannot evaluate S at π‘0 by typing π(π‘0 ); use the subs command to display the values of S at certain times π‘. For instance the value of S at π‘ = 0.5 is If you wish to evaluate S at several values of π‘, then use vector notation. As an example we evaluate S at π‘ = 0.5, 0.6, 0.7: To plot the solution S on the interval [0, 1] In practice, the goal is to study a family of solutions at various initial conditions. One way to do this is to solve the IVP: ππ¦ ππ‘ = π‘ 3 + π¦, π¦(0) = π If we wish to plot the solutions curves with initial values π¦(0) = 7, 7.5, 8, 8.5 on the interval [0, 1]: Exercise MA282-1 (B. Hunt) (Solution at the end of document) Consider the initial value problem: π‘π¦ ′ + 3π¦ = 5π‘ 2 , π¦(2) = 5 a. Solve using the MATLAB function dsolve b. Plot the solution π¦(π‘) on the intervals 0.5 ≤ π‘ ≤ 5 and 0.2 ≤ π‘ ≤ 20. Determine the behavior of π¦(π‘) as π‘ approaches 0 from the right and as π‘ becomes large. c. Change the initial condition to π¦(2) = 3 and determine the behaviour of this solution, again by plotting on intervals such as 0.2 ≤ π‘ ≤ 20 and 0.5 ≤ π‘ ≤ 5. Exercise 282-2 (B. Hunt) (Solution at the end of document) a. Solve the initial value problem: π¦ ′ − 2π¦ = π ππ2π‘, π¦(0) = π b. Use MATLAB to graph solutions for π = −0.5, −0.45, … , −0.05, 0 on the interval [0, 1]. Use axis tight. c. Use MATLAB to graph solutions for π = −0.5, −0.45, … , −0.05, 0 on the interval [0, 2.5] d. What happens to the solution curves as t increases? e. What effect small changes in initial data can have on the global behavior of solutions curves? MA 282-Assignment #2: First Order Differential Equations -Part-2 Qualitative Techniques 1. Understanding the quiver command The quiver command allows us to draw vectors in the plane π 2 : For instance, if we wish to draw the two vectors a = < 2,3 > and π = < 1,4 > and place them at the points (0, 0) and (0, 1) in π 2 , then we form the following four matrices: 1. 2. 3. 4. The x-positions matrix: [0,0] The y-positions matrix: [0,1] The x-components matrix: [2,1] The y-components matrix: [3,4] If we type "quiver([0 0],[0 1],[2 1],[3 4])" in the command window, we see Note that MATLAB automatically scales vectors so that they do not overlap. To prevent MATLAB from scaling vectors we add a zero to the script: We can use the command axis to request that MATLAB draw the vectors and display them on a specific window. In this example we draw the vectors A and B and display them on the window 0 ≤ π₯ ≤ 10, 0 ≤ π¦ ≤ 10: To display a grid, simply type "grid on": We can use the command "linewidth" to change the width of the vectors; in this example we multiply the original length by 1.5: Important Remark To draw a single vector π =< π1 , π2 > and place it at the point π₯ = (π₯1 , π₯2 ), type ππ’ππ£ππ([π₯1 , π₯1 ], [π₯2 , π₯2 ], [π1 , π1 ], [π2 , π2 ]); that is we draw the vector π =< π1 , π2 > twice. 2. Slope Fields ππ¦ Consider the equation ππ‘ = π¦ − π‘. If π¦(π‘) is a solution of the equation, then π(π‘, π¦) = π¦ − π‘ represents the slope of the tangent line to the graph of π¦(π‘) at the point (π‘, π¦). The following table gives the slopes of the tangent lines at three different points. (π‘, π¦) π(π‘, π¦) (-1, 1) 2 (-1, 0) 1 (1, 4) 3 Think of the vector < π, π > as the position vector of the point (π, π) that is, the vector whose tail is the origin and tip the point (π, π). In other words, < π, π > is the vector that joins the points (0, 0) and (π, π). The vectors < 1, 2 >, < 1, 1 >, and < 1, 3 > are parallel to the tangent lines to the graph of π(π‘, π¦) at the points (−1, 1), (-1, 0), and (1, 4). We can use the quiver command to plot the vectors < 1, 2 >, < 1, 1 >, and < 1, 3 >: We observe that the vectors have the desired slopes but have different lengths at different points. We can scale the vectors by dividing each one of them by its length πΏπ : The slope field of our differential equation can be obtained by repeating the same procedure for a large number of points. To do this we use the MATLAB commend meshgrid. In this example we plot the slope field of the above equation on the domain −4 ≤ π‘ ≤ 4, −3 ≤ π¦ ≤ 3. We can obtain a better picture by reducing the length of the vectors by half: Using the dsolve command, we see that the general solution of our equation consists of the family of functions π¦(π‘) = π‘ + 1 + ππ π‘ . The following figure shows the solutions for π = −2, −1, 0, 1, 2 superimposed on the slope field. Example Plot the slop field: π¦ ′ = −π‘π¦ 2 . A special attention should be directed to the second line and the way S was written. Make sure that use ".*" and ".^" Exercise MA282-3 (B. Hunt) (Solution at the end of document) Consider the critical threshold model for population growth π¦ ′ = −(2 − π¦)π¦. a. Find the equilibrium solutions of the differential equation. b. Draw the slop field and use it to decide which equilibrium solutions are stable and which are unstable (π¦(π‘) is stable if solutions that start near π¦(π‘) converge to π¦(π‘)). Hint: modify [T,Y]=meshgrid(-4:0.2:4, -4:0.2:4)until you obtain a desirable slope field. c. What is the limiting behavior of the solution if the initial population is between 0 and 2? Greater than 2? d. Use dsolve to find the solutions with initial values 1.5, 0.3, and 2.1. e. Plot the three solutions of part (d) together with the slop field on the same graph. Do the solutions follow the slope field as you expect it to? Exercise MA282-4 (Solution at the end of document) Consider π¦ ′ = (πΌ − 1)π¦ − π¦ 3 . a. Use solve to find the roots of (πΌ − 1)π¦ − π¦ 3. Explain why π¦ = 0 is the only real root when πΌ ≤ 1, and why there are three distinct roots when πΌ > 1. b. For πΌ = −2, −1, πππ 2, draw a direction field for the differential equation and deduce there is only one equilibrium solution. Is it stable? c. Do the same for πΌ = 1. d. For πΌ = 1.5, 2, draw the direction field . Identify the equilibrium solutions and explain their stability. e. Explain: "As πΌ increase through 1, the stable solution π₯ = 0 bifurcates into two stable solutions" MA 282-Assignment #3: Numerical Solutions-the MATLAB Command ode45 So far we have been using examples where the command dsolve was capable of producing explicit solutions of the differential equations. Now consider the IVP: π¦ ′ = π¦ − π¦ 2 − 0.2 sin(π‘) , π¦(0) = 2. If we type: In this situation we use the numerical solver ode45 to find and plot approximate solutions for the equation. First we define an anonymous function (we could use an inline function): We will use the notation yap instead of y just to emphasis that the solution is an approximation one: We solved the IVP over the interval [0, 15] with initial condition π¦(0) = 2. Notice the way the second and third arguments were used with the command ode45. In particular, 0 must be one of end points of the interval over which we wish to obtain a solution. If we wish to plot the same solution solution on the interval [-2 , 15], then we plot forwards i.e. over [0, 15] and backwards i.e. over [0, -2]: Now suppose we want to plot a family of solutions with initial values π¦(0) = 0.1, 0.3, 0.5, 1, 2 over the interval [0, 15], then: Similarly, we can plot the entire family on the interval [-2, 15] by solving forwards and backwards: Remark The choice of axis is very important in producing a complete graph. Example Consider the initial value problem π¦ ′ = π¦ − π¦ 2 − 0.2 sin(π‘) , π¦(7) = 0.5. Use ode45 to find an approximate solution over the interval [0, 15]. Plot the approximate solution together with an appropriate slope field. Exercise MA282-5 (Solution at the end of document) π‘ Consider the differential equation π¦ ′ = π¦ . a. Use ode45 to calculate and plot an approximate solution to the initial value problem: π¦ ′ = π‘ π¦ , π¦(0) = 1 for 0 ≤ π‘ ≤ 2 b. Use dsolve to find an exact solution of the IVP c. Plot the exact and the approximate solution on the same graph. Can you distinguish between the two curves? d. Plot a family of solutions with initial values π¦(0) = 1.2, 1.4, 1.6, 1.8 over the interval [-1, 2] MA 282-Assignment #4: First-Order Systems Example1 The predator-prey model ππ = 2π − 1.2π πΉ ππ‘ ππΉ = −πΉ + 0.9 π πΉ ππ‘ predicts cyclic population behavior for both the predator population described by F(t) and the prey population described by R(t). See Blanchard for more details. Equilibrium Solutions 2π − 1.2π πΉ = 0 The solutions of the system { are pairs of constant functions called equilibrium solutions −πΉ + 0.9π πΉ = 0 to the system. We can use the solve command to find the equilibrium points of the above system by typing: 10 5 That is the equilibrium solutions are (π , πΉ) = (0, 0) and (π , πΉ) = ( 9 , 3). R(t)- and F(t)-graphs If we specify the initial conditions π 0 = 1 and πΉ0 = 0.5 we can use the MATLAB function ode45 to obtain approximate solutions to the system. Let π = (π (π‘), πΉ(π‘)) ; we think of π as a vector whose components are π (π‘) and F(t). For MATLAB, π (π‘) is π(1) and πΉ(π‘) is π(2). With this in mind, we begin by defining an anonymous function f whose components are the right hand side of the system: Let us denote by πππ the approximate solution to the IVP; that is πππ = (π (π‘), πΉ(π‘)) is an approximation to the actual solution of the system with the indicated initial conditions. If we wish to plot the first component of πππ i.e. π (π‘) over the interval 0 ≤ π‘ ≤ 15 then: Similarly we can plot the component πΉ(π‘) for 0 ≤ π‘ ≤ 15: We can plot the two solutions on the same graph as follows: Phase Portrait The plot of a solution (πΉ(π‘), π (π‘)) as a function of π‘ would be a curve in the (π‘, πΉ, π ) − π ππππ. The projection of this curve into the (πΉ, π ) − πππππ is called a trajectory or a solution curve. A trajectory is an example of a parameterized curve and is drawn by plotting (πΉ(π‘), π (π‘)) as π‘ varies. A plot of a family of trajectories is called a phase portrait of the system. The solution curve for the predator-prey system ππ = 2π − 1.2π πΉ ππ‘ ππΉ = −πΉ + 0.9 π πΉ ππ‘ corresponding to the initial condition (π 0 , πΉ0 ) = (1, 0.5) is Making a phase portrait requires some planning. The best approach is to start by plotting the equilibrium solutions: Next use a loop to add extra curves: Remark The above phase portrait corresponds to the curves where (π (0), πΉ(0)) = (π, π) for several values of π and π. Moreover we solved forwards from π‘ = 0 to π‘ = 6. If we change the initial conditions to (π (2), πΉ(2)) = (π, π) then it is necessary that we plot forwards and backwards otherwise we do not obtain complete curves. Exercise MA282-6 (Solution at the end of document) ππ Consider the predator-prey system { ππ‘ ππΉ ππ‘ π = 2 (1 − 3 ) π − π πΉ . (See Blanchard section 2.1) = −2πΉ + 4π πΉ a. Use solve to find all the equilibrium solutions b. Plot the solution curve corresponding to the initial conditions π (1) = 1, πππ πΉ(1) = 3.7 over the interval 0 ≤ π‘ ≤ 30. For axis, use [0, 2, 0, 6]. c. Describe the fate of the prey and predator populations based on the solution curve. d. Display graphs of the solutions π (π‘) and πΉ(π‘) corresponding to the initial conditions π (1) = 1, πππ πΉ(1) = 3.7 over the interval 0 ≤ π‘ ≤ 30. Compare with part (c). MA 282-Assignment #5: Vector Fields Once again consider the predator-prey system ππ = 2π − 1.2π πΉ ππ‘ ππΉ = −πΉ + 0.9 π πΉ ππ‘ If we let π(π‘) = (π (π‘), πΉ(π‘)), then the system can be written in the form ππ ππ‘ = (2π − 1.2π πΉ, −πΉ + 0.9π πΉ). For each point (π , πΉ) in the RF-plane we assign a vector whose components are given by (2π − 1.2π πΉ, −πΉ + 0.9π πΉ). The assignment: (π , πΉ) βΆ (2π − 1.2π πΉ, −πΉ + 0.9π πΉ) is an example of a vector field. We visualize this vector field as a set of arrows based at points in the RF-plane. As an example, let us draw some selected vectors: The lengths of vectors are important however to avoid overlapping, we often scale the vectors so they all have the same length; the result is called a direction field. The following few commands produce a vector field of the predator-prey system: We can get a better picture by selecting smaller vectors; this can be done by scaling the vectors by a factor of 0.5: Now we plot the direction field together with the solution curve corresponding to the initial condition (π (0), πΉ(0)) = (1, 0.5). Exercise MA282-7 (Blanchard) (Solution at the end of document) π2 π₯ ππ₯ a. Convert the second-order differential equation ππ‘ 2 + 2 ππ‘ − 3π₯ + π₯ 3 = 0 into a first-order system in terms of π₯ and π£, where π£ = ππ₯ ππ‘ b. Find all equilibrium points c. Use MATLAB to sketch the associated direction field. Hint: use meshgrid(-5:.4:5,5:.4:5)and axis tight d. The following script plots several solution curves over the interval −3 ≤ π‘ ≤ 3 f=@(t,P) [P(2);3*P(1)-P(1)^3-2*P(2)]; a=-2:0.2:2; b=-2:0.2:2; for k=1:length(a) [t,Pap]=ode45(f, [0,3],[a(k),b(k)]); plot(Pap(:,1),Pap(:,2),'linewidth',2) hold on [t,Pap]=ode45(f, [0,-3],[a(k),b(k)]); plot(Pap(:,1),Pap(:,2),'linewidth',2) end hold off axis([-5,5,-5,5]) What are the initial conditions? e. Use the above script to plot a phase portrait together with the direction field of part c. f. Describe the behavior of the solutions? MA 282-Assignment #6: Linear Systems We restrict our discussion to systems of the form ππ₯ = ππ₯ + ππ¦ ππ‘ ππ¦ = ππ₯ + ππ¦ ππ‘ where π, π, π, π are constants. We let = ( π π ππ π₯(π‘) π ) , π(π‘) = ( ), and we write the system in the form = π΄π. π¦(π‘) ππ‘ π 1. Strait -Line Solutions ππ Recall, if the matrix π΄ has a real eigenvalue π with associated eigenvector π£, then the linear system ππ‘ = π΄π has the straight line solution π(π‘) = π ππ‘ π£. Example ππ Consider the system ππ‘ = ( 2 2 ) π. 1 3 We use the MATLAB command eig to calculate the eigenvalues and their associated eigenvectors as follows. The eigenpairs of the system are: π1 = 1, π£1 = [ −2 1 ] and π2 = 4, π£2 = [ ]. 1 1 So the system has two straight- line solutions namely, π1 (π‘) = π π‘ [ π‘ 4π‘ −2 ] = [−2π ] and π2 (π‘) = [π 4π‘ ]. π‘ 1 π π We can plot the direction field of the system exactly as we did for non-linear systems: To plot half of the straight line solution, we type Any multiple of the vector π£1 is also an eigeinvector of π1 . In particular – π£1 is an eigenvector and the lower half of the line is obtained by plotting the solution – π π‘ π£1 . Similarly we can plot the second straight solution as folows. Please consult the textbook for detailed analysis of the importance of straight line solutions. Next we use MATLAB to solve linear systems in two different ways. We will focus on the technical parts; students should consult their textbooks to learn about the valuable information that eigenpairs pairs contain. 2. Solving Systems in Terms of Eigenpairs Case I: two distinct real eigenvalues Example As an example we solve the system ππ ππ‘ −3 1 2 =( ) π, π(0) = [ ]. 0 2 5 −3 1 We enter the coefficient matrix π΄ = ( ) in MATLAB: 0 2 Next we use the command eig to calculate the eigenpairs of π΄: 1 1/5 ) provides the eigenvectors associated to the distinct 0 1 eigenvalues π1 = −3 and π2 = 2 found along the diagonal of the matrix Evl. Note that the columns of the matrix πΈπ£π = ( Therefore the general solution is 1 1/5 π(π‘) = π1 π −3π‘ [ ] + π2 π 2π‘ [ ]. 0 1 2 Substituting for the initial condition π(0) = [ ]: 5 1 1/5 2 π1 [ ] + π2 [ ]=[ ] 0 5 1 which can be written in the form: 1 1/5 π1 2 ( ) [π ] = [ ] 5 2 0 1 1 1/5 Observe that πΈπ£π = ( ), hence we obtain: 0 1 π1 2 πΈπ£π [π ] = [ ]. 5 2 Now we use MATLAB to solve for π1 and π2 as follows: that is π1=1, π2 = 5, and the solution to our initial value problem is 1 1/5 π(π‘) = π −3π‘ [ ] + 5π 2π‘ [ ] 0 1 or 1 1 π(π‘) = [ ] π −3π‘ + [ ] π 2π‘ 0 5 Case II: two complex eigenvalues As an example we find the general solution of the system So the eigenpairs are ππ ππ‘ =( −2 −3 ) π. 3 −2 −π π1 = −2 − 3π, π£1 = [ ] 1 and π π2 = −2 + 3π, π£2 = [ ]. 1 Consider the second pair and let us calculate the real and imaginary parts of the complex solution: π π(π‘) = [ ] π (−2+3π)π‘ 1 π ππ −2π‘ πππ 3π‘ − π −2π‘ π ππ3π‘ −π −2π‘ π ππ3π‘ π −2π‘ πππ 3π‘ π(π‘) = [ ] π −2π‘ (πππ 3π‘ + ππ ππ3π‘) = [ ]=[ ]+[ ]π −2π‘ −2π‘ −2π‘ −2π‘ 1 π πππ 3π‘ + ππ π ππ3π‘ π πππ 3π‘ π π ππ3π‘ Consequently the general solution of this system is −π −2π‘ π ππ3π‘ π(π‘) = πΆ1 [ π −2π‘ πππ 3π‘ ] + πΆ2 [ π −2π‘ πππ 3π‘ ] π −2π‘ π ππ3π‘ Case III: The eigenvalue π has multiplicity 2 i. ππ 3 0 )π 0 3 There are two independent eigenvectors associated with π : ππ‘ = ( 1 0 In this case the general solution is π(π‘) = π1 π 3π‘ [ ] + π2 π 3π‘ [ ] 0 1 ii. ππ 3 1 There is only one linearly independent vector associated with π: ππ‘ = ( 0 )π 3 0 So there is one vector, π£ = [ ], associated with π = 3. 1 0 A particular solution is given by π1 = π π π‘ π£ = π 3π‘ [ ]. 1 A second solution is π2 = π‘π π π‘ π£ + π€π ππ‘ , where (π΄ − ππΌ)π€ = π£. Note that Evr is just the vector π£ hence we can easily solve for π€: 1 0 Therefore, π2 = π‘π π π‘ π£ + π€π ππ‘ = π‘π 3 π‘ [ ] + [ ] π 3π‘ and the general solution is π(π‘) = πΆ1 π1 (π‘) + πΆ2 π2 (π‘). 0 1 3. Solving Systems using the command dsolve Let us consider the system discussed in Case III (ii): ππ ππ‘ That is the system ππ₯ = 3π₯ ππ‘ ππ¦ = π₯ + 3π¦ ππ‘ To find the general solution, we type 3 1 =( 0 ) π. 3 To solve the same system but with the initial conditions π₯(0) = 2, π¦(0) = 5: Exercise MA282-8 (Solution at the end of document) Let π be a real number different from 0 and consider the system ππ₯ = ππ₯ + 4π¦ ππ‘ ππ¦ = ππ¦ ππ‘ 1. 2. 3. 4. Use MATLAB to verify that the system has a double eigenvalue π Use MATLAB to verify that there is only one eigenvector associated with π Write a script that calculates the general solution of the system Verify your script by using dsolve to solve the system MA 282-Assignment #7: Phase Portraits and Direction Fields Consider the initial value problem ππ₯ = 2π₯ + 2π¦ ππ‘ , π₯(0) = π, π¦(0) = π. ππ¦ = π₯ + 3π¦ ππ‘ We can use desolve to solve this IVP problem in terms of π and π: As we have indicated before the outputs of the desolve command are symbolic expressions. We can use MATLAB to change those symbolic expressions into functions π₯π(π‘, π, π) and π¦π(π‘, π, π). This can be done using the MATLAB command eval. Finally since we intend to plot solutions curves, we need to make sure that all expressions are vectorized ; has dots before * and /; otherwise π‘ will be interpreted as a matrix and multiplications and divisions will not make any sense. The following script should plot the straight-line solutions: Let us plot the straight-line solutions together with some solutions curves: Putting everything together by adding a direction field: Exercise MA282-9 (Blanchard Ex 19, 3.3) Consider the initial value problem ππ₯ 1 = −2π₯ + π¦ ππ‘ 2 , π₯(0) = π, π¦(0) = π. ππ¦ = −π¦ ππ‘ 1. 2. 3. 4. 5. Draw a direction field for the system. Determine the type of the equilibrium point at the origin. Use dsolve to solve the IVP in terms of m and n Find all straight-line solutions Plot the straight-line solutions together with the solutions with initial conditions (π, π) = (2, 1), (1, −2), (−2, 2), (−2, 0)