My piecewise function becomes NaN

In the function below my integral becomes -inf when b is nonzero.
I think because of that my ans yields NaN instead of 2. How do I solve this problem?
Nz2=@(a,b) ((b==0)*integral(myfun,itta,inf)+(a>0 & b>0)*2)
Ans=Nz2(a,b)

4 Comments

Hello. As I can see you want to evalute integral numerically with integrand function myfunc(arg) and limits [itta, inf], where itta - is some variable and inf - means infinity. If so, it's not correct because numerical integration implies finite number of operations (in your case matlab must show warning message: 'Reached the limit...').
Could you provide more information about the task?
Adam Danz
Adam Danz on 31 Aug 2019
Edited: Adam Danz on 31 Aug 2019
What are myFun, a, and b? I'm assuming your question is why you're getting -inf within the integral.
Hi, the following is my task,
i want to compute
output=v4*Nz2(t1n,t1d)
where
Nz2=@(a,b) ((a>0&b==0)*q+(a==0&b==0)+(a>0&b>0));
t1n=qfunc(some variable value that changes & depends on a variable x that I define)
t1d=qfunc(some variable value that changes & depends on a variable x that I define)
q = some finite interval when b=0 and it equals to -Inf when b is non zero
So when b is non zero i get the ouput as NaN which is wrong.
My output should be a finite value
See walter's answer to understand why you're getting a NaN and see his comment(s) under his answer.

Sign in to comment.

 Accepted Answer

You indicate that your integral becomes infinite when b is non-zero. You try to compensate for that by using (b==0) * integral() thinking that it will "select" the integral() calculation when b is 0 and thinking that otherwise it will skip it. But that is not what happens. When you calculate (b==0)*integral() then both sides are calculated no matter what the value of b is. When b is 0 then that is fine, as you get (0==0)*finite_value which is the finite_value. But when b is non-zero then you get (nonzero==0)*infinite_value which is 0*infinite_value which is NaN.
You cannot use the logical_condition*expression calculation form when the expression can be infinite or nan in any situation where the logical_condition is false.
To deal with this, you will either need to use the symbolic toolbox piecewise() function, or you will need to write a small function that uses if or logical indexing so that you do not calculate the integral in the b ~= 0 case.

3 Comments

If I use the piecewise function ,I get the following error :
Undefined function 'piecewise' for input arguments of type 'double'.
This is what i use the function as:
Nz2=piecewise(t1n(j)>0 & t1d(j)==0,integral(),t1n(j)>0 &t1d(j) > 0,1);
where t1n(j) and t1d(j) are a fixed value for a particular jth value in loop.
Please help.
Nz2=piecewise( sym(t1n(j))>0 & sym(t1d(j))==0,integral(), sym(t1n(j))>0 & sym(t1d(j)) > 0, 1, 0);
The extra 0 is needed for the cast where t1n(j) <= 0 or t1n(j) < 0
Thanks! It worked for my problem.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!