function that takes function handles f, g, h, that respectively calulates f, f prime and fdouble prime in matlab

main.m
function [f_val, f_prime, f_double_prime] = calc_derivatives(f, g, h, x)
% Calculates the value, first derivative and second derivative of a function 
% given function handles for the function, its first derivative and second derivative
% Input:
%   f: function handle for f(x)
%   g: function handle for f'(x)
%   h: function handle for f''(x)
%   x: value at which to estimate the derivatives
% Output:
%   f_val: value of f(x)
%   f_prime: value of f'(x)
%   f_double_prime: value of f''(x)

% Evaluate the function
f_val = f(x);

% Evaluate the first derivative
f_prime = g(x);

% Evaluate the second derivative
f_double_prime = h(x);
end
643 chars
23 lines

The above code defines a calc_derivatives function that takes in the handles for the given function, f, its first derivative (f'), and second derivative (f'') as well as the value x, at which the derivatives are to be evaluated.

The function computes the values of the discussed derivatives and returns these values as three separate outputs: f_val, f_prime, and f_double_prime.

gistlibby LogSnag