odd and even functions
조회 수: 8 (최근 30일)
이전 댓글 표시
채택된 답변
Voss
2022년 5월 6일
One thing you can do is to plot the function and visually inspect it:
f = @(x)0.5*(sin(x)+abs(sin(x)));
fplot(f,[-pi pi])
It doesn't look even or odd.
You could also evaluate the function at some values of x and -x and compare the results:
x = -pi:0.1:pi;
is_even = isequal(f(x),f(-x))
is_odd = isequal(f(x),-f(-x))
(A sanity check with functions known to be odd or even)
f = @(x)sin(x);
is_even = isequal(f(x),f(-x))
is_odd = isequal(f(x),-f(-x))
f = @(x)cos(x);
is_even = isequal(f(x),f(-x))
is_odd = isequal(f(x),-f(-x))
추가 답변 (1개)
David Hill
2022년 5월 6일
x=-pi:.001:pi;
f=@(x).5*(sin(x)+abs(sin(x)));
if isequal(f(x),f(-x))
even_odd=2;%even
elseif isequal(-f(x),f(-x))
even_odd=1;%odd
else
even_odd=0;%neither
end
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!