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

3 views (last 30 days)
*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

Accepted Answer

Star Strider
Star Strider on 5 Sep 2015
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.

More Answers (1)

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

Categories

Find more on Elementary Math in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!