Math2800 ODE's Introduction

Embed Size (px)

Citation preview

  • 7/31/2019 Math2800 ODE's Introduction

    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 sufficientlysimple) or numerically. For example, consider the first-order linear ODE y' + 2xy = x. Call theODE 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 yis a function of x. Furthermore, the ordinary derivative is represented as a partial derivative.This because the diffcommand is used for both ordinary and partial derivatives. To see if Maplecan 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 thisODE 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).

  • 7/31/2019 Math2800 ODE's Introduction

    2/31

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

    Here Maple can solve the equation exactly and the solution is given in terms of two specialfunctions 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 thecase 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 berepresented 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

  • 7/31/2019 Math2800 ODE's Introduction

    3/31

    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 givenx, (iii) evaluate the second derivative of y at a given point, (iv) plot the second derivative, etc. Inorder 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 thesolution,

    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 yat 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 definedby 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 definedby 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 assuch 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

  • 7/31/2019 Math2800 ODE's Introduction

    4/31

    command is used,

    evalf(y(-sqrt(Pi)));

    Maple can plot this solution using theplot command,

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

    x

    0 2 4

    I have decided to take the range of x be -5

  • 7/31/2019 Math2800 ODE's Introduction

    5/31

    x

    0 2 4

    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 plotof 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 itsdefinition 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 Itry 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);

  • 7/31/2019 Math2800 ODE's Introduction

    6/31

    x

    0 2 4

    1

    2

    3

    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 withidentification using the option legend (see ). Save the plot of y''(x) in different colour andappropriate 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 theplotspackage. 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);

  • 7/31/2019 Math2800 ODE's Introduction

    7/31

    y(x) y''(x)

    x

    0 2 4

    1

    2

    3

    Aside - Helping Maple to Graph Functions

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

  • 7/31/2019 Math2800 ODE's Introduction

    8/31

    x

    0 5 10

    200

    400

    600

    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);

    x

    0 5 10

    y2

    4

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

  • 7/31/2019 Math2800 ODE's Introduction

    9/31

    Maple will try to adjust the graph,

    plot(tan(x),x=-10..10,y=-5..5,discont=true);

    x

    0 5 10

    y2

    4

    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

  • 7/31/2019 Math2800 ODE's Introduction

    10/31

    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 alreadybeen 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);

  • 7/31/2019 Math2800 ODE's Introduction

    11/31

    y(x) y'(x)

    x

    0 1 2

    1

    2

    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 touse 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);

  • 7/31/2019 Math2800 ODE's Introduction

    12/31

    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);

    x

    0 2 4

    y 50

    100

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

  • 7/31/2019 Math2800 ODE's Introduction

    13/31

    x

    0 2 4

    y 50

    100

    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);

  • 7/31/2019 Math2800 ODE's Introduction

    14/31

    y(x) y'(x)

    x

    0 2 4

    y 50

    100

    Second Order IVP - Numerical Solution

    8. Of course, if an ODE is complicated enough Maple will not be able to solve it exactly. Forexample, 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);

  • 7/31/2019 Math2800 ODE's Introduction

    15/31

    sol_7(1);

    The procedure sol_7 evaluated at a given value x_0 is a list from which we can extract thenumbers y(x_0) and y'(x_0). For example, suppose I need to know the value of the solution andits 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 itsslope at that point is 1.97, correct to two decimal places. We can use the odeplot command tograph this numerical solution. The odeplot command lives in the plots package which we havealready loaded.

    odeplot(sol_7);

  • 7/31/2019 Math2800 ODE's Introduction

    16/31

    x

    0 5 10

    y

    5

    10

    15

    20

    We can look at various part of this solution curve using the view option,

    odeplot(sol_7,view=[-2..1,-2..3]);

    x

    0 1

    y

    1

    2

    3

    First Order IVP - Numerical Solutions

  • 7/31/2019 Math2800 ODE's Introduction

    17/31

    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. Forexample,

    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 approachesinfinity) 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

  • 7/31/2019 Math2800 ODE's Introduction

    18/31

    t

    0 1

    y

    Maple cannot extend the solution outside the interval (-1.02389133424902656,1.02388960782259697) because it cannot integrate outside this integral. It is also clear that weneed 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

  • 7/31/2019 Math2800 ODE's Introduction

    19/31

    t

    0 1

    y

    The inability of Maple to integrate past suggests that the solution curve becomes very steep as tapproaches -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 derivativey'(x). In this case we do not have a formula for y(x) which we could differentiate. However, it isstill possible to plot y'(x). Firstly, we need to redo the numerical integration with the optionoutput=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 plotcommand 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"]);

  • 7/31/2019 Math2800 ODE's Introduction

    20/31

    t

    0 1

    y

    Now, let ne try to costruct an approximation to the derivative function. The ODE in normal formis 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'"]);

  • 7/31/2019 Math2800 ODE's Introduction

    21/31

    t

    0 1

    y' 5

    10

    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''"]);

  • 7/31/2019 Math2800 ODE's Introduction

    22/31

    t

    0 1

    y'' 5

    10

    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);

  • 7/31/2019 Math2800 ODE's Introduction

    23/31

    y(t) y'(t) y''(t)

    t

    0 1

    y 5

    10

    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 numericsolution

    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

  • 7/31/2019 Math2800 ODE's Introduction

    24/31

    x0 1

    y 1

    2

    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 solutioncurve cannot be cut by a vertical line more than once. Thus, in this example, the solution curvecannot extend past -1 and 1. However, this solution curve can be extended and the resultingcurve is called an integral curve. An integral curve is simply a union of one or more solutioncurves. 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 whichare 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 thedenominator of the original ODE). Note that y'(x) = y'(t) / x'(t). Secondly, the initial value y =1when x = 0 can be rewritten as y = 1 and x =0 when t = 0, i.e., x(0) = 0 and y(0) = 1. LoadingDEtools and substituting the ODEs, dependent variables, the range of t ([0,1] is a reasonablestarting 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 theinitial conditions are written separately (also enclosed in square braces).

    DEplot(eqn_10,[x(t),y(t)],t=0..1,IC_10);

  • 7/31/2019 Math2800 ODE's Introduction

    25/31

    x0 1 2 3

    y

    1

    2

    3

    4

    5

    By default DEplot shows the direction arrows (these are tangential to the integral curves andpoint in the direction of increasing t). Normaly, we want to suppress these arrows using thearrows=none option,

    DEplot(eqn_10,[x(t),y(t)],t=0..1,IC_10,arrows=none);

    x0 1 2 3

    y

    1

    2

    3

    4

    5

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

  • 7/31/2019 Math2800 ODE's Introduction

    26/31

    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);

    x0 1 2 3

    y

    1

    2

    3

    4

    5

    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 specifywhich 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);

  • 7/31/2019 Math2800 ODE's Introduction

    27/31

    x

    0 1 2 3 4

    y

    2

    4

    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);

    x

    0 1 2 3 4

    y

    2

    4

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

  • 7/31/2019 Math2800 ODE's Introduction

    28/31

    p_10 be the graph of the integral curve and p_11 be the graph of the solution curve (I have madethe 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]);

    x

    0 1 2 3 4

    y2

    4

    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 threesolution curves: one defined on y>2, one defined on 2 < y < 0 (pictured) and one defined on y