Undefined unary operator '-' for input arguments of type 'function_handle'.

1 view (last 30 days)
Hi everyone,
I am having some problems in using the integral functions, is there any suggestion for improving this code and fix the error?
Also, I tried to implement integral2 with limits which depend on other variables but it seems not possible...
Thank you in advance!
r_XR = @(theta, y) Ru(i1)*cos(theta) + sqrt(y.^2 - Ru(i1)^2 * (sin(theta)).^2 ) ;
thetaR = @(y) asin(y/Ru(i1)); % Ru(i1) is a constant
thetaR2 = @(y) -1*asin(y/Ru(i1));
r_mR = @(theta, y) Ru(i1)*cos(theta) - sqrt(y.^2 - Ru(i1)^2 * (sin(theta)).^2 ) ;
integrand_L = @(theta, y, z) z .* exp(-z.^2/(2*var)) ./ (1 + 1/tau * (z./ y).^alpha) ;
int_1La = @(theta,y) integral(@(z) integrand_L(theta,y,z), r_mR(theta, y), r_XR(theta, y), 'ArrayValued',true) ; % first integral
La = @(y) exp(lambda/(2*pi*var) * (integral(@(theta) int_1La(theta,y), thetaR2(y), thetaR(y), 'ArrayValued',true) - integral2(@(theta, z) integrand_L(theta,y,z), -pi, pi, 0, inf))) ;
I2a = @(theta, y) integral(@(r) lambda_r, r_mR(thetaR(y), y), r_XR(thetaR(y), y), 'ArrayValued',true) ;
integrand1_I1a = @(theta, y) lambda/(2*pi*var) * ( r_XR .* exp(-r_XR.^2/(2*var)) + r_mR .* exp(-r_mR.^2 /(2*var))) .* y./sqrt(y.^2 - (Ru(i1) * sin(theta)).^2) ;
I1a = @(y) 2.*I2a(thetaR, y) ./ sqrt(Ru(i1)^2 - y.^2) + integral(@(theta) integrand_I1a, -thetaR, thetaR, 'ArrayValued',true) ;
PDFa =@(y) exp(-integral(@(theta) I2a, -thetaR, thetaR, 'ArrayValued',true)) .* I1a(y) ;
integrand_Pca = @(y) exp(-tau/p*y.^alpha*N0/2).*La(y).*PDFa(y) ;
Pca = integral(integrand_Pca, 0, Ru(i1),'ArrayValued',true) ;
Undefined unary operator '-' for input arguments of type 'function_handle'.
Error in SimulationAnalysis_INH_CDFofX>@(y)exp(-integral(@(theta)I2a,-thetaR,thetaR,'ArrayValued',true)).*I1a(y)
Error in SimulationAnalysis_INH_CDFofX>@(y)exp(-tau/p*y.^alpha*N0/2).*La(y).*PDFa(y)
Error in integralCalc/iterateArrayValued (line 156)
fxj = FUN(t(1)).*w(1);
Error in integralCalc/vadapt (line 130)
[q,errbnd] = iterateArrayValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in SimulationAnalysis_INH_CDFofX (line 105)
Pca = integral(integrand_Pca, 0, Ru(i1),'ArrayValued',true) ;

Accepted Answer

Steven Lord
Steven Lord on 26 Feb 2020
@(y)exp(-integral(@(theta)I2a,-thetaR,thetaR,'ArrayValued',true)).*I1a(y)
theta_R is a function handle. If you want your lower limit to be -1 times the value that function handle returns, you can't simply write -thetaR. You need to create another function handle that evaluates thetaR and multiplies the returned value by -1. Don't worry, it's simpler to write than it is to describe in text.
minusThetaR = @(x) -thetaR(x); % eqn 1
When you evaluate minusThetaR it evaluates thetaR, passing through whatever input minusThetaR received into thetaR. Whatever comes out of that thetaR call is negated and returned as the output of minusThetaR.
Of course you don't need to define a separate variable for that if you don't want. You could make it truly anonymous:
@(y)exp(-integral(@(theta)I2a,@(x) -thetaR(x),thetaR,'ArrayValued',true)).*I1a(y) % eqn 2
At a quick glance, this part of your code also looks suspicious:
@(theta)I2a
I2a is a function handle. When evaluated, this will ignore whatever was passed into it and simply return the function handle. That's not what integral expects or needs. From the way it was defined above you want something more like:
@(theta) I2a(theta, y) % eqn 3
Whatever called the anonymous function shown in eqn 2 above will pass y into it. integral will call eqn 3 with appropriate values of theta, and because it was defined in a context where y was known it "picks up" the value of y.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!