Why am I not getting all the outputs mentioned in my function ?
Mostrar comentarios más antiguos
*The code for my function is given below*
As written in the function statement, I want to have all three of rs2,x,y in my output. However I am getting only rs2 and not x and y. I can not figure out why , please help.
function[rs2,x,y]= rwn(nt)
nt = 3;
fprintf('The number of jumps = %d',nt)
x(1)=0;% start particle at the origin
y(1)=0;% start particle at the origin
rs2(1)=0; % square displacement is 0 for first step
% create a list of random numbers ranging from 1 to 4, with nt entries
fd=ceil(4.*rand(nt,1))
% the next two lines define the jumps on the square lattice: right,up, left, down
delx = [1 0 -1 0];
dely = [0 1 0 -1];
% loop over the nt jumps, adding the jump vector choosing the random
%number from the list generated earlier
for j=1:nt % sum over nt jumps
x(j+1)=x(j)+delx(fd(j)); % x position at j+1 jump
y(j+1)=y(j)+dely(fd(j)); % y position at j+1 jump
rs2(j+1)=x(j+1)^2 + y(j+1)^2; % square displacement position at j+1 jump
end
end
Respuesta aceptada
Más respuestas (1)
James Tursa
el 5 de Sept. de 2015
Are you calling the function with three outputs? E.g.,
nt = 3;
[rs2,x,y] = rwn(nt);
1 comentario
Agnivo Gosai
el 5 de Sept. de 2015
Categorías
Más información sobre Multidimensional Arrays 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!