Lesson 2: Variables, Assignment and EquationsMaple has extensive graphics capabilities. Here's a graph of a function.plot(x^2 - 3*x - 4, x = -2 .. 5);A 3-dimensional graph:plot3d(x^2 - y^2, x = -2 .. 2, y = -2 .. 2);An animation:plots[animate](plot, [sin(x-t),x=-Pi..Pi], t=0..2*Pi);Maple is incredibly powerful, but to learn to use it effectively takes some effort. One thing to remember: Maple has absolutely no intelligence. It will try to do exactly what you tell it to do, no more and no less.Unfortunately, what you tell it to do is not always the same as what you thought you were telling it to do, or what you wanted it to do.
It has no idea of what you want to do, or why you want to do it.
It can't read your mind.
So to get it to do something, you have to know what command would make it do that, and how to use that command.HelpMaple has thousands of functions and commands. Probably no one person knows all the details of all of them. Fortunately, Maple has an extensive help system. To get help on a particular command, you can enter a question mark followed by the name of the command:?intOr you can use the Table of Contents, or Topic Search or Text Search from the Help menu.MistakesYou will make mistakes in Maple. Everybody does. Much of your time using Maple will be spent in trying to figure out what you did wrong, and how to correct it. It's important to look at the result Maple is giving you; if it's not what you expected, there's probably a reason for that.2+3This was just a warning: I forgot the semicolon at the end. 2+-3;That's an error: you can't have one arithmetic operator followed directly by another. Maybe it should have been 2 + (-3).2+(3-5;The end parenthesis was missing.evafl(3/17);A spelling mistake. Maple doesn't catch this: it just acts as if "evafl" is the name of a function it doesn't know about.expand((x+y+z)^(10^6));This could cause a crash: the answer would have over 500 billion terms. It's very unlikely that your computer has a memory big enough to store that. If a limit to Maple's memory has been set, as discussed on the web page and in our first class, it should fail "gracefully".Caution: We put -T infinity, 100000 onto the Maple icon. That won't have any effect if you launched Maple in some other way (e.g. by clicking on a worksheet file) rather than from the icon.If Maple does crash, one problem is that you could lose the work you've been doing. The remedy for this is to save your worksheet. There's an "auto-save" feature that will automatically save a backup copy at regular intervals, if you have already saved your file (so it isn't "Untitled"). Under the "File" menu you can choose "Recent Documents" and then "Restore Backup".Another useful button on the Tool Bar is the hand in a stop sign (which is red when Maple is doing a calculation). This button is supposed to interrupt whatever Maple is doing. Unfortunately it doesn't always work: some things don't like to be interrupted. When it does work, it should tell you Warning, computation interruptedAfter an interruption, Maple may or may not work, but at least you should be able to save your worksheet and start again.The Maple help system includes an Error Message Guide that may be helpful in interpreting what error messages mean.VariablesVariables in Maple are names. Ordinary names can start with a letter (a to z, A to Z) and can contain letters, digits and the underscore _. So these are names:x, X, name, NCC_1701;Case matters, so x and X are different names. These are not names: 2+x, x+w;The names x, X, name, NCC_1701 have not been given values: they are said to be unassigned, or sometimes free variables. You could use such a name to represent, for example, a mathematical variable.x^3 + 3*x + X;A name can be assigned a value using the := operator. Whatever is on the right of the := is assigned as the value of the name on the left. x := 5;x + 3;Once assigned, the name keeps its assigned value through the rest of the Maple session, unless you give it a different value.x := 2;x + 3;Notice that the Maple output from the previous "x + 3" hasn't changed, even though the value of x has. So the worksheet keeps a record of what the results were at the time the command was entered, which isn't necessarily the same as what it would be now. You could click on the previous command, and press Enter again, and you'll get 5. But now things will look strange to an unsuspecting person reading the worksheet. Therefore: try to avoid doing this sort of thing! Keep your worksheet in a condition where if you started at the beginning and performed the commands, one after the other, the results would be the output you see.But it's awfully hard to avoid temptation. You do something, and get some results, and then say, "Oh, I should have done that differently five commands ago", so you go back and change it. But meanwhile you've assigned some variables, and so when you redo the command the results will be different because of that.There are two very helpful tools: restart and the !!! button.restart is a command that tells Maple to go back to how it was at the beginning, when you started the session, with all variables unassigned.restart;x + 3;The !!! button redoes all commands in the worksheet, from start to end.It's a good idea to have restart at the beginning of your worksheet, so that if you do press !!! it starts with a clean slate.Any variable can be assigned essentially any value: a number, another name, or a complicated expression. x := t;t:= s;Now what do you suppose I'll get if I look at x again?x;What's happened here is that x has the value t, and t has the value s. Maple evaluates x in two stages: x is evaluated to t, and then in turn t is evaluated to s. This chain of evaluations could be as long as you want.Now let's say I don't want x to have a value any more: I want to unassign it. It wouldn't help to sayx := x;because this doesn't assign x the value x, it assigns x what Maple gets by evaluating x, namely s. Instead what you can do is this:x := 'x';Those ' ' are single quotes (the ones next to Enter on the keyboard).x + 3;There are some exceptions to the rule that a name can be assigned a value.name := 3;Certain names are part of the Maple language, and you are not allowed to give them values. If you did, it would be likely to mess up all sorts of Maple commands that use those names.Equations and how to solve themWhile := produces an assignment, = by itself makes a mathematical equation.a = 3;That didn't assign any value to the name a, or change it in any way.a;What can you do with an equation? For example, you can ask whether it is true.is((x+y)*(x-y) = x^2 - y^2);is(1+1 = 3);Or you can solve it.solve(x+3 = 2*x - 4);Since there was only one variable in this equation, I didn't have to tell solve what variable to solve for. solve(a + 3*x = 5);What Maple is saying here is that x can be anything, and then a is LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JkYrLUYjNictSSNtb0dGJDYtUSomdW1pbnVzMDtGJy8lLG1hdGh2YXJpYW50R1Enbm9ybWFsRicvJSZmZW5jZUdRJmZhbHNlRicvJSpzZXBhcmF0b3JHRjwvJSlzdHJldGNoeUdGPC8lKnN5bW1ldHJpY0dGPC8lKGxhcmdlb3BHRjwvJS5tb3ZhYmxlbGltaXRzR0Y8LyUnYWNjZW50R0Y8LyUnbHNwYWNlR1EsMC4yMjIyMjIyZW1GJy8lJ3JzcGFjZUdGSy1GIzYmLUkjbW5HRiQ2JFEiM0YnRjctRjQ2LVExJkludmlzaWJsZVRpbWVzO0YnRjdGOkY9Rj9GQUZDRkVGRy9GSlEmMC4wZW1GJy9GTUZYLUYsNiVRInhGJy8lJ2l0YWxpY0dRJXRydWVGJy9GOFEnaXRhbGljRidGNy1GNDYtUSIrRidGN0Y6Rj1GP0ZBRkNGRUZHRklGTC1GUTYkUSI1RidGN0Y3RitGN0YrRjc=. Oh, but that's not what I meant, I wanted it to solve for x. But Maple doesn't read minds. I have to tell it what I want it to do.solve(a + 3*x = 5, x);If I put the x in curly braces, Maple doesn't just return the value of x, it returns an equation, also in curly braces. We'll see the reason for the curly braces later.solve(a + 3*x = 5, {x});An equation itself is a Maple object, by the way, so you can assign it as the value of a variable. This is handy if you want to use the same equation many times, and don't want to have to type it in each time. eq:= a + 3*x = 5;solve(eq, {x});Let's try a quadratic equation.solve(x^2 - 3*x - 4);I left out the x or {x} here, because there's only one variable. I also left out the "=0": if you give solve an expression instead of an equation, it interprets it as (expression) = 0.
There are two solutions, LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2Ji1GLDYlUSJ4RicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0YnLUkjbW9HRiQ2LVEpJmVxdWFscztGJy9GOFEnbm9ybWFsRicvJSZmZW5jZUdRJmZhbHNlRicvJSpzZXBhcmF0b3JHRkIvJSlzdHJldGNoeUdGQi8lKnN5bW1ldHJpY0dGQi8lKGxhcmdlb3BHRkIvJS5tb3ZhYmxlbGltaXRzR0ZCLyUnYWNjZW50R0ZCLyUnbHNwYWNlR1EsMC4yNzc3Nzc4ZW1GJy8lJ3JzcGFjZUdGUS1JI21uR0YkNiRRIjRGJ0Y+Rj5GK0Y+ and LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2Ji1GLDYlUSJ4RicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0YnLUkjbW9HRiQ2LVEpJmVxdWFscztGJy9GOFEnbm9ybWFsRicvJSZmZW5jZUdRJmZhbHNlRicvJSpzZXBhcmF0b3JHRkIvJSlzdHJldGNoeUdGQi8lKnN5bW1ldHJpY0dGQi8lKGxhcmdlb3BHRkIvJS5tb3ZhYmxlbGltaXRzR0ZCLyUnYWNjZW50R0ZCLyUnbHNwYWNlR1EsMC4yNzc3Nzc4ZW1GJy8lJ3JzcGFjZUdGUS1JI21uR0YkNiRRKyZ1bWludXMwOzFGJ0Y+Rj5GK0Y+, and Maple gives us both. Of course, the solution of a quadratic equation could involve square roots.solve(x^2 - 3*x - 5);... or complex numbers.solve(x^2 - 3*x + 4);Maple uses LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSJJRicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0YnL0Y4USdub3JtYWxGJ0YrRjo= for LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1JJm1zcXJ0R0YkNiMtRiM2JS1JI21vR0YkNi1RKiZ1bWludXMwO0YnLyUsbWF0aHZhcmlhbnRHUSdub3JtYWxGJy8lJmZlbmNlR1EmZmFsc2VGJy8lKnNlcGFyYXRvckdGPy8lKXN0cmV0Y2h5R0Y/LyUqc3ltbWV0cmljR0Y/LyUobGFyZ2VvcEdGPy8lLm1vdmFibGVsaW1pdHNHRj8vJSdhY2NlbnRHRj8vJSdsc3BhY2VHUSwwLjIyMjIyMjJlbUYnLyUncnNwYWNlR0ZOLUkjbW5HRiQ2JFEiMUYnRjpGOkY6RitGOg== instead of LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSJpRicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0YnL0Y4USdub3JtYWxGJ0YrRjo=. How about a cubic?solve(x^3 - 3*x + 4);Three solutions, two of which involve LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSJJRicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0YnL0Y4USdub3JtYWxGJ0YrRjo= (the commas separating the solutions are sometimes hard to spot). What are their numerical values? It would have been useful to have saved the result in a variable. I could have saidsolutions := solve(x^3 - 3*x + 4);But actually I didn't need to redo the command, because there is a special variable: %. Maple always assigns this the result of the last command. So I could have just saidsolutions := %;There's also %% for the second-last and %%% for the third-last (but that's as far back as it goes). Now that I have the three solutions, I can refer to any particular one as solutions[1], solutions[2] or solutions[3], putting the number in square brackets.solutions[2];evalf(%);Next a polynomial of degree 5:solutions := solve(x^5 - x^3 + 2*x + 1);What does this somewhat bizarre notation mean? RootOf just means "a solution of": it writes the polynomial, using its own variable LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSNfWkYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy9GOFEnbm9ybWFsRidGK0Y6 instead of the variable you used, and then LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSZpbmRleEYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy9GOFEnbm9ybWFsRidGK0Y6 = (something from 1 to 5). That's Maple's way of keeping track of which solution is which. So RootOf( LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYxLUklbXN1cEdGJDYlLUkjbWlHRiQ2JVEjX1pGJy8lJ2l0YWxpY0dRJXRydWVGJy8lLG1hdGh2YXJpYW50R1EnaXRhbGljRictSSNtbkdGJDYkUSI1RicvRjZRJ25vcm1hbEYnLyUxc3VwZXJzY3JpcHRzaGlmdEdRIjBGJy1JI21vR0YkNi1RKCZtaW51cztGJ0Y8LyUmZmVuY2VHUSZmYWxzZUYnLyUqc2VwYXJhdG9yR0ZHLyUpc3RyZXRjaHlHRkcvJSpzeW1tZXRyaWNHRkcvJShsYXJnZW9wR0ZHLyUubW92YWJsZWxpbWl0c0dGRy8lJ2FjY2VudEdGRy8lJ2xzcGFjZUdRLDAuMjIyMjIyMmVtRicvJSdyc3BhY2VHRlYtRiw2JUYuLUY5NiRRIjNGJ0Y8Rj4tRkI2LVEiK0YnRjxGRUZIRkpGTEZORlBGUkZURlctRjk2JFEiMkYnRjwtRkI2LVExJkludmlzaWJsZVRpbWVzO0YnRjxGRUZIRkpGTEZORlBGUi9GVVEmMC4wZW1GJy9GWEZib0YuRmhuLUY5NiRRIjFGJ0Y8LUZCNi1RIixGJ0Y8RkUvRklGNEZKRkxGTkZQRlJGYW8vRlhRLDAuMzMzMzMzM2VtRictRi82JVEmaW5kZXhGJ0YyRjUtRkI2LVEiPUYnRjxGRUZIRkpGTEZORlBGUi9GVVEsMC4yNzc3Nzc4ZW1GJy9GWEZkcC1GOTYkUSI0RidGPC1GQjYtUSIpRidGPC9GRkY0RkgvRktGNEZMRk5GUEZSL0ZVUSwwLjE2NjY2NjdlbUYnL0ZYRl9xRjw= just means the fourth solution of this equation, in whatever numbering Maple is using.evalf(solutions[1]); There's a good reason Maple doesn't give you a formula for the solutions of this polynomial: there is no general formula (not in terms of radicals, at least) for roots of a polynomial of degree 5 or more.Now let's try an equation not involving a polynomial.solve(sin(x)=1/2, x);By the way, remember, it's sin(x), not sin x or sin*x or sinx.That's one solution, but not the only one. But there's a way to get Maple to give us the others. To find out how, try the help system.?solvesolutions:= solve(sin(x)=1/2, x, AllSolutions);LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSRfQjFGJy8lJ2l0YWxpY0dRJXRydWVGJy8lLG1hdGh2YXJpYW50R1EnaXRhbGljRicvRjhRJ25vcm1hbEYnRitGOg== (or LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSRfQjJGJy8lJ2l0YWxpY0dRJXRydWVGJy8lLG1hdGh2YXJpYW50R1EnaXRhbGljRicvRjhRJ25vcm1hbEYnRitGOg== etc) is a variable that can take values 0 or 1 (B for binary). LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSRfWjFGJy8lJ2l0YWxpY0dRJXRydWVGJy8lLG1hdGh2YXJpYW50R1EnaXRhbGljRicvRjhRJ25vcm1hbEYnRitGOg== could be any integer (Z for the German Zahl). The ~ tells you that Maple has made an assumption on this variable. Maple uses names that start with the underscore _, because it wants to avoid using names that you're already using. about(_Z1);about(_B1);Maple needed to use something like this to express the solutions of the equation, because there are infinitely many of them. We say it's a parametric solution. To get a particular solution, we want to give values to the parameters LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSRfQjFGJy8lJ2l0YWxpY0dRJXRydWVGJy8lLG1hdGh2YXJpYW50R1EnaXRhbGljRicvRjhRJ25vcm1hbEYnRitGOg== and LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2I1EhRictRiM2JC1GLDYlUSRfWjFGJy8lJ2l0YWxpY0dRJXRydWVGJy8lLG1hdGh2YXJpYW50R1EnaXRhbGljRicvRjhRJ25vcm1hbEYnRitGOg==. We can do this using eval.eval(solutions, {_B1 = 1, _Z1 = 0});"eval" is short for "evaluate". What this does is evaluate the first item (solutions) after making the substitutions given by the second item (an equation or a set of equations). We can also use eval to check that this really is a solution, by plugging it back into the equation.eval(sin(x)=1/2, x=%);Here's a harder equation.eq := x * sin(x) = Pi/2;solve(eq,x,AllSolutions);Maple doesn't know the solutions, even though there are two that are easy to guess.evalf(%);Here's something rather cool. What number is this?identify(%);Maple objects introduced in this lessonplotplot3dplots[animate]?:=restart' '=issolveIRootOf
AllSolutions
about
identify