kill(all)$

simple optimization f(x,y) : 'fixed step,fx,fy,hessian,num', part 1c ,version 1.0
peter.vlasschaert@gmail.com, 21/03/2019

info :
====
see :website : special topics (numerical : pde)
simple optimization f(x,y) : 'fixed step,fx,fy', part 1a ,version 1.0a
simple optimization f(x,y) : 'fixed step,fx,fy,hessian', part 1b ,version 1.0a

numerical approximation : derivatives 'normal' and 'mixed' ( see :  numerical pde)
====================================================================

from youtube: learning maxima
-----------------------------------------------
https://www.youtube.com/watch?v=K_SKxoAcTOU&list=PLbrsenR2t6fxAjtC1Q9RKDMYArGpbJpgK&index=6
ref : Maxima CAS 5: Loading plugins and making plugins. *

forward difference * : first order derivative ( molecule )
=================

p1:f(x,y) := 4-x^2-y^2;

p1a:rhs(p1);

plot 3D   'f(x,y)'
-----------

p1b:wxplot3d([f(x,y), 4, [x, -4, 4], [y, -4, 4]])$

numerical approximation : first order derivative
========================================

∂f/∂x ,∂f/∂y  ?
===========

;dfx: (f(x+hs,y) -f(x,y))/hs;
 dfy:(f(x,y+hs) -f(x,y))/hs;

fxynumfd_max_or_min(hss,hh,xx0,yy0,ff,nn):= block(
[], h: hh, f1:ff,x0:xx0,y0:yy0,n:nn,hs:hss,
    fx:(f(x+hs,y) -f(x,y))/hs,fxx:subst([x=x0,y=y0],fx),
    fy:(f(x,y+hs) -f(x,y))/hs, fyy:subst([x=x0,y=y0],fy),    
            xx : x0+h*fxx,
            yy : y0 +h*fyy,    
      for i:1  thru n
             do(
    xx:xx+subst([x=xx,y=yy],fx)*h,
    yy:yy+subst([x=xx,y=yy],fy)*h
                         ),
    f11:subst([x=xx,y=yy],f1),
           [xx,yy,f11]   
)$

how to use: fxynumfd_max_or_min
==============================

code : max or min.
---------------------------
==========================
hss               = step value computing first derivatives x and y. (small value)
hh                 = step value
start_value = x0,y0  for search 
ff                    = f(x,y)
nn                  = number of steps
==========================

pd1:fxynumfd_max_or_min(0.1,0.12,0.5,-1.0,p1a,10);

rem :  'p1a',see above.

central difference * : first order derivative. ( molecule)
=================

numerical approximation : first order derivative
========================================

∂f/∂x ,∂f/∂y  ?
===========

cdfx: (f(x+hsc,y) -f(x-hsc,y))/(2*hsc);
cdfy:(f(x,y+hsc) -f(x,y-hsc))/(2*hsc);

fxynumcd_max_or_min(hss,hh,xx0,yy0,ff,nn):= block(
[], h: hh, f1:ff,x0:xx0,y0:yy0,n:nn,hsc:hss,
    fx:(f(x+hsc,y) -f(x-hsc,y))/(2*hsc),fxx:subst([x=x0,y=y0],fx),
    fy:(f(x,y+hsc) -f(x,y-hsc))/(2*hsc), fyy:subst([x=x0,y=y0],fy),    
            xx : x0+h*fxx,
            yy : y0 +h*fyy,    
      for i:1  thru n
             do(
    xx:xx+subst([x=xx,y=yy],fx)*h,
    yy:yy+subst([x=xx,y=yy],fy)*h
                         ),
    f11:subst([x=xx,y=yy],f1),
           [xx,yy,f11]   
)$

how to use: fxynumcd_max_or_min
==============================

code : max or min.
---------------------------
==========================
hss               = step value computing first derivatives x and y. (small value)
hh                 = step value
start_value = x0,y0  for search 
ff                    = f(x,y)
nn                  = number of steps
==========================

pd1:fxynumcd_max_or_min(0.1,0.12,0.5,-1.0,p1a,10);

rem :  'p1a',see above.

rem : central differences beter for the approximation than the forward differences
           with same parametric values.
========================================================================
see course : introduction to numerical analysis.( = taylor expansion gives the solution)
=========================================================================

rem : this source code can be used for programming , truebasic,c++,pacal,fortran.
code :fxynumfd_max_or_min(hss,hh,xx0,yy0,ff,nn)
            fxynumcd_max_or_min(hss,hh,xx0,yy0,ff,nn)           
             

final  result
=========

big number of steps ' = nn '. ( central difference) ,' 10 times more steps '.
============================================================

pd1:fxynumcd_max_or_min(0.1,0.12,0.5,-1.0,p1a,100);

maximum value for function  f(x,y). (why max : see part 1b , see above )
============================================================

p1a : fmax =pd1[3];

'--------------------------------------------------------------------------------------------------------------------------------'