Вы находитесь на странице: 1из 9

Using Mathcad to Solve Systems of Differential Equations Charles Nippert

Getting Started Systems of differential equations are quite common in dynamic simulations. Solving a system of differential equations is somewhat different than solving a single ordinary differential equation. The solution procedure requires a little bit of advance planning. The system of differential equations must first be placed into the "standard form" shown below: y'1 = F1 (t , y1 , y 2 L y n ) y' 2 = F2 (t , y1 , y 2 L y n ) M y' n = Fn (t , y 0 , y1 L y n ) y 1 (t 0 ) = C 1 y 2 (t 0 ) = C 2 M y n (t 0 ) = C n It is generally possible to do this transformation however it may require some manipulation. Example of Transformation of a System of Differential Equations Consider the system of differential equations and initial value conditions shown below y' '1 = y1 3y 2 y 2 = y1 '+ y' 2 y1 (0) = 2 y'1 (0 ) = 1

y 2 (0 ) = 3 To generate a solution, define a new variable y3 = y1 and rearrange to obtain the following system. y'1 = y 3 y' 2 = y 2 y 3 y' 3 = y 1 3y 2 y1 (0) = 2 y 2 (0 ) = 3

y 3 (0) = 1 In the sample on these notes you will obtain a numerical solution for the following system of differential equations. You will the use the solution to make both a conventional parametric plot and a plot of the phase plane.

y'1 = y 2 y' 2 = y1 y1 (0) = 1

y 2 (0) = 2 Enter the Initial Values 1. Open Mathcad in the usual fashion and move the cursor about 1 inch below the menu slightly to the right. If the matrix toolbar is not visible open it by choosing "View/Toolbars/Matrix". The initial values are entered as a vector. You can give the vector any name you wish. In this example enter the name space "y0". Press the ":" key to generate the arithmetic assignment symbol ":=". Use the mouse cursor to press the matrix logo on the matrix toolbar, . On my toolbar it is the first button on the first row. The "Insert Matrix" dialog box immediately appears. The initial conditions are entered as a vector with one column and two rows. When you are finished the dialog box should look like figure 1. Figure 1 Insert Matrix Dialog Box

2.

Press the "OK" button. The dialog box should immediately close and a column vector with two elements should immediately appear. The blue inverted L. cursor should be on the upper black rectangle or placeholder in the matrix. Type "1" to enter the first boundary condition. You can use the arrow keys on the keyboard to move between the elements on a matrix. Press the down arrow key to move to the next element and enter its value, "2". The finished vector should look like figure 2. Figure 2 Initial Condition Vector

3.

Press "Enter". The system of differential equations is entered as a vector function. Because this vector function is somewhat large, you may wish to move

the cursor down to allow enough space for it. Mathcad uses a standard function format similar to the kind in used in many programming languages. First the function name is entered followed by parentheses that enclose the "argument list" which is a list of variables the function needs to perform its operations. The argument list for the vector function containing the derivatives consists of the independent variable (the parameter in the dominator of the derivative), and a vector that will contain the values of the dependent variables. You may give the vector function any name you wish. In this example name the vector function "D". Enter the left-hand side of the vector function definition by typing "D(t, y):". Create a vector with one column and two rows by pressing the matrix button. The number of rows and columns in the dialog box should be these values because the dialog box should retain the values you entered when you entered the boundary conditions in the initial conditions vector. Your screen should now look like figure 3. Figure 3 Entering The Differential Equations

4.

Recall that the differential equations are y' 0 = y1 y'1 = y 0 Now, enter the right hand side of the first differential equation, the one for y0. First press "y", this is the name of the vector that you entered in the argument list. To enter the subscript, press the subscript key, . In my computer this button was to the right of the matrix button, and was the second button on the first row. The small black rectangle will appear below in slightly to the right of the letter y. Press the "0" key data value the subscript. Use the down arrow key to move to the second placeholder and enter the second differential equation. Follow the procedure you followed in the first differential equation to enter the subscript. When you have completed entering the differential equation, it should look something like figure 4. Figure 4 The Completed System Of Differential Equations

5.

Mathcad has several functions for solving systems of ordinary differential equations. Each one uses a different integration algorithm and takes the same arguments. The three functions are shown in the table below:

Table 1 Mathcad Functions For Solving Systems Of Ordinary Differential Equations Function Name Rkfixed Rkadapt Brief Description Runge-Kutta method with a fixed step Runge-Kutta method with a variable step (a more robust method that adjusts the integral step size based on an estimation of integration error)

