Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

In Maple, how do I plot a procedure containing an if statement?

Using Maple, consider the following example to plot a procedure with an if statement:

> f:=proc(x) > if x<0 then 0 elif x<1 then x elif x<2 then 2-x else 0 fi; > end; f := proc(x) if x < 0 then 0 elif x < 1 then x elif x < 2 then 2-x else 0 fi end

When you try to plot f(x), Maple returns an error message. The problem is due to Maple's order of evaluation. Suppose you invoke the following command:

> plot(f(x),x=0..2); Error, (in f) cannot determine if this expression is true or false :x<0

Maple first tries to calculate the value of f(x) before substituting the numeric values in for x. In the procedure, Maple cannot evaluate the if statement, as it cannot compare the size of x to 0; thus, it returns the error message.

A simple solution is to use single quotes to delay evaluation until a numerical value has been passed to x:

> plot('f(x)',x=0..2); 1 + AAA + AA AA + AA AA + A A 0.8 AAA AAA + A AA + AA AA + AA AA 0.6 AAA AAA + A AA + AA AA + AA AA + AA AA 0.4 AAA AAA + AA AA + AA AA + AA AA 0.2 AA AA + AA AA + AA AA +AA AAA **--+--+---+---+--+---+---+---+--+---+---+--+---+---+---+--+---+---+--+--** 0 0 0.5 1 1.5 2

A more elegant solution is to plot the function f instead of the formula f(x), for example:

> plot(f, 0..2); 1 + AAA + AA AA + AA AA + A A 0.8 AAA AAA + A AA + AA AA + AA AA 0.6 AAA AAA + A AA + AA AA + AA AA + AA AA 0.4 AAA AAA + AA AA + AA AA + AA AA 0.2 AA AA + AA AA + AA AA +AA AAA **--+--+---+---+--+---+---+---+--+---+---+--+---+---+---+--+---+---+--+--** 0 0 0.5 1 1.5 2

For more about Maple, see Maple at IU.

For more about statistical and mathematical software, email the UITS Stat/Math Center, visit the center's web page, or phone 812-855-4724 (IUB) or 317-278-4740 (IUPUI). The center is located in Bloomington at 410 N. Park Avenue, and is open for consultation by appointment Monday-Friday 9am-5pm.

This is document affi in domain all.
Last modified on March 18, 2011.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.