Why am I not getting all the outputs mentioned in my function ?

3 次查看(过去 30 天)
*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

采纳的回答

Star Strider
Star Strider 2015-9-5
Call your function in your calling script as:
[rs2,x,y]= rwn(nt);
If you only call it with one return variable, only the first is returned by default. If you call it with two, the first two, and with three, all three.

更多回答(1 个)

James Tursa
James Tursa 2015-9-5
Are you calling the function with three outputs? E.g.,
nt = 3;
[rs2,x,y] = rwn(nt);

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by