Bulirsh-Stoer method (a very robust method which some prefer to Runge-Kutta) All three functions take the same argument list which is shown below Table 2 Sample Reference To Mathcads Differential Equation Solvers rkfixed(y0,ti,tf,M,D) Rkadap(y0,ti,tf,M,D) Bulstoer(y0,ti,tf,M,D) Variable y0 ti tf M D Description The name of the vector containing the initial conditions The starting point of the integration The ending point of the integration The number of integration steps between the starting and ending point

Bulstoer

The name of the vector function containing the differential equations The differential equation solvers are called on the right hand side of an arithmetic assignment statement. The left-hand side defines a matrix that will contain the results of the numerical integration. In this example, you will create a matrix named "yn", use the "rkfixed" function to obtain a solution from zero to two using 20 integration steps. Press "Enter" if your screen still shows the blue inverted L. cursor in the differential equation vector. The equation you're going to enter will APPEAR on the screen as "yn := rkfixed(y0,0,2.0,20,D)". Remember to type ":" or use the evaluation toolbar to make the arithmetic's assignment symbol, ":=", appear on screen. Your completed solution should look like figure 5.

Figure 5 Completed Mathcad Solution to a System of Ordinary Differential Equations

6.

You can see the numerical results of the integration by displaying the contents of the matrix "yn". Press enter and move the red cross cursor far down and to the right of the rest of your equation. Next, type "yn=". A table showing a portion of the results will appear. Clicking on any portion of the table will cause that portion to be highlighted and will cause a scrollbar to appear to the right of the table. Use this scrollbar to make the rest of the table visible. This table is shown in figure 6. Figure 6 Table Showing Partial Results Of Numerical Integration

Clicking the mouse cursor on any portion of the table highlights that portion and makes the scroll bar visible.

Use the scroll bar to view any portion of the table.

8.

Notice that the table in figure 6 has three columns. The first column shows the range of the independent variable. The next column shows the values of y0. The final column shows the values of y1. Often this tabular data is all that you need. However it is often desirable to display the data in graphical form. It is important that you notice the shift in subscripts that Mathcad makes. As you enter the integration the first dependent in variable as the subscript zero. However, the first dependent variable has the subscript one in the output matrix. Before you make the plot, you will make 3 1-dimensional arrays that contain a date in the columns of the yn matrix. Doing this step will make your plots easier to read and create. Entering individual columns can prove difficult if you intend to draw more than one line on a plot. Select an area of your worksheet somewhere below the differential equations and click the mouse button. Type "t:yn" Type "yn" then on the matrix toolbar. This button was the click on the column range button second button in the second row of the matrix toolbar in my computer. Type "0" to specify the column number in the yn matrix. At this point the arithmetic assignment statement should look like figure 7. Figure 7 Defining a Vector From a Column In a Matrix

8.

Create two more arrays, y1 and y2 that contain the values from the first and second columns of the yn matrix. This portion of your worksheet should now look like figure eight. Figure 8 Defining the Vectors From the yn Matrix t := yn<0> y1 := yn<1> y2 := yn<2>

9.

You will now make a plot of y0 and y1 versus the independent variable, t. Move the red cross cursor to a vacant area of your worksheet. You can create a x-y plot in any of the following ways: 1. Press the "@" 2. Select "Insert/Graph/X-Y Plot" from the menu. This plot will be y1 and y2 versus t. Enter "t" as the name of the variable on the horizontal axis. Keep pressing the left arrow key until the blue inverted L. cursor surrounds the leftmost solid black rectangle that is the placeholder for the vertical axis. When plotting more than one line on a X-Y axis, you enter the names of the

arrays separated by a coma. Type the characters "y1,y2" and press "Enter". The graph should immediately appear. Double-click on the graph to call up the formatting dialog box. Your worksheet should now resemble figure 9. Figure 9 The Solution To This System of Differential Equations

10.

You can now format this graph. First, check the "Grade to Lines" box for both the x-axis and y-axis. The analytical solution of these differential equations includes exponential terms check the "Log Scale" option for the y-axis. Press "OK". The dialog box should immediately close and the graph will show grid lines and the y-axis will become a logarithmic axis. Use the mouse to grab the solid black rectangular "handle" in the lower right corner and enlarge and the graph. Now your figure should resemble figure 10.

Figure 10 Finished Plots of y1 & y2 versus t

11.

A phase plot is a plot of the dependent variables plotted against each other. The independent variable is not shown. You will not make a phase plot for this system. Click on a portion of the worksheet that is below your solution but still empty and create a new graph. This time label the horizontal axis "y1" and label the vertical axis "y2". Double-click on the graph to open the format dialog box and check "Grid Lines" for both axes. Press the "OK" button and then enlarge the graph. Your face plot should resemble figure 11.

Figure 11 Finished Phase Plot

Вам также может понравиться