0% found this document useful (0 votes)
82 views31 pages

Math2800 ODE's Introduction

This document provides an introduction to using Maple to solve ordinary differential equations (ODEs). It discusses how Maple can solve first-order ODEs both exactly and numerically, as well as first-order systems of ODEs and higher-order ODEs. It also demonstrates how Maple can solve initial value problems for first-order and higher-order ODEs. Examples are provided throughout to illustrate each concept.

Uploaded by

leopard9987
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views31 pages

Math2800 ODE's Introduction

This document provides an introduction to using Maple to solve ordinary differential equations (ODEs). It discusses how Maple can solve first-order ODEs both exactly and numerically, as well as first-order systems of ODEs and higher-order ODEs. It also demonstrates how Maple can solve initial value problems for first-order and higher-order ODEs. Examples are provided throughout to illustrate each concept.

Uploaded by

leopard9987
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Maple and ODE's - An Introduction

restart:

General Solutions - First Order ODE's


1. Maple can solve Ordinary Differential Equations (ODE's) exactly (if they are sufficiently simple) or numerically. For example, consider the first-order linear ODE y' + 2xy = x. Call the ODE eqn_1, eqn_1:=diff(y(x),x)+2*x*y(x)=x;

Whenever we type eqn_1 Maple will substitute the ODE. Notice that Maple needs to know that y is a function of x. Furthermore, the ordinary derivative is represented as a partial derivative. This because the diff command is used for both ordinary and partial derivatives. To see if Maple can solve the ODE use the command dsolve. The syntax is dsolve(ODE,dependent variable). Call the solution sol_1, sol_1:=dsolve(eqn_1,y(x));

Every time we type sol_1, Maple returns the above solution. As you can see Maple can solve this ODE exactly. The arbitrary constant of integration is denoted by _C1. sol_1;

Explicit or implicit solutions can be tested, in principle, by using the command odetest . The syntax is odetest(solution,ODE). In the present case, odetest(sol_1,eqn_1); Zero signifies that the ODE is satisfied. Suppose that we asked to find out whether y(x) = x/2 + _C2 exp(-x^2) (which will be called test_1) also solves the ODE eqn_1. test_1:=y(x)=x/2+_C2*exp(-x^2);

odetest(test_1,eqn_1);

The answer is nonzero, hence y(x) = x/2 + _C2 exp(-x^2) does not solve eqn_1. 2. Consider the following first-order ODE: y' + 2 exp(-x^2) = sin(pi x^2) eqn_2:=diff(y(x),x)+2*exp(-x^2)=sin(Pi*x^2);

Note that the mathematical constant pi is represented by Pi (the case is important).

sol_2:=dsolve(eqn_2,y(x));

Here Maple can solve the equation exactly and the solution is given in terms of two special functions erf (the error function; ) and FresnelS (the Fresnel sine integral).

General Solutions - First Order System of ODE's


3. Maple can attempt to solve first-order ODE systems. For example, consider x' = - x and y' = x - 2y, where x and y are functions of t. eqn_3:={diff(x(t),t)=-x(t),diff(y(t),t)=x(t)-2*y(t)};

Observe that the system, separated by a comma and enclosed in curly braces, is called eqn_3. There is no need to name each equation separately. Try to solve, sol_3:=dsolve(eqn_3,{x(t),y(t)}); Observe that the dependent variables are, just like the system, enclosed in curly braces. As in the case of a single first-order ODE we can check the solution, odetest(sol_3,eqn_3);

General Solutions - Higher Order ODE's


4. Maple can also attempt to solve higher order equations exactly. For example, consider y'' - y = x^3 - 2x, eqn_4:=diff(y(x),x$2)-y(x)=x^3-2*x;

Observe Maple's notation for the second derivative The 5th derivative, for example, would be represented as diff(y(x),x$5) sol_4:=dsolve(eqn_4,y(x)); As expected for a second order ODE, there are two arbitrary constants of integration.

Initial Value Problem - First Order ODE's


5. Maple can also try to solve Initial Value Problems (IVP). For example, y' + 2xy = x, y(0) = -1, eqn_5:={diff(y(x),x)+2*x*y(x)=x,y(0)=-1};

The ODE and the initial condition are separated by a comma and enclosed inside curly braces. Use the dsolve command

sol_5:=dsolve(eqn_5,y(x));

Now that we have the solution, we may want to (i) construct its graph, (ii) evaluate y at a given x, (iii) evaluate the second derivative of y at a given point, (iv) plot the second derivative, etc. In order to do all this we need to y as a function of x. The first step is to use the assign command. assign(sol_5); Although nothing seems to have happend (no output), from now on typing y(x) will return the solution, y(x);

It may now appear that y is a function of x defined by 1/2 - (3/2) exp(-x^2). Hence, to evaluate y at x = 0 we should simply type y(1). Unfortunately, this is not the case. Observe, y(0); We do not get the expected -1. In order that Maple understands that y is a function of x defined by 1/2 - (3/2) exp(-x^2), we need to do the following y:=unapply(y(x),x);

The unapply command creates a function called y which is a function of x and which is defined by the formula called y(x). For example, suppose that we want to define functions h(x) = x^2 + 1/x and g(x,y) = x^2+y^2, h:=unapply(x^2+1/x,x);

g:=unapply(x^2+y^2,x,y); If we need to know the value of g at the point (-1,5) then we simply type g(-1,5), g(-1,5); Returning to our ODE example, we have converted the solution to a function y(x) (understood as such by Maple) so we can evaluate y(x_0) for any x_0 in the domain of the solution. Let's check that the solution passes though the initial point (0,-1), y(0); What is the value of the solution at x = - sqrt(pi)? y(-sqrt(Pi));

Notice that Maple gives me the exact value. In order to get a decimal approximation, the evalf

command is used, evalf(y(-sqrt(Pi))); Maple can plot this solution using the plot command, plot(y(x),x=-5..5);

I have decided to take the range of x be -5<= x <= 5, you can make it whatever you like. Note that Maple tries to make the graph look as "good" as possible. This leads, in general, to different scales on the axes. If you wish, you can override this using the scaling option, see plot,options, inside the plot command. plot(y(x),x=-5..5,scaling=constrained);

Which graph looks better? If you want to use a graph later on you may save it under a name. For example, to save the plot of y(x) (the IVP solution) under the name p_1 type p_1:=plot(y(x),x=-5..5): Observe that I have suppressed the output. If you use ; instead, you will not see the graph but its definition in terms of coordinates. Not very informative. Suppose that I now want to define y''(x), the 2nd derivative of y(x). Since Maple will object if I try to use the symbol y'', I will use y_2 instead. y_2:=unapply(diff(y(x),x$2),x);

I can now evaluate y'(x_0) at various points x_0 and I can plot the function y''(x), y_2(-5); or evalf(y_2(-5)); Plot of y''(x), plot(y_2(x),x=-5..5);

It is possible to plot the IVP solution y(x) and its second derivative y''(x) on the same set of axes. We have already saved the plot of y(x) under the name p_1. I will save it again with identification using the option legend (see ). Save the plot of y''(x) in different colour and appropriate identification under the name p_2. p_1:=plot(y(x),x=-5..5,legend="y(x)"): p_2:=plot(y_2(x),x=-5..5,color=black,legend="y''(x)"): In order to put these on the same graph I need the command display which lives in the plots package. Firstly, load in the plots package, then use the display command, with(plots):
Warning, the name changecoords has been redefined

display(p_1,p_2);

y(x)

y''(x)

Aside - Helping Maple to Graph Functions


Which graph looks better? Note, however, that sometimes Maple does not know best and YOU have to help it. For example, plot y = tan(x), -10<= x <= 10, plot(tan(x),x=-10..10);

600 400 200

10

Clearly, the first problem is the range on the y-axis. Let's reduce it using the option y = a..b, where a and b are some reasonable numbers (you can experiment). plot(tan(x),x=-10..10,y=-5..5);

y
2

10

This is better, but we still have the vertical lines which clearly should not be there. We can get rid of them by warning Maple that tan(x) is a discontinuous function using the discont option.

Maple will try to adjust the graph, plot(tan(x),x=-10..10,y=-5..5,discont=true);

y
2

10

Now the graph looks as expected.

Initial Value Problem - Special Functions


6. Example: Use Maple to find the exact solution of the IVP y' + 2 exp(-x^2) = sin(pi x^2), y(0) = 1. Hence evaluate y(-2) and y'(02); plot, on the same set of axes, both y(x) and y'(x) for -3<= x <= 2. NOTE: Since I want to use y(x) which was defined above as 1/2 - (3/2) exp(-x^2) I must reset before proceeding (otherwise an error will result)! Observe, y(x);

y:='y': y(x); So y(x) has been reset to an unknown variable. Now solve the new IVP, eqn_2:={diff(y(x),x)+2*exp(-x^2)=sin(Pi*x^2),y(0)=1};

sol_2:=dsolve(eqn_2,y(x));

Redefine the solution so that Maple understands it to be a function of x, assign(sol_2); y:=unapply(y(x),x);

Let y_3(x) denote the 3rd-derivative of y(x), then y_1:=unapply(diff(y(x),x),x);

Thus, exactly, y(-2);y_1(-2);

or approximately, evalf(y(-2));evalf(y_1(-2));

Plotting y(x) and y'''(x) on the same set of axes. (No need to load plots package. It has already been loaded above.) p_3:=plot(y(x),x=-3..2,legend="y(x)"): p_4:=plot(y_1(x),x=-3..2,color=black,legend="y'(x)"): display(p_3,p_4);

y(x)

y'(x)

Initial Value Problem - Higher Order ODE's


7. Let us try to solve the second-order IVP y'' - y = x^3 - 2x, y'(0) = 5, y(0) = 2. Since we want to use y(x) as the dependent variable we must reset it. y:='y': Define the ODE and solve it, eqn_6:={diff(y(x),x$2)-y(x)=x^3-2*x,(D@@1)(y)(0)=5,y(0)=2};

Note that initial condition y'(0) = 5 is passed to Maple as D(y)(0) = 5. The initial condition y'''(a) = b would translate to (D@@3)(y)(a) = b (see D ). sol_6:=dsolve(eqn_6,y(x));

Convert to the function y(x), assign(sol_6): y:=unapply(y(x),x);

Define y'(x) as afunction, y_1:=unapply(diff(y(x),x),x);

Check that the solution y(x) and its first derivative y'(x) yield the correct initial conditions, y(0);y_1(0);

Plot y(x) and y'(x) on separate sets of axes, plot(y(x),x=-5..5,y=-100..100);

100

50

plot(y_1(x),x=-5..5,y=-100..100);

100

50

Plot y(x) and y'(x) on the set of axes, p_5:=plot(y(x),x=-5..5,y=-100..100,legend="y(x)"): p_6:=plot(y_1(x),x=-5..5,y=-100..100,color=blue,legend="y' (x)"): display(p_5,p_6);

100

50

y(x)

y'(x)

Second Order IVP - Numerical Solution


8. Of course, if an ODE is complicated enough Maple will not be able to solve it exactly. For example, try exp(y)y'' + sin(x^3y') = 0, y:='y': dsolve(exp(y(x))*diff(y(x),x$2)+sin(x^3*diff(y(x),x))=0,y (x)); Maple cannot find the general solution and there is nothing we can do bout it. Suppose, however, that we are interested in the IVP consisting of the above ODE together with the conditions y(0)= 1, y'(0)=2. eqn_7:={exp(y(x))*diff(y(x),x$2)+sin(x^3*diff(y(x),x))=0,y (0)=1,(D@@1)(y)(0)=2};

dsolve(eqn_7,y(x)); Still no exact solution. However, Maple can attempt to find an approximate (numerical) solution. We need to supply the option numeric to the dsolve command, sol_7:=dsolve(eqn_7,numeric); The result is a Maple procedure. What does this mean? sol_7(0);

sol_7(1);

The procedure sol_7 evaluated at a given value x_0 is a list from which we can extract the numbers y(x_0) and y'(x_0). For example, suppose I need to know the value of the solution and its derivative at x = 3.5. Denote y(3.5) by y3_5 and y'(3.5) by y_1_3_5, then sol_7(3.5);

sol_7(3.5)[2]; rhs(sol_7(3.5)[2]); Combining into one step, y3_5:=rhs(sol_7(3.5)[2]); y_1_3_5:=rhs(sol_7(3.5)[3]); Thus the solution curve passes through the point (3.5,7.91), correct to two decimal places, and its slope at that point is 1.97, correct to two decimal places. We can use the odeplot command to graph this numerical solution. The odeplot command lives in the plots package which we have already loaded. odeplot(sol_7);

20

15

y 10

10

x
We can look at various part of this solution curve using the view option, odeplot(sol_7,view=[-2..1,-2..3]);

y
1

First Order IVP - Numerical Solutions

9. Example: Consider the initial value problem y'(t) + 2 t y(t)^2 = t exp(y(t)), y(0) = -1. Firstly, try to solve exactly, eqn_8:={diff(y(t),t)+2*t*y(t)^2=t*exp(y(t)),y(0)=-1};

dsolve(eqn_8,y(t));

Maple is unable to find the exact solution. Let's try numerical integration: sol_8:=dsolve(eqn_8,numeric); Since the ODE is first order, sol_8 contains a table of t and (the corresponding) y values. For example, sol_8(0); sol_8(1); sol_8(2);
Error, (in sol_8) cannot evaluate the solution further right of 1.0238896, probably a singularity

Maple cannot find the value of y when t = 2. It warns that it cannot integrate to the right of t = 1.0238896 and suggests a singularity (perhaps the slope of the solution curve approaches infinity) as a possible reason. Let's try to plot the numerical solution sol_8: odeplot(sol_8);
Warning, cannot compute solution further left of -1.02389133424902656 Warning, cannot compute solution further right of 1.02388960782259697

Maple cannot extend the solution outside the interval (-1.02389133424902656, 1.02388960782259697) because it cannot integrate outside this integral. It is also clear that we need to restrict the y axis to get a reasonable graph, odeplot(sol_8,view=[-1.2..1.2,-10..1]);
Warning, cannot compute solution further left of -1.02389133424902656 Warning, cannot compute solution further right of 1.02388960782259697

The inability of Maple to integrate past suggests that the solution curve becomes very steep as t approaches -1.02 (from above) and 1.02 (from below). This is borne out by the graph. OK, so we can plot the numerical solution using odeplot. Is it also possible to plot the derivative y'(x). In this case we do not have a formula for y(x) which we could differentiate. However, it is still possible to plot y'(x). Firstly, we need to redo the numerical integration with the option output=listprocedure. sol_8:=dsolve(eqn_8,numeric,output=listprocedure); sol_8(0); y:=unapply(rhs(sol_8(t)[2]),t); Although the output is not very informative, I have just defined (using the table of values sol_8 (t)), y as a function of t. To convince you of this let me plot the solution curve using the plot command instead of the dsolve command. You can compare the two graphs. plot(y(t),t=-1.2..1.2,y=-10..1,labels=["t","y"]);

Now, let ne try to costruct an approximation to the derivative function. The ODE in normal form is y' = t exp(y(t)) - 2 t y(t)^2. Denoting y'(t) by y_1(t), y_1:=unapply(t*exp(y(t))-2*t*y(t)^2,t); We can now plot the first derivative of the solution. plot(y_1(t),t=-1.2..1.2,y=-10..10,labels=["t","y'"]);

10

y'

In order to plot the second derivative of the solution we need to differentiate t exp(y(t)) - 2 t y(t) ^2 with respect to t. Of course, I cannot use y(t) so I will use z(t) instead, diff(t*exp(z(t))-2*t*z(t)^2,t);

This suggests that the second derivative y''(x),denoted here by y_2(x), of the solution y(x) is of the form, y_2:=unapply(exp(y(t))+t*exp(y(t))*y_1(t)-2*y(t)^2-4*t*y(t) *y_1(t),t);

plot(y_2(t),t=-1.2..1.2,y=-10..10,labels=["t","y''"]);

10

y''

We can plot y(t), y'(t) and y''(t) on the same set of axes, p_7:=plot(y(t),t=-1.2..1.2,y=-10..10,labels=["t","y"], legend="y(t)"): p_8:=plot(y_1(t),t=-1.2..1.2,y=-10..10,labels=["t","y"], color=blue,legend="y'(t)"): p_9:=plot(y_2(t),t=-1.2..1.2,y=-10..10,labels=["t","y"], color=black,legend="y''(t)"): display(p_7,p_8,p_9);

10

y(t)

y'(t)

y''(t)

Solution Curves, Integral Curves and Phase Portraits


10. Example: Suppose we want to solve the IVP y'=(1+3x^2)/(3y^2-6y), y(0)=1, y:='y': eqn_9:={diff(y(x),x)=(1+3*x^2)/(3*y(x)^2-6*y(x)),y(0)=1};

sol_9:=dsolve(eqn_9,y(x)): The exact solution is too complicated (just change : to ; and see for your self). Try a numeric solution sol_9:=dsolve(eqn_9,numeric); Let's plot the solution, odeplot(sol_9);
Warning, cannot compute solution further left of -1.00000490249797025 Warning, cannot compute solution further right of 1.00000079762606009

x
Here the slope of the solution curve approaches infinity as t approaches -1 (from above) and 1 (from below). Maple is unable to integrate past these points. As we shall see below the graph "turns around" at these points. Note that the solution of an IVP is a function. Hence the solution curve cannot be cut by a vertical line more than once. Thus, in this example, the solution curve cannot extend past -1 and 1. However, this solution curve can be extended and the resulting curve is called an integral curve. An integral curve is simply a union of one or more solution curves. Let's plot the integral curve for the above IVP. We use DEplot command which is part of the DEtools package. Firstly, we need to rewrite the given ODE as a system of two ODEs which are such that infinite derivatives cannot arise. Introduce a new parameter t and write y'(t) =1+3x (t)^2 (this is just the numerator of the original ODE), x'(t) = 3y(t)^2-6y(t) (this is just the denominator of the original ODE). Note that y'(x) = y'(t) / x'(t). Secondly, the initial value y =1 when x = 0 can be rewritten as y = 1 and x =0 when t = 0, i.e., x(0) = 0 and y(0) = 1. Loading DEtools and substituting the ODEs, dependent variables, the range of t ([0,1] is a reasonable starting point) and the initial values into the DEplot we get: with(DEtools): eqn_10:=[diff(y(t),t)=1+3*x(t)^2,diff(x(t),t)=3*y(t)^2-6*y (t)];

IC_10:=[[x(0)=0,y(0)=1]]; For DEplot the equations (if there is more than one) must be enclosed in square braces and the initial conditions are written separately (also enclosed in square braces). DEplot(eqn_10,[x(t),y(t)],t=0..1,IC_10);

y
2

1 0 1 2 3

x
By default DEplot shows the direction arrows (these are tangential to the integral curves and point in the direction of increasing t). Normaly, we want to suppress these arrows using the arrows=none option, DEplot(eqn_10,[x(t),y(t)],t=0..1,IC_10,arrows=none);

y
2

1 0 1 2 3

x
By default DEplot plots the integral curve in yellow colour at thickness=3. Normaly, we want to

change the colour to a darker one (for printing) and reduce the thickness to 0, DEplot(eqn_10,[x(t),y(t)],t=0..1,IC_10,arrows=none, linecolor=black,thickness=0);

y
2

1 0 1 2 3

x
We can extend the integral curve by increasing the range of t. Negative values of t are allowed. Their presence merely indicates integration in the direction of decreasing t. We can specify which part of the plane we wnt to look at by specifying ranges for x and y, DEplot(eqn_10,[x(t),y(t)],t=-1..1,IC_10,x=-1.5..4,y=-5..5, arrows=none,linecolor=black,thickness=0);

y
2

Observe that the integral curve is not smooth. We need to decrease the integration step size. DEplot(eqn_10,[x(t),y(t)],t=-1..1,IC_10,x=-1.5..4,y=-5..5, arrows=none,linecolor=black,thickness=0,stepsize=0.01);

y
2

I have said above that a solution curve is just a subset of the corresponding integral curve. Let's us check this for the present IVP. To do this we just plot both curves on the same set of axes. Let

p_10 be the graph of the integral curve and p_11 be the graph of the solution curve (I have made the colour of the solution curve red and increased its thickness to 3 for better visualization, p_10:=DEplot(eqn_10,[x(t),y(t)],t=-1..1,IC_10,x=-1.5..4,y= -5..5,arrows=none,linecolor=black,thickness=0,stepsize= 0.01): p_11:=odeplot(sol_9,color=red,thickness=3):
Warning, cannot compute solution further left of -1.00000835155014123 Warning, cannot compute solution further right of 1.00000079762606009

display([p_10,p_11]);

y
2

It is clear that the solution curve through (0,1) is a subset of the integral curve through (0,1). Furthermore, we see that this integral curve is a union of three solution curves: one defined on y>2, one defined on 2 < y < 0 (pictured) and one defined on y < 0. Finaly, observe that it is possible to plot several integral curves on the same diagram by adding initial values. The following graph shows integral curves through the points (t,y) = (0.1), (2,2) and (0,4), IC_11:=[[x(0)=0,y(0)=1],[x(0)=2,y(0)=2],[x(0)=0,y(0)=4]]; DEplot(eqn_10,[x(t),y(t)],t=-1..1,IC_11,x=-4..4,y=-5..5, arrows=none,linecolor=black,thickness=0,stepsize=0.01);

y
2

A diagram of a set of integral curves of a given ODE (or a system of ODE's) is called a phase space (or phase portrait) of that ODE (system of ODE's).

Direction Fields - First Order Systems


As you may have noticed in the previous section DEplot can be used to plot the direction field of first order ODE. Consider y' = x^2-y^2, eqn_11:=diff(y(x),x)=x^2+y(x)^2;

DEplot(eqn_11,y(x),x=-3..3,y=-3..3);

3 2

y(x)
1

There are several types of arrows: small, medium, large and line. The default (see above) is small. DEplot(eqn_11,y(x),x=-3..3,y=-3..3,arrows=medium);

3 2

y(x)
1

DEplot(eqn_11,y(x),x=-3..3,y=-3..3,arrows=large);

DEplot(eqn_11,y(x),x=-3..3,y=-3..3,arrows=line);

3 2

y(x)
1

The option arrows = none supresses the direction field. In the above example Maple chooses where to calculate the direction field.

You might also like