To determine if a function is odd, even, or neither, you can implement a MATLAB function that tests the symmetry properties of the function. The function ( f(x) ) is:
- Even if ( f(x) = f(-x) ) for all ( x ).
- Odd if ( f(x) = -f(-x) ) for all ( x ).
Here's a MATLAB function that takes a function handle as input and determines whether it is even, odd, or neither:
function result = checkFunctionSymmetry(func, xRange)
% Check if a function is odd, even, or neither
% func: function handle, e.g., @(x) x.^2
% xRange: vector specifying the range of x values to test, e.g., linspace(-10, 10, 1000)
% Evaluate the function at x and -x
xValues = xRange;
f_x = func(xValues);
f_neg_x = func(-xValues);
% Check for even symmetry
if all(abs(f_x - f_neg_x) < 1e-10) % Tolerance for numerical precision
result = 'Even';
% Check for odd symmetry
elseif all(abs(f_x + f_neg_x) < 1e-10)
result = 'Odd';
else
result = 'Neither';
end
end