Error using sum Invalid data type. First argument must be numeric or logical.

Hi everybody.
I try to make integration over the variable q for the expression with inside integration and summation and find the answer in the form of the numerical value. Here my simple code
function z=self_energy_summation
k=1;
w=10;
z1=load('test.txt');
tt=z1(741,1);
T_c=z1(741,2);
mu=z1(741,3);
N=1:10;
nn=10^4;
fun1=@(a,q) a.*tanh((a.^2-mu)./(2*T_c)).*log((2*a.^2+2*a.*q+q.^2-2*mu-1i*2*pi.*N(1:10).*T_c)./(2*a.^2-2*a.*q+q.^2-2*mu-1i*2*pi.*N(1:10).*T_c))./q-2;
Gamma0=@(q) tt+integral(@(a)fun1(a,q),0,nn,'ArrayValued',true);
fun2=@(q) k*w*Gamma0./q.^3;
R=@(q) sum(fun2);
y=integral(@(q) R(q),0.001,nn,'ArrayValued',true)
end
Before the last integration I get the error
"Error using sum Invalid data type. First argument must be numeric or logical."
If I execute my script without last integration then the program works correctly.
Could you help me?

回答(1 个)

R=@(q) sum(fun2(q));

5 个评论

Thank you. But after this correction I still have the same error.
But after this correction I still have the same error
Very doubtful. You may get an error but it shouldn't be the exact same error. What is the error you now get?
I apologize. I provided incorrect information. Anyway I have an error
Undefined operator '*' for input arguments of type 'function_handle'.
Error in self_energy_summation>@(q)k*w*Gamma0./q.^3 (line 12) fun2=@(q) k*w*Gamma0./q.^3;
Error in self_energy_summation>@(q)sum(fun2(q)) (line 13) R=@(q) sum(fun2(q));
Error in self_energy_summation>@(q)R(q) (line 14) y=integral(@(q) R(q),0.001,nn,'ArrayValued',true)
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 self_energy_summation_test (line 14) y=integral(@(q) R(q),0.001,nn,'ArrayValued',true)
fun2 = @(q) k*w*gamma0(q)./q.^3;
possibly, assuming gamma0 is supposed to use q as input.
You cannot perform arithmetic operations on function handles, nor can you sum them. Instead perform arithmetic or call sum on the values returned by evaluating the function handle.
f = @(x) x.^2;
g1 = 2*f(1:10) % works
g2 = 2*f % does not work

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by