Saturday, August 24, 2024

Casio fx-CG50 and Python: Directional Derivative for f(x,y)

Casio fx-CG50 and Python: Directional Derivative for f(x,y)


Introduction


Problem:

Given the following:

* A function of two variables, f(x, y)

* An evaluation point, (x0, y0)

* A directional vector v = < v1, v2 >


Find the directional derivative at the point (x0, y0).


Steps:

1. Determine the gradient of f, ∇f. (∇ is the nabla symbol.) The gradient is a vector of partial derivatives, ∇f = < ∂f / ∂x, ∂f / ∂y >.

2. Evaluate the gradient at the point (x0, y0).

3. Determine the unit vector of the directional vector. Let the norm of | v | = √(v1^2 + v2^2), and the unit vector is determined as u = < v1 / | v |, v2 / | v | > = < u1, u2 >.

4. Take the dot product of the gradient and unit vector: ∇f ⋅ u


The Casio basic code allows for more than one evaluation point.



Casio fx-CG 50 Program: DIRDERIV


Program DIRDERIV

304 bytes


“DIRECTION DERIV.”

Rad

1×10^-4 → H

“NO QUOTES NEEDED”

“F(X,Y)”? → fn1

“DIR VECTOR”? → Vct V

UnitV(Vct V) → Vct U


Lbl 1

“X0”? → R

“Y0”? → S

R + H → X

S → Y

fn1 → A

R – H → X

(A – fn1) ÷ (2 × H) → A

R → X

S + H → Y

(B – fn1) ÷ (2 × H) → B

DotP( [ [ A, B ] ] , Vct U) → D

“[ D _| DX, D _| DIR ] = “ ◢

[ [ A, B, D ] ]

Menu “AGAIN?”, “YES”, 1, “NO” , 0

Lbl 0

“THANK YOU.”



Function memory variables is found through the [ OPTN ] menu. In Run-Matrix Mode, function memory can only be accessed when the Input/Output setup is set at Linear.


Get the _|, the fraction category, by pressing the fraction key, [ []/[] ].



Python Script: dirderv.py


Programmed on the Casio fx-CG50. Only module that is used is the math module. The function f(x,y) is defined in the subroutine in the script.


Code:


from math import *


# define f(x,y) here

def f(x,y):

  return x*sin(y) (or any function you want)


h = 0.0001

print(“Directional Deriv.”)

print(“Directional Vector: “)

vx = eval(input(“vx? “))

vy = eval(input(“vy? “))

n = sqrt(vx**2+vy**2)

ux = vx/n

uy = vy/n


print(“Point (x0,y0)”)

x0 = eval(input(“x0? “))

y0 = eval(input(“y0? “))


a = (f(x0+h,y0) – f(x0-h,y0))/(2 * h)

b = (f(x0,y0+h) – f(x0,y0-h))/(2 * h)

d = a*ux + b*uy


print(“dx=”,a)

print(“dy=”,b)

print(“dir.=”,d)


Examples


Example 1:

f(x,y) = x^3 + y^2

at (x0, y0) = (3, 2)

Directional Vector = < 1, 1 >



Norm: √(1^2 + 1^2) = √2

The unit vector: < 1/√2, 1/√2 >


Partial Derivatives:

∂f / ∂x = 3 * x^2, Value = 1

∂f / ∂y = 2 * y, Value = 1


Directional Derivative:

∇f * u ≈ 21.9203122


Example 2:

f(x,y) = x * sin y

at (x0, y0) = (0.5, -0.2)

Directional Vector = < -π, 0 >



Norm: √((-π)^2 + 0^2) = π

The unit vector: < -1, 0 >


Partial Derivatives:

∂f / ∂x = sin(y), Value ≈ -0.1986693308

∂f / ∂y = x * cos(y), Value ≈ 0.4900332881


Directional Derivative:

∇f * u ≈ 0.1986693308


Source


Dawkins, Paul “Section 13.7: Directional Derivatives”. Paul’s Online Notes. Last Modified November 16, 2022. Accessed July 4, 2024. https://tutorial.math.lamar.edu/classes/calciii/directionalderiv.aspx



Eddie


All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.