how to remove NaN from anon function?

I am using this simpsons rule, for computing (x)/sin(x), but i receive NaN, and i dont know how to change NaN to zero for anonfunctions. it works fine for functions where I don't run into terms like 0/0, so im guessing I essentially need to remove the very first term/replace it with 0.
if true
% code
function return_value = simpsons(f,a,b,n)
if numel(f)>1
n=numel(f)-1; h=(b-a)/n;
Area= abs(h/3*(f(1)+2*sum(f(3:2:end-2))+4*sum(f(2:2:end))+f(end)));
else
h=(b-a)/n; xi=a:h:b;
return_value=abs(h/3*(f(xi(1))+2*sum(f(xi(3:2:end-2)))+4*sum(f(xi(2:2:end)))+f(xi(end))));
end

답변 (1개)

Sara
Sara 2014년 4월 25일

0 개 추천

If x is the array in which you want to remove the NaN:
x(isnan(x)) = 0;

댓글 수: 2

Nabeel
Nabeel 2014년 4월 25일
thank you for the reply but this doesn't seem to work, the command only changes the final answer of the entire area/return_value to zero after it has been calculated as NaN
Sara
Sara 2014년 4월 26일
You mean your f is nan sometimes? Can you post your code? Makes it easier to help.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Elementary Math에 대해 자세히 알아보기

질문:

2014년 4월 25일

댓글:

2014년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by