Creating arrayfun with two variables
Mostrar comentarios más antiguos
Hello.
I'd like to create cell array with results of a function S for variables q and k, which is defined in a code below.
function z=comparison_sum_integral_trapz
tic
format long
tt=-0.000689609;t=0.242731; muu=0.365908;
[m,NN]=meshgrid(0:100,-500:1:500);
y1= @(N,q,k) t*q./k.*log((-k.^2+2*k.*q-q.^2+muu+1i*(2*pi*N.*t-(2*m(1,:)+1)*pi*t))./(-k.^2-2*k.*q-...
q.^2+muu+1i*(2*pi*N.*t-(2*m(1,:)+1)*pi*t)))./(tt*pi+integral(@(a)a.*tanh((a.^2-muu)./(2*t)).*log((2*a.^2+2*a.*q+...
q.^2-2*muu-1i*2*pi*N*t)./(2*a.^2-2*a.*q+q.^2-2*muu-1i*2*pi*N*t))./q-2,0,10000,'AbsTol',1e-6,'RelTol',1e-3,'ArrayValued',true));
R1=@(q,k) integral(@(N)y1(N,q,k),500,10^6,'AbsTol',1e-6,'RelTol',1e-3,'ArrayValued',true);
R11=@(q,k) integral(@(N)y1(N,q,k),-10^6,-500,'AbsTol',1e-6,'RelTol',1e-3,'ArrayValued',true);
y2=@(q,k) t*q./k.*log((-k.^2+2*k.*q-q.^2+muu+1i*(2*pi*NN(:,1).*t-(2*m(1,:)+1)*pi*t))./(-k.^2-2*k.*q-...
q.^2+muu+1i*(2*pi*NN(:,1).*t-(2*m(1,:)+1)*pi*t)))./(tt*pi+integral(@(a)a.*tanh((a.^2-muu)./(2*t)).*log((2*a.^2+2*a.*q+...
q.^2-2*muu-1i*2*pi*NN(:,1).*t)./(2*a.^2-2*a.*q+q.^2-2*muu-1i*2*pi*NN(:,1).*t))./q-2,0,10000,'AbsTol',1e-6,'RelTol',1e-3,'ArrayValued',true));
R2=@(q,k) sum(y2(q,k));
S=@(q,k) R2(q,k)+R11(q,k)+R1(q,k)-4*sqrt(2)/pi*(1/1000)/(pi^(3/2)*sqrt(t))*q.^2;
q=0.001:1:7;
k=0.001:1:7;
out=arrayfun(S,k,q,'UniformOutput',false)
end
My problem is that I expected to get a cell array 7×7 but instead of I obtained 1×7.
out =
1×7 cell array
Columns 1 through 5
[1×101 double] [1×101 double] [1×101 double] [1×101 double] [1×101 double]
Columns 6 through 7
[1×101 double] [1×101 double]
Elapsed time is 3.165576 seconds.
I'd like to avoid an expoitation of
meshgrid
due to long time calculations (on the next step I will calculate the cell array out for larger arrays of q and k ).
What I did it wrong?
I will apreciate for any suggestions.
2 comentarios
Rik
el 17 de Nov. de 2018
Your S function returns an imaginary array of 101 elements. Run the code below to see what happens for a scalar input. Your code doesn't have any comments, so I don't understand what it is doing, so I can't determine what to fix.
temp=S(q(1),k(1));
x=real(temp);y=imag(temp);
plot(x,y)
Yuriy Yerin
el 17 de Nov. de 2018
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Numerical Integration and Differentiation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!