Identify and odd or even function

14 vues (au cours des 30 derniers jours)
Cesar Cango
Cesar Cango le 19 Avr 2016
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.

Réponses (1)

BhaTTa
BhaTTa le 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

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by