14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 Programming Methods 225<br />

Additional Numeric Operators<br />

expressions for the derivatives. The expression is threaded by inserting assignments to temporary variables of<br />

expressions that are needed in several places for the derivatives.<br />

Here is an example involving an expression involving three variables. Listing all three variables returns the<br />

first derivatives with respect to each. The result is a list with the original expression and then the derivatives<br />

in the order requested. However, note here that JMP creates a temporary variable T#1 for storing the<br />

subexpression x^2, and then uses that subexpression subsequently to save calculations.<br />

result2=derivative(3*y*x^2+z^3, {x,y,z}); show(result2);<br />

result2 = {3 * y * (T#1 = x ^ 2) + z ^ 3, 6 * x * y, 3 * T#1, 3 * z ^ 2}<br />

To take second derivatives, specify the variable as a third argument. Both the second and third arguments<br />

must be lists. JMP returns a list with the original expression, the first derivative(s), and then the second<br />

derivative(s) in the order requested.<br />

second=derivative(3*y*x^2,{x},{x}); show(second);<br />

second = {3 * y * x ^ 2, 6 * x * y, 6 * y}<br />

second=derivative(3*y*x^2,{y},{y}); show(second);<br />

second = {3 * y * (T#1 = x ^ 2), 3 * T#1, 0}<br />

second=derivative(3*y*x^2,{y},{x}); show(second);<br />

second = {3 * y * (T#2 = x ^ 2), 3 * T#2, 6 * x}<br />

NumDeriv takes the first numeric derivative of an operator or function with respect to the value of the first<br />

argument by calculating the function at that value and at that value plus a small delta ( Δ ) and dividing the<br />

difference by the delta. NumDeriv2 computes the second numeric derivative in a similar fashion. These are<br />

used internally for nonlinear modeling but are not frequently useful in JSL. Note that these functions do<br />

not differentiate using a variable, but only with respect to arguments to a function. In order to differentiate<br />

with respect to x, you have to make x one of the immediate arguments, not a symbol buried deep into the<br />

expression.<br />

Suppose to differentiate y =3x 2 at the value of x =3. The incorrect way would be to submit<br />

x=3;<br />

n=NumDeriv(3*x^2);<br />

The correct way is to make x an argument in the function.<br />

x=3;<br />

f=function({x}, 3*x^2);<br />

n=NumDeriv(f(x), 1);<br />

Consider both the mathematical notation and the JSL equivalent for another example:<br />

For fx () x 2<br />

d 2 ( x + Δ) 2 – x 2<br />

d 2<br />

= , it calculates x = ------------------------------ . At x , .<br />

dx Δ<br />

0<br />

= 3 x = 6.00001<br />

dx<br />

x=3; y=numderiv(x^2); // or equivalently: y = numDeriv(3^2);<br />

6.0000099999513<br />

And here are a few more examples:<br />

x = numderiv(sqrt(7));

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

Saved successfully!

Ooh no, something went wrong!