Procedures and Programming
Back to Portal
Introduction
Maple lets you write programs (also known as procedures) to automate your work, batch process data files, and more.
Unlike low-level languages, you can use high level math tools in Maple procedures. This procedure, for example, calculates the friction factor for liquid flow in a pipe; the fsolve function is used to numerically solve an equation
friction≔proce,Dia,Rey if not typeRey,numeric or not typee,numeric then 'frictionRey,e': else if Rey>2300 then fsolve 1f=−2⋅log10e3.7⋅Dia+2.51Rey f: else 64Rey end if: end if:end proc:
friction0.0001,0.1,5000
0.03849535900
Read the Programming Guide to learn how to write procedures
Code Generation
You can also convert Maple procedures to other languages. Simply use Maple's interactive development environment to develop, optimize and validate your code, and then generate code for your target language.
These two procedures, for example, replaces the use of fsolve in the procedure above with a bisection method. The procedures are then converted to Python.
friction ≔ proc f, e, Dia, Rey return −2 ⋅ log10e3.7 ⋅ Dia+2.51Rey ⋅ f−1f:end proc:
bisectionColebrook ≔ proc friction, a, b, e, Dia, Rey local epsilonABS, epsilonSTEP, c, atemp, btemp: epsilonABS ≔ 0.1ⅇ-4: epsilonSTEP ≔ 0.1ⅇ-4: atemp ≔ a: btemp ≔ b: while epsilonSTEP ≤ btemp−atemp or epsilonABS ≤ absfrictionatemp, e, Dia, Rey and epsilonABS ≤ absfrictionbtemp, e, Dia, Rey do c ≔ atemp2+ btemp2: if absfrictionc, e, Dia, Rey ≤ 0 then break elif frictionatemp, e, Dia, Rey⋅frictionc, e, Dia, Rey < 0 then btemp ≔ c else atemp ≔ c end if end do: return atemp:end proc:
bisectionColebrookfriction,0.0001,0.1,0.0001,0.1,5000
0.03848930665
Now generate Python code for the two procedures
CodeGenerationPythonbisectionColebrook
def bisectionColebrook ( friction, a, b, e, Dia, Rey): epsilonABS = 0.1e-4 epsilonSTEP = 0.1e-4 atemp = a btemp = b while (epsilonSTEP <= btemp - atemp or epsilonABS <= abs(friction(atemp, e, Dia, Rey)) and epsilonABS <= abs(friction(btemp, e, Dia, Rey))): c = atemp / 2 + btemp / 2 if abs(friction(c, e, Dia, Rey)) <= 0: break elif friction(atemp, e, Dia, Rey) * friction(c, e, Dia, Rey) < 0: btemp = c else: atemp = c return(atemp)
CodeGenerationPythonfriction
import math
def friction ( f, e, Dia, Rey): return(-2 * math.log10(e / 0.37e1 / Dia + 0.251e1 / (Rey * math.sqrt(f))) - 0.1e1 / math.sqrt(f))
Code Edit Regions
Code Edit Regions let you write your procedures using a traditional text input interface that features several IDE-like features, such as
syntax highlighting
bracket matching
command completion
automatic text wrapping
optional line number display
and automatic indenting.
Right-click on the following Code Edit region and select "Collapse Code Edit Region" to collapse it
Applications
Code Generation for a Robot Arm
Maximize the Efficiency of a Rankine Cycle
Organic Rankine Cycle
Download Help Document