11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6.7 Generating Grids of Points • 267the ribbonplot procedure from section 6.2. Thus, for simplicity of presentation,this section avoids this particular case.The goal is to compute an array of values for f at each point on am × n rectangular grid. That is, at the locationsx i = a + (i − 1)δ x and y j = c + (j − 1)δ ywhere δ x = (b − a)/(m − 1) and δ y = (d − c)/(n − 1). Here i and j varyfrom 1 to m and 1 to n, respectively.Consider the function f: (x, y) ↦→ 1/ sin(xy). You need to evaluate fover the m × n grid with the ranges a, . . . , b and c, . . . , d.> f := (x,y) -> 1 / sin(x*y);f := (x, y) → 1sin(x y)Example 1 The first step is to convert the function f to a numericprocedure. Since Maple requires numeric values (rather than symbolic)for plots, create a procedure to convert f to a form that returns numericalanswers or the special value undefined.> fnum := convert( f , numericproc );fnum := proc(_X , _Y )local err;err := traperror(evalhf(f(_X , _Y ))) ;if type([err], [numeric]) then errelseerr := traperror(evalf(f(_X , _Y ))) ;if type([err], [numeric]) then err else undefined end ifend ifend procThe fnum procedure, which is the result of this conversion, attempts tocalculate the numerical values as efficiently as possible. Hardware floatingpointarithmetic, although of limited precision, is more efficient than softwarefloating-point and is frequently sufficient for plotting. Thus, the fnumprocedure uses the evalhf function first. If evalhf is successful, it returnsa numeric result; otherwise, it generates an error message. If thishappens, the fnum procedure attempts the calculation again by using softwarefloating-point arithmetic and calling evalf. Even this calculation is

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!