Unit 1: Basic Usage of Maple: 1. Command Line, Names, Help
Unit 1: Basic Usage of Maple: 1. Command Line, Names, Help
Outline 1. Command line, names, help 2. Symbolic computations 3. Exact computations 4. Float computations 5. Evaluating expressions 6. Context-sensitive menus 7. Graphics 8. Derivatives 9. Integrals 10. Equations 11. Differential equations 12. Symbolic ODE solving 13. Direction fields 14. Numeric ODE solving 15. Maple vs Matlab
A Maple nano-course
e := 1 Cd 2 We can use the character # to add comments to the command line. > expand e # command expand( ) forces product evaluations. 1 C2 d Cd2 Enclosing an expression between right quotes avoids direct evaluation > c d'A Ca' c := a CA > c 1.5 The evaluation of an unassigned name returns the name. > a 1 > f f We can clean a name by assigning the quoted name to itself. > c d'c'; c := c Command restart cleans all the names. > b 3 > e 1 Cd 2 > restart > b b > e e Names of Greek letters can be used as Maple names, they are displayed using the Greek alphabet. Note that Maple interprets a black space as an implicit multiplication. > alpha d 3; := 3 > c d A cos phi t c := A cos t Help. We can get access to the help system pressing [Ctrl]+[F1] or using the question mark?. > ?eval Execution groups. We can write several commands in one line, in this case we have to separate them with semicolons > 1 C1; a d 3; expand b Ca 2 2 a := 3 b2 C6 b C9
Page 2 of 19
A Maple nano-course
To improve readability, we can break the command line without executing the code pressing [SHIFT]+[INTRO] (simultaneously). Use the underscore character to enter subindexes > e0 d 14; v0 d 2; a dK 0.5; t d 3; 1 ef d e0 Cv0$t C $a$t2 2 e0 := 14 v0 := 2 a := K 0.5 t := 3 ef := 17.75000000
2. Symbolic computations
Go to outline Maple uses clean names as algebraic objects. > x d 1; y d 2 x := 1 y := 2 > x Cy 3 > x d'x'; y d'y' x := x y := y > p d x Cy
3
p := x Cy 3 Command expand forces the evaluation of all the products. > expand p x3 C3 x2 y C3 x y2 Cy3 We use command factor( ), to factorize expressions, and command simplify( ) for simplifications. 2 > q d x C3 x C2; q := x2 C3 x C2 > factor q x C2 x C1 x2 K4 > rd 3 x K6 x2 C11 x K6 r := > simplify r x C2 x K4 x C3 We can get access to the numerator and the denominator of a fraction with numer( ), and denom( ).
2
x2 K4 x3 K6 x2 C11 x K6
Page 3 of 19
A Maple nano-course
> factor denom r x K1 > factor numer r x K2 x C2 simplify( ) also works with trigonometric expressions. > a d sin phi ; b d cos phi ; c d a2 Cb2; a := sin b := cos c := sin > c1 d simplify c c1 := 1 Partial fraction decomposition can be obtained with convert(expression , parfrac) > r x2 K4 x3 K6 x2 C11 x K6 > rp d convert r, parfrac 5 3 rp := K 2 x K3 2 x K1
2
x K2
x K3
Ccos
3. Exact computations
Go to outline Exact numbers are operated without rounding. Integers are exact numbers. Fractions are simplified automatically. 12 > ad 32 3 a := 8 > b d 3 Ca 27 b := 8 Functions on exact numbers produce exact numbers. > a d sqrt 3 a := 3 > b d 1 Ca > expand b 4 C2 A trigonometric example. > a d sin 3 ; b d cos 3 ; c d a Cb 2; c1 d expand c ; simplify c1 ; a := sin 3
Page 4 of 19
2
b := 1 C 3 3
A Maple nano-course
b := cos 3 c := cos 3 Csin 3 c1 := cos 3 We can use integers of arbitrary size > a d 240 a := 1099511627776 > b d 680 b := 178689910246017054531432477289437798228285773001601743140683776 a > dd b 1 d := 162517526629032594911616322214684076451054145765376 The number is Pi. Pi > cos 4 1 2 pi is the Greek letter . It is not a number. pi > cos 4 cos 1 4 2
2 2 2
4. Float computations
Go to outline We can get float approximations of exact values with evalf( ) > a d sqrt 2 a := 2 > af d evalf a af := 1.414213562 Functions applied on float arguments, produce float results. > b d exp 2 b := e2 > bf d evalf b bf := 7.389056099 > exp 2. 7.389056099 > cos 0.2 0.9800665778 By default, Maple uses 10-digit float evaluations. Command evalf(value,n), computes a n-digit float evaluation. > evalf Pi 3.141592654 > evalf Pi, 100 3.141592653589793238462643383279502884197169399375105820974944592307816406286\
Page 5 of 19
A Maple nano-course
208998628034825342117068 The system variable Digits sets the number of digits for floats. By default, the value of Digits is 10. We can assign new values to Digits. > cos 2. K 0.4161468365 > Digits d 20 Digits := 20 > cos 2. K 0.41614683654714238700 Command restart sets the system variable Digits back to its default value
5. Evaluating expressions
Go to outline We can evaluate expressions with eval(expression, var=value). > restart; > f d x2 Ccos x f := x2 Ccos x > vf d eval f, x = 2 vf := 4 Ccos 2 Evaluations with float values, normally produce float results. > vf2 d eval f, x = 3. vf2 := 8.010007503 eval( ), can handle multiple evaluations and symbolic values. > f d x2 Ca x Cb; f := x2 Ca x Cb > pvals d a = 1, b = z Ky, x = 2 pvals := a = 1, b = z Ky, x = 2 > fp d eval f, pvals fp := 6 Cz Ky Note that the names a, b, and x remain unassigned. > a a > b b > x x
6. Context-sensitive menus
Go to outline Most frequent commands are available in contex-sensitive menus, which can be accessed by mouse right-click. When using the context-sensitive menus in the worksheet mode, Maple write the commands for us. > f d x3 K3 x C2 f := x3 K3 x C2 > diff x^3 K3 * x C2, x 3 x2 K3
Page 6 of 19
A Maple nano-course
5 x
10
7. Graphics
Go to outline We can plot expressions with plot( ). > f d 4 Kx2 f := 4 Kx2 > plot f, x = 0 ..2
4 2 0 0 0.5 1 x 1.5 2
> gd
1 x2 g := 1 x2
Page 7 of 19
A Maple nano-course
The graphic in the previous plot cannot be properly appreciated due to the vertical asymptote in x=0. If convenient, we can indicate a proper interval for the dependent variable. > plot g, x =K ..2, y =K ..10 2 1
10 8 6 y 4 2 K 2 K 1 0 1 x 2
Some nice plot options are gridlines, thickness, and color > plot g, x =K ..2, y =K ..10, thickness = 2, color = blue, gridlines 2 1
10 8 y 6 4 2 K 2 K 1 0 1 x 2
To draw multiple plots, we pass a list of expressions [f1,f2,....] to plot > plot f, g , x =K ..4, y =K ..10, color = red, blue , thickness = 2, gridlines 4 2
10 8 6 y 4 2 K 4 K 3 K 2 KK 1 2 1 2 x 3 4
8. Derivatives
Go to outline Command diff(f,x) computes the partial derivative of f with respect to x.
Page 8 of 19
A Maple nano-course
> f d x sin x f := x sin x > df d diff f, x ; df := sin x Cx cos x Second order derivatives can be calculated with diff(f,x,x). > df2 d diff f, x, x df2 := 2 cos x Kx sin x For higher order derivatives, we use the operator $. > x$4 x, x, x, x Fourth derivative > df4 d diff f, x$4 df4 := K cos x Cx sin x 4 Maple can also do an inductive computation of the generic n-th derivative > n d'n' n := n > g d x exp x g := x ex > dfn d diff g, x$n dfn := ex x Cn Prime notation. Derivative with respect to x can be computed using the prime notation. > f d x$sin x f := x sin x > f' sin x Cx cos x > f''' K sin x Kx cos x 3 Dot notation. Derivatives with respect to t can be computed using the dot notation. Press [Ctrl] +[Shift]+["] for quick introduction of the dot notation. sin t > yd t sin t y := t . > y1 d y cos t sin t y1 := K t t2 .. > y2 d y sin t 2 cos t 2 sin t y2 := K K C 2 t t t3
9. Integrals
Go to outline The command int(t,x) computes a primitive of f with respect to x. > x d'x' x := x > f d x sin x f := x sin x
Page 9 of 19
A Maple nano-course
> F d int f, x F := sin x Kx cos x Definite integrals can be computed with int(f,x=a..b). > f d x2 f := x2 > v d int f, x = 0 ..1 1 v := 3 Symbolic limits of integration can also be used. > a d'a'; b d'b'; a := a b := b > v d int f, x = a ..b 1 3 1 3 v := b K a 3 3 If Maple cannot obtain an exact value, the integral is returned unevaluated. In this case, a float approximation can be obtained with evalf( ). > f d sin x6 Cx f := sin x6 Cx > v d int f, x = 0 ..1
1
v :=
0
sin x6 Cx dx
10. Equations
Go to outline Exact and symbolic solving. Exact and symbolic equation solutions can be computed with solve( ). > restart > eq d 2 x C1 = 5 eq := 2 x C1 = 5 > s d solve eq s := 2 For equations f(x)=0, we can use the simplified form solve(f) > f d x2 C2 x K3 f := x2 C2 x K3 > s d solve f s := 1, K 3 We have obtained a sequence of solutions. To access the members of the sequence, we use s[1] and s[2]. > s 1 1 > s 2 K 3 In 2D math mode, we can also use subindex notation. The underscore character can be used in
Page 10 of 19
A Maple nano-course
2D math mode for quick writing of subindexes. 1 > s1 1 > s2 K 3 When the equation contains more than one algebraic variable, we can solve with respect to any of them. > eq d a x2 Cb x Cc eq := a x2 Cb x Cc > sx d solve eq, x sx := > sa d solve eq, a sa := K > sb d solve eq, b a x2 Cc x Numerical solutions. Numerical equation solving can be performed with fsolve( ). > eq d 2 x2 Kcos x Kx3 = 0 eq := 2 x2 Kcos x Kx3 = 0 This equation has no exact solutions. > solve eq RootOf K _Z2 Ccos _Z C_Z3 2 We use fsolve( ) to compute a numerical solution. > s1 d fsolve eq s1 := 0.7643136487 To investigate the existence of other roots in the interval [-1..3], we extract the equation lefthand-side with lhs( ), and make a plot in the interval > f d lhs eq f := 2 x2 Kcos x Kx3 1 5 > plot f, x =K ..3, y =K ..5 sb := K b x Cc x2 1 KC b 2 b2 K4 a c 1 bC ,K a 2 b2 K4 a c a
4 y 2 K 1 0 K 2 K 4
We can see that fsolve( ) has computed the solution in the interval [0,1]. To can compute the other two solutions by indicating the searching interval to fsolve( ). > s2 d fsolve eq, x =K ..0 1
Page 11 of 19
1 x
A Maple nano-course
2 1.5 x 1 0.5 0 0 1 2 t 3 4
A Maple nano-course
.. . > ode d x C2 x C5 x = 0 d2 ode := 2 x t C2 dt Initial conditions. . > ics d x 0 = 2, x 0 =K 1 ics := x 0 = 2, D x 0 = K 1 ODE solution > s d dsolve ode, ics s := x t = Expression containing the solution > fx d eval x t , s fx := Value of x for t = 1.6 > vx d eval fx, t = 1.6 vx := K 0.4089972466 x t plot for 0 % t % 6 > plot fx, t = 0 ..6, gridlines, thickness = 2 1 K t e t sin 2 t C2 eK cos 2 t 2 1 K t e t sin 2 t C2 eK cos 2 t 2 d x t dt C5 x t = 0
2 1.5 1 0.5 0 1 2 3 t 4 5 6
Time required for x to get the value 0.75 > t1 d fsolve fx = 0.75 t1 := 0.5618757616
a a Cw2
2
A Maple nano-course
2
a cos w t a Csin w t w C 2 a Cw a2 Cw2 To compute the the solution for a particular set of values, we use eval again. > pval d a = 2, w = 10, y0 = 3 pval := a = 2, w = 10, y0 = 3 > fp d eval f, pval 155 K t 1 5 e 2 C cos 10 t C sin 10 t 52 52 52 we can also get a float version of this solution > fpf d evalf fp 2. fpf := 2.980769231 eK t C0.01923076923 cos 10. t C0.09615384615 sin 10. t Plot and particular value for t=1.3 > v1 d eval fp, t = 1.3 v1 := 0.2792439728 > plot fp, t = 0 ..4 fp :=
3 2 1 0 0 1 2 t 3 4
Multiple plot displaying the output and the excitation > plot cos 10 t , fp , t = 0 ..5, color = red, blue , gridlines, legend = "Excitation", "Output" , thickness = 2
3 2 1 0 K 1 1 2 t 3 4 5
Excitation
Output
A Maple nano-course
MeijerGsols, MultiplicativeDecomposition, ODEInvariants, PDEchangecoords, PolynomialNormalForm, RationalCanonicalForm, ReduceHyperexp, RiemannPsols, Xchange, Xcommutator, Xgauge, Zeilberger, abelsol, adjoint, autonomous, bernoullisol, buildsol, buildsym, canoni, caseplot, casesplit, checkrank, chinisol, clairautsol, constcoeffsols, convertAlg, convertsys, dalembertsol, dcoeffs, de2diffop, dfieldplot, diff_table, diffop2de, dperiodic_sols, dpolyform, dsubs, eigenring, endomorphism_charpoly, equinv, eta_k, eulersols, exactsol, expsols, exterior_power, firint, firtest, formal_sol, gen_exp, generate_ic, genhomosol, gensys, hamilton_eqs, hypergeomsols, hyperode, indicialeq, infgen, initialdata, integrate_sols, intfactor, invariants, kovacicsols, leftdivision, liesol, line_int, linearsol, matrixDE, matrix_riccati, maxdimsystems, moser_reduce, muchange, mult, mutest, newton_polygon, normalG2, ode_int_y, ode_y1, odeadvisor, odepde, parametricsol, particularsol, phaseportrait, poincare, polysols, power_equivalent, rational_equivalent, ratsols, redode, reduceOrder, reduce_order, regular_parts, regularsp, remove_RootOf, riccati_system, riccatisol, rifread, rifsimp, rightdivision, rtaylor, separablesol, singularities, solve_group, super_reduce, symgen, symmetric_power, symmetric_product, symtest, transinv, translate, untranslate, varparam, zoom
2 > ode d y'= x Ky
d y x = x Ky x dx
2 y(x) 1
0 K 1 K 2
2 x
We can also include some particular solutions > ps d y 0 = 0 , y 0 = 1 , y 0 = 2 , y 0 =K , y 0 =K 0.7 0.8 ps := y 0 = 0 , y 0 = 1 , y 0 = 2 , y 0 = K , y 0 = K 0.7 0.8 > DEplot ode, y x , x = 0 ..5, y =K ..2, ps 2
Page 15 of 19
A Maple nano-course
2 y(x) 1
0 K 1 K 2
2 x
The option linecolor can be used to assign colors to the different particular solutions. The option color controls the arrows' color > ps d y 0 = 0 , y 0 = 1 , y 0 = 2 , y 0 =K , y 0 =K 0.7 0.8 ps := y 0 = 0 , y 0 = 1 , y 0 = 2 , y 0 = K , y 0 = K 0.7 0.8 > pcol d black, blue, green, cyan, red pcol := black, blue, green, cyan, red 2 > DEplot ode, y x , x = 0 ..5, y =K ..2, ps , linecolor = pcol , color = gray
2 y(x) 1
1 K 1 K 2
2 x
d y x = K 2 y x Ccos x x dx
x 1 _z13 3 K 1 3 x 3
s := y x = e
1 3 x 3
cos _z1 e
0
d_z1 Ce
In this case, we can obtain a numerical solution with the option numeric. > sn d dsolve ode, y 0 = 1 , y x , numeric sn := proc x_rkf45 ... end proc The result is a procedure that implements a numerical method to approximate the solution. By default, dsolve implements the Runge-Kutta forward 45 method; if convenient, a variety of
Page 16 of 19
A Maple nano-course
other numerical methods can be selected. > sn 1.2 x = 1.2, y x = 1.14961123596730 To draw a plot of the solution using the procedure provided by dsolve, we can use the command odeplot( ), included in the library plots. > with plots animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal, conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, densityplot, display, dualaxisplot, fieldplot, fieldplot3d, gradplot, gradplot3d, implicitplot, implicitplot3d, inequal, interactive, interactiveparams, intersectplot, listcontplot, listcontplot3d, listdensityplot, listplot, listplot3d, loglogplot, logplot, matrixplot, multiple, odeplot, pareto, plotcompare, pointplot, pointplot3d, polarplot, polygonplot, polygonplot3d, polyhedra_supported, polyhedraplot, rootlocus, semilogplot, setcolors, setoptions, setoptions3d, spacecurve, sparsematrixplot, surfdata, textplot, textplot3d, tubeplot > odeplot sn, x = 0 ..5
1.4 1 y 0.6 0
2 x
With the option output=listporcedure, odeplot( ) produces separate procedures for each output > sn d dsolve ode, y 0 = 1 , y x , numeric, output = listprocedure sn := x = proc x ... end proc, y x = proc x ... end proc We can extract the procedure that computes y x selecting the right-hand-side of the second element in sn. Note that sn[2] (or sn2) is the second element in the list sn > sn 2 y x = proc x ... end proc We want to extract the right-hand-side of sn[2] > py d rhs sn 2 py := proc x ... end proc This isolated procedure py computes the approximate value of y x with no label. > py 1.2 1.14961123596730 Now, the solution can be plotted in the usual way with the plot( ) command. > plot py x , x = 0 ..5, gridlines, thickness = 2
Page 17 of 19
A Maple nano-course
1.4 1 0.6 0
2 x
If we try to draw a direction field for the current ode with DEplot, we find that command DEplot no longer works. > DEplot ode, y x , x = 0 ..5, y =K ..2 2 d DEplot y x = K 2 y x Ccos x , y x , x = 0 ..5, y = K ..2 x 2 dx The reason is that we have issued a restart command at the beginning of the present section and all previously loaded libraries are unloaded by command restart. > with DEtools : > DEplot ode, y x , x = 0 ..5, y =K ..2 2
2 y(x) 1
0 K 1 K 2
2 x
We can also include our particular solution and some nice options. > DEplot ode, y x , x = 0 ..5, y =K ..2, y 0 = 1 , linecolor = blue, color = grey, arrows 2 = medium
2 y(x) 1
0 K 1 K 2
>
2 x
Page 18 of 19
A Maple nano-course
Run Assign Avoid result display Comments Separate commands Brake command line without executing Work with unassigned variables
Page 19 of 19