381 - A13 - Derivatives

398 days ago by Professor381

function y = f(x) y = sqrt(x); end function y = fp(x) y = 1/(2*sqrt(x)); end function y = fpp(x) y = -1/(4*sqrt(x)^3) end x0 = 2 delx = 0.1 # let's just always make delx > 0 format long exact1st = fp(x0) exact2nd = fpp(x0) 
       
x0 =  2
delx =    1.000000000000000e-01
exact1st =    3.535533905932737e-01
y =   -8.838834764831842e-02
exact2nd =   -8.838834764831842e-02
x0 =  2
delx =    1.000000000000000e-01
exact1st =    3.535533905932737e-01
y =   -8.838834764831842e-02
exact2nd =   -8.838834764831842e-02
# 2-pt formulas fdiff = (f(x0+delx)-f(x0))/delx bdiff = (f(x0-delx)-f(x0))/(-delx) 
       
fdiff =    3.531125502687305e-01
bdiff =    3.539964406506613e-01
fdiff =    3.531125502687305e-01
bdiff =    3.539964406506613e-01
# 3-pt formulas cdiff = (f(x0+delx) - f(x0-delx))/(2*delx) enddiff = (-3*f(x0)+4*f(x0+delx)-f(x0+2*delx))/(2*delx) 
       
cdiff =    3.535544954596959e-01
enddiff =    3.535512014327336e-01
cdiff =    3.535544954596959e-01
enddiff =    3.535512014327336e-01
# 5-pt formula fivediff = (f(x0-2*delx)-8*f(x0-delx)+8*f(x0+delx)-f(x0+2*delx))/(12*delx) 
       
fivediff =    3.535533905449195e-01
fivediff =    3.535533905449195e-01
# second derivative centered difference seconddiff = (f(x0-delx)-2*f(x0)+f(x0+delx))/delx^2 
       
seconddiff =   -8.845749182242456e-02
seconddiff =   -8.845749182242456e-02