# My loop does not produce the results I want, due to coding errors

조회 수: 2(최근 30일)
댓글: Adil Saeed 2022년 8월 5일
I have this code I am developing a random walks model for a mammal and analysing a receive level heard from this mammal, however when producing the loop code for several positions it does not show exactly what I need, as the for loop in my eyes is not completing.
A quick run throught the code:
clc;
clear;
x0 = 0;
y0 = 0;
Sr = [x0 y0]; %Source coordinates
a = 0.25; %Attenuation - FIND SOME VALUES
IL = 180; %Impact Level
v = 2; %Speed
t = 5; %seconds
T = 5400; %Total Time
s = v*t; %metres
iter = T/t; %number of positions
Tv = 0:t:(T-t);
SL = 212 + 10*log10(10);
X = zeros(iter,1);
Y = zeros(iter,1);
RL = zeros(iter,1);
Pre set parameters, although the X, Y and RL may be wrong, here I am trying to store values in these matrices to be used later in graphs and figures.
% starting condition - random position start
Z = [randi([-10 10]) randi([-10 10])];
d = sqrt((Z(1)-x0)^2+(Z(2)-y0)^2); % Pythagoras Theorem for distance between the Source and initial Reciever points
A random coordinate is generated for the first initial position and the distance between this position/ coordinate from the source (0,0).
%Random Walks Loop Code
for i = 1:iter
if i == 1
F = [randi([0 360])];
x = Z(1) + s*cos(F);
y = Z(2) + s*sin(F);
end
%Update for distance
s1 = sqrt((x-x0)^2+(y-y0)^2); % Pythagoras Theorem for distance between the 2 (Source and Reciever) point
TL = 20*log10(s1); %Transmission Loss
RL(i,:) = SL - TL; % Receive Level
% save result
X(i,:) = x(:,1);
Y(i,:) = y(:,2);
end
This is my loop, the F is a random number between 0 and 360 corresponding a degree to be used for a new position and coordinates the mammal will move towards to be used in the loop to calculate values for RL. I need this for every position / iter howver my code does not do this. The functions labelled x and y are the new positions and coordinates, but now going through it i see an error. The values for Z(1) and Z(2) need to change for each loop to be the previous x and y coordinates, rather than the initial random coordinates.
s1 shows the new distance between the coordinates, however I have just noticed the value for d earlier, also needs to be used here for the RL of the intial coordinates, so that itself has its own RL and TL, and then all the new coordinates within the loop has its own, all to be stored in a iter x 1 matrix, i.e., for each positional coordinates.
The save results shows all the results as a matrix to be used in figures and models. Also how do i sotre all the values from the function TL and s1 also.
figure
plot(x,y, '-*')
title('Random Walks')
xlabel('Distance from the Source (m)')
ylabel('Distance from the Source (m)')
ax = gca; %the change of the axes location to origin
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
A graph for the results which should showcase a random walks of a mammal moving based in different degree directions

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

### 채택된 답변

Jon 2022년 8월 3일
I have made numerous corrections in your code. I think this is now at least closer to what you want
clc;
clear;
x0 = 0;
y0 = 0;
Sr = [x0 y0]; %Source coordinates
a = 0.25; %Attenuation - FIND SOME VALUES
IL = 180; %Impact Level
v = 2; %Speed
t = 5; %seconds
T = 5400; %Total Time
s = v*t; %metres
iter = T/t; %number of positions
Tv = 0:t:(T-t);
SL = 212 + 10*log10(10);
X = zeros(iter,1);
Y = zeros(iter,1);
RL = zeros(iter,1);
% starting condition - random position start
Z = [randi([-10 10]) randi([-10 10])];
d = sqrt((Z(1)-x0)^2+(Z(2)-y0)^2); % Pythagoras Theorem for distance between the Source and initial Reciever points
%Random Walks Loop Code
for i = 1:iter
% % % if i == 1
F = [randi([0 360])];
x = Z(1) + s*cosd(F); % use cosd for angle in degrees
y = Z(2) + s*sind(F); % use sind for angle in degrees
Z = [x y]; % update current location
% % % end
%Update for distance
s1 = sqrt((x-x0)^2+(y-y0)^2); % Pythagoras Theorem for distance between the 2 (Source and Reciever) point
TL = 20*log10(s1); %Transmission Loss
% % % RL(i,:) = SL - TL; % Receive Level
RL(i) = SL - TL; % Receive Level
% save result
% % % X(i,:) = x(:,1);
% % % Y(i,:) = y(:,2);
X(i) = x;
Y(i) = y;
end
figure
% % % plot(x,y, '-*')
plot(X,Y, '-*')
title('Random Walks')
xlabel('Distance from the Source (m)')
ylabel('Distance from the Source (m)')
ax = gca; %the change of the axes location to origin
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
##### 댓글 수: 16표시숨기기 이전 댓글 수: 15
ahhh yes, now this is where i went wrong, the graph now looks more to like what i wanted. thank you

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

### 범주

Find more on Marine and Underwater Vehicles 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!

Translated by