# Fill array with called function using initial conditions. Eulers based.

3 views (last 30 days)
Joshua Liddle on 24 Sep 2021
Commented: Sulaymon Eshkabilov on 25 Sep 2021 at 17:57
I am using Eulers method to calculate positional vectors for 3 animals. Each animal has its own function.
The first animal (mouse) is constrained to a circular path. "xm and ym".
The second animal (cat) uses the first animal coordinates to "chase", these coordinates are "xc and yc".
The last animal (dog) uses the second animal to "chase", "xd and yd".
I have one main script that contains a loop:
%Get cat and dog position values, store in vector
for i = 1:n-1
[xc,yc] = Cat(xm,ym);
[xd,yd] = Dog(xc,yc);
xc = xc(i);
yc = yc(i);
xd = xd(i);
yd = yd(i);
end
However, my confusion arrises when using initial conditions for the cat and dog as inputs to the functions, then writing new " i+1 " values to the arrays of xm, ym, xc and yc.
Cat and dog functions are as follows:
%Cat position program
%------------------------------------------------------
function [xc,yc] = Cat(xm,ym)
%-------------------------------------------------
%Variable set
vc = 12/3.6; %10 km/hr converted to m/s
xc0 = -10; %Initial conditions
yc0 = 0;
%-------------------------------------------------
%Eulers Iterations
dmc = sqrt( (xm-xc)^2 + (ym-yc)^2 );
xc_dot = (vc*(xm-xc))/(dmc);
yc_dot = (vc*(ym-yc))/(dmc);
xc = xc + dt*xc_dot;
yc = yc + dt*yc_dot;
end
%Dog position program
%------------------------------------------------------
function [xd,yd] = Dog(xc,yc)
%-------------------------------------------------
%Variable set
vd = 20/3.6; %10 km/hr converted to m/s
xc0 = -20; %Starting x position
yc0 = 20; %Starting y position
%-------------------------------------------------
%Eulers Iterations
dcd = sqrt( (xc-xd)^2 + (yc-yd)^2 );
xd_dot = (vd*(xc-xd))/(dcd);
yd_dot = (vd*(yc-yd))/(dcd);
xd = xd + dt*xd_dot;
yd = yd + dt*yd_dot;
end
Basically my biggest confusion is how to take values of "i" and "i+1" and properly organize them into arrays which are located in the main script file. If the arrays were kept within the Cat and Dog functions, it would be much easier. However they need to be located in the main script. I also thought if I could pass an entire array as an input to the function, however I haven't been able to succesfully do that either
Any help is greatly appreciated.

Sulaymon Eshkabilov on 25 Sep 2021
There are a few variables are to be defined in your two fcn files, i.e.:
%Cat position program
%------------------------------------------------------
function [xc,yc] = Cat(xm,ym)
%-------------------------------------------------
%Variable set
vc = 12/3.6; %10 km/hr converted to m/s
% INITIAL VALUES are not used in your code as defined and thus, they need
% to be renamed:
xc = -10; %Initial conditions
yc = 0;
% INITILIZE dt:
dt =
%-------------------------------------------------
%Eulers Iterations
dmc = sqrt( (xm-xc)^2 + (ym-yc)^2 );
xc_dot = (vc*(xm-xc))/(dmc);
yc_dot = (vc*(ym-yc))/(dmc);
xc = xc + dt*xc_dot;
yc = yc + dt*yc_dot;
end
%Dog position program
%------------------------------------------------------
function [xd,yd] = Dog(xc,yc)
%-------------------------------------------------
%Variable set
vd = 20/3.6; %10 km/hr converted to m/s
% INITIAL VALUES are not used in your code as defined and thus, they need
% to be renamed:
xc = -20; %Starting x position
yc = 20; %Starting y position
% INITIALIZE the followings:
xd =
yd =
%-------------------------------------------------
%Eulers Iterations
dcd = sqrt( (xc-xd)^2 + (yc-yd)^2 );
xd_dot = (vd*(xc-xd))/(dcd);
yd_dot = (vd*(yc-yd))/(dcd);
xd = xd + dt*xd_dot;
yd = yd + dt*yd_dot;
end
Your main code should be edited a little bit, e.g:
% INITIAlIZE
n =
xm =
ym =
xc =
yc =
for i = 1:n-1
[xc,yc] = CAT(xm,ym);
[xd,yd] = Dog(xc,yc);
Xc(ii) = xc;
Yc(ii) = yc;
Xd(ii) = xd;
Yd(ii) = yd;
end
##### 2 CommentsShowHide 1 older comment
Sulaymon Eshkabilov on 25 Sep 2021 at 17:57
In fact, what I meant is this:
for ii = 1:n-1 % ii represents i. Since i is reserved in MATLAB for an imaginary number
[xc,yc] = CAT(xm,ym);
[xd,yd] = Dog(xc,yc);
Xc(ii) = xc;
Yc(ii) = yc;
Xd(ii) = xd;
Yd(ii) = yd;
end