Is There a Limit to Foolproofing Code When Using Derivative Functions in MATLAB?

The function is supposed to calculate the derivative of a given function f(x) at a specific point (x,f(x)). The code includes a tolerance value and a loop to iterate through different values of h until the error is within the given tolerance. However, if the tolerance is set too low, the calculated derivative may be incorrect. The user should take responsibility for choosing an appropriate tolerance value.
  • #1
Link-
100
0
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
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
 
Physics news on Phys.org
  • #2
There is a limit to foolproofing code. A user should be responsible for misuse of a function.

In this particular case, a check could be done on the value of error. If error = 0, it could be due to a bad choice of h.
 
  • Like
Likes Greg Bernhardt

FAQ: Is There a Limit to Foolproofing Code When Using Derivative Functions in MATLAB?

What is a derivative function in Matlab?

A derivative function in Matlab is a mathematical tool that calculates the rate of change of a function at a given point. It can be used to find the slope of a curve and to determine the maximum and minimum points of a function.

How do I define a derivative function in Matlab?

To define a derivative function in Matlab, you need to use the "diff" function. This function takes two arguments - the function you want to differentiate and the variable with respect to which you want to differentiate. For example, "diff(f(x),x)" will return the derivative of the function f(x) with respect to x.

Can I plot a derivative function in Matlab?

Yes, you can plot a derivative function in Matlab. You can use the "ezplot" function to plot the original function and its derivative on the same graph. This can help visualize the relationship between the two functions and identify important points such as maxima and minima.

How can I use a derivative function to solve problems?

A derivative function can be used to solve a variety of problems in mathematics, physics, and engineering. It can be used to find the rate of change in a system, optimize functions, and solve differential equations. By calculating the derivative of a function, you can gain valuable insights into the behavior of a system and make predictions about its future behavior.

Are there any limitations to using derivative functions in Matlab?

Like any mathematical tool, there are limitations to using derivative functions in Matlab. One limitation is that it can only approximate the derivative of a function at a given point, which may not always be accurate. Additionally, the accuracy of the derivative function may be affected by the complexity of the original function and the chosen step size. It is important to carefully choose the function and parameters to ensure the accuracy of the results.

Similar threads

Replies
2
Views
3K
Replies
3
Views
1K
Replies
8
Views
2K
Replies
9
Views
2K
Replies
1
Views
1K
Replies
2
Views
3K
Back
Top