Identify and odd or even function

8 views (last 30 days)
Cesar Cango
Cesar Cango on 19 Apr 2016
Answered: BhaTTa on 9 Sep 2024
I've been trying to add to my program, a part that can identify if a function is odd or even, not a number, a function.

Answers (1)

BhaTTa
BhaTTa on 9 Sep 2024
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

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!