next up previous
Next: About this document ... Up: lab_template Previous: lab_template


Assigning labels, expressions and functions


The purpose of this lab is to introduce you to the basics needed in any Maple lab. The main topics include: assigning labels, defining expressions, defining functions and plotting.

Assigning labels

Suppose you want to assign a constant value to a variable name. Assigning labels is done by choosing a variable name followed by := and then the value to be assigned to that variable. For example, to let myname be assigned the value 1.5, you can type:
> myname := 1.5;
Once you hit return after this Maple line, any time the string of characters myname is used, it will have the value of 1.5. Note the use of the colon in front of the equal sign. This is necessary and if you forget the colon, Maple will not give an error message. It will appear as if you correctly assigned the value 1.5 to the label myname, however when you try to use myname again in the worksheet, it will not have the value 1.5.
Reusing variable names within the same lab is not recommended. It can be done, however, Maple will only store in its memory the last value that you assigned to the variable. So, if you go back to work on a previous problem with the same variable and forget to re-execute the old value, you could get some incorrect answers. Even if you do it correctly, sometimes Maple keeps a value in memory even though you've deleted the Maple line and tried to re-assign it. The best way to re-use a variable is to "clear" its memory for that variable. You can try the next example to see how this works.
> p := 5;
> p;
> p := 'p';
> p;
Once you have cleared the variable, you are free to use it again. Another thing to keep in mind is that if you close your Maple worksheet for any reason and reopen it, none of the commands will be stored in memory until you re-execute each command by hitting return after each of the Maple lines. This can also be done by clicking on the Edit menu and choosing Execute worksheet, however, this could be time consuming if there are a lot of plots in the worksheet.

Entering an expression

Expressions such as $x^3+3x^2-x+1$ can be entered in a similar way to variable assignment. Choose a variable name to represent the expression and assign the expression to the variable as follows.
> expr := x^3+3*x^2-x+1;
Note that in the expression above, there is an asterisk between 3 and $x^2$. A common mistake is to write two functions next to each other without the "*" symbol. This would give incorrect results when using this expression since Maple doesn't understand implied multiplication. Another thing to keep in mind is when entering expressions in Maple that require the use of parentheses for grouping, the symbols ``(`` and ``)'' should be used since square brackets and curley brackets have special purposes in Maple.

Entering a function

Suppose you want to enter the same expression stored in expr above, but as a function of $x$. In Maple, you would type
> f := x-> x^3+3*x^2-x+1;
Below is how ``NOT'' to enter a function:
> f(x) := x^3+3*x^2-x+1;
The difference between expressions and functions are first the obvious, that expressions do not have to satisfy the definition of a function in the sense that for each input $x$, there is a unique value $f(x)$. A function may be defined as an expression, but not all expressions can be defined as functions. The differences in Maple are numerous as you will see below when we evaluate the expression or function for a given value as well as when using the plot command.

Evaluating functions and expressions

In order to evaluate an expression at a given value of $x$, you must use the subs command. For example, if we wanted to evaluate the expression expr$=x^3+3x^2-x+1$ at $x=2$ and at $\displaystyle x=\frac{1}{2}$, the examples below show how this can be done
> subs(x=2,expr);
> eval(expr,x=2);
> subs(x=2.,expr);
> subs(x=1/2,expr);
> subs(x=0.5,expr);
In the subs command, the first argument tells Maple what you would like to substitute in for $x$. The second argument tells Maple what expression you are substituting into. Note the difference in outputs when a whole number or fraction is entered compared to a decimal. In many cases, the subs command inserts values into the exptession but does not perform the evaluation in which case the eval command may be better for evaluating an expression. This can be seen in the example below.
> subs(theta=Pi,sin(theta)*cos(theta));
> eval(sin(theta)*cos(theta),theta=Pi);
In Maple, functions are much easier to evaluate than expressions. In order to evaluate the function $f(x)=x^3+3x^2-x+1$ at $x=2$, then simply type
> f(2);
Here are a few more examples of evaluating functions.
> f(a+h);
> f(Pi);
> evalf(f(Pi));
Note the use of the evalf command in the last example above. This command is used when we want Maple to output the answer in decimal form. If this command is not used, the output to your Maple commands will be calculated analytically, where as the evalf command forces Maple to calculate the answers numerically. The evalf command has one essential argument, however a second argument can be added in order to tell how many digits we want to be in the answer. The example below evaluates $\Pi$ using 10 digits, not 10 decimal places.
> evalf(Pi,10);


The following example shows how the Maple plot command is used to plot a function of one variable.
> f := x-> x^2;
> plot(f(x),x=-2..2);
The plot command above has two essential arguments. The first argument is the function(s) or expression(s) that you want to plot. The function or expression can be typed in before the plot command or you can simply type it in as the first argument. The second argument is the range of numbers for the $x$-axis. The plot command allows you to add additional optional arguments as well. For instance, you may want to also restrict the y-axis range or add a title to your plot.
> plot(x^2,x=-2..2,y=-5..5,title=`My First Plot`);
This particular command allows you to add arguments, but if you were to leave off one of the essential arguments, you will get an error message. You can also plot more than one function or expression on the same graph by enclosing them in curly braces ``{}'' and separating them by commas. For example, we can plot $f(x)=x^2-2$ and $g(x)= -x+2$ on the same graph.
> f := x-> x^2-2;
> g := x-> -x+2;
> plot({f(x),g(x)},x=-4..4);

Plotting Polar Curves

When you graph curves in polar coordinates, you are usually assuming that $r$ is a function of the angle $\theta$ and $\theta$ is the parameter that describes the curve. In Maple, simply add the specification coords=polar as the last argument in the plot command. Try the example below which represents the polar plot of a rose with 4 petals. Note that any variables can be used in the plot command, not necessarily $r$ and $\theta$.
> plot(sin(2*theta),theta=0..Pi,coords=polar);
> f:=x->sin(2*x);
> plot(f(x),x=0..Pi,coords=polar);


  1. Enter $2-3\sin(x)$ as an expression.
    1. Evaluate this expression at $x=a+h$ using the subs command.
    2. Compare the outputs of evaluating this expression at $x=0$ using the subs and the eval command.
    3. Evaluate the expression at $\displaystyle x=\frac{\pi}{4}$ both analytically and numerically using the eval command.
    4. Plot this expression over the $x$ range $0 \leq x \leq 2\pi$ in rectangular coordinates and then again in polar coordinates. Identify the name of the polar curve in text.

  2. Define $\displaystyle f(x)= \frac{x\ln(x^2+1)}{x^4+1}$ as a function.
    1. Evaluate $f(x)$ at $x=0$ and $x=1$. Express answers in analytic form.
    2. Evaluate $f(x)$ at $x=\sqrt{2}$ and express answer in decimal form.
    3. Plot $f(x)$ over the interval $-5 \leq x \leq 5$.

  3. Define $1-\sin(x)$ and $2+\cos(x)$ as functions. Plot them both on the same graph in polar coordinates over the interval $0 \leq x \leq 2\pi$. Estimate the two intersection points by observing the rectangular plot. Plug each $x$ value back into both functions to show that the $y$ values are the same. State the intersection points, each as a polar point, in text.

next up previous
Next: About this document ... Up: lab_template Previous: lab_template
Dina J. Solitro-Rassias