MATLAB: How can i change x to (x+h)

replacing

This is my function, f(x)=x^2 – 4 and f(x+h)=(x+h)^2 – 4
f=@(x) x.^2- 4;
f=@(x+h) x.^2- 4 (this gives me error)
How can i write f(x+h) ?

Best Answer

  • I am not certain what problem you are having. You have to define all the values you pass to your function as arguments before calling your function.
    Example —
    f = @(x) x.^2- 4;
    h = 1E-8;
    dfdx = @(f,x,h) (f(x+h) - f(x))./h;
    x = 4;
    fval = f(x)
    derv = dfdx(f,x,h)