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(adsbygoogle = window.adsbygoogle || []).push({});

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.Code (Text):

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

Need help.

Thanks

link

**Physics Forums - The Fusion of Science and Community**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Derivative function in matlab

Can you offer guidance or do you also need help?

Draft saved
Draft deleted

Loading...

Similar Threads - Derivative function matlab | Date |
---|---|

Mathematica Bessel function derivative in sum | Jul 27, 2017 |

[Mathematica] Bug in Integrate with derivatives of a delta function | Jul 17, 2014 |

Mathematica: A function that derives another function on all its arguments | Mar 18, 2012 |

How to define the derivative of a function as a new function in Maxima | Sep 4, 2011 |

Mathematica: take derivative in terms of original function | Jun 6, 2011 |

**Physics Forums - The Fusion of Science and Community**