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

조회 수: 5 (최근 30일)
Yuriy Yerin
Yuriy Yerin 2018년 6월 18일
댓글: Steven Lord 2018년 6월 19일
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개)

Walter Roberson
Walter Roberson 2018년 6월 18일
R=@(q) sum(fun2(q));
  댓글 수: 5
Guillaume
Guillaume 2018년 6월 19일
fun2 = @(q) k*w*gamma0(q)./q.^3;
possibly, assuming gamma0 is supposed to use q as input.
Steven Lord
Steven Lord 2018년 6월 19일
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

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

카테고리

Help CenterFile Exchange에서 Matrix Computations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by