Register to reply

Derivative function in matlab

by Link-
Tags: derivative, function, matlab
Share this thread:
Link-
#1
May1-08, 11:26 PM
P: 98
I wrote a user-defined function in matlab that is supposed to take the derivative of a function f(x) in the point that correspond to (x,f(x)). Here's the code
function [fprime]=numderivative(f,x,h,tol)

hd=h*10^-1;
hh=h;
hhdd=hd;
itnum=numel(x);
error=tol+1;
for ii=1:itnum
    while error>tol
        fprime1=(f(x(ii)+h)-f(x(ii)))/h;
        fprime2=(f(x(ii)+hd)-f(x(ii)))/hd;
        error=abs(fprime1-fprime2);
        h=h*10^-1;
        hd=h*10^-1;
    end
    fprime(ii)=fprime1;
    h=hh;
    hd=hhdd;
    error=tol+1;
end
Obviously if you insert a real small tolerance (f(x(ii)+h)-f(x(ii))) would be zero even if the derivative is not zero. I'm trying to come up with something but I just don't know what to do.
Need help.

Thanks
link
Phys.Org News Partner Science news on Phys.org
What lit up the universe?
Sheepdogs use just two simple rules to round up large herds of sheep
Animals first flex their muscles

Register to reply

Related Discussions
Matlab Derivative Approximation Math & Science Software 1
Matlab function Math & Science Software 2
Matlab mex-function Math & Science Software 0
Matlab function HW help please Math & Science Software 1
Matlab Second Derivative Ploting Problem Math & Science Software 3