Fill array with called function using initial conditions. Eulers based.
이전 댓글 표시
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.
답변 (1개)
Sulaymon Eshkabilov
2021년 9월 25일
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
Joshua Liddle
2021년 9월 25일
Sulaymon Eshkabilov
2021년 9월 25일
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
카테고리
도움말 센터 및 File Exchange에서 Simulation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!