several initial conditions in a loop

조회 수: 5 (최근 30일)
Samson
Samson 2019년 8월 28일
댓글: Samson 2019년 8월 28일
a=0.2;b=0.2;c=5.7;sigma= 16; beta= 4; rho = 45.92;
% initial condition
x= rand(9,1);
g= (0:5:60);
% g= 0.5;
% computing the trajectory
dt = 0.01;
tspan = 100000;
xinput = x;
X = zeros(9,tspan);
deltaDR=zeros(length(g));
deltaRA=zeros(length(g));
for j= 1:length(g)
X(:,1)=x;
for i = 1: tspan
xoutput = rk4angelstepb(@attractor,dt,xinput,a,b,c,sigma,beta,rho,g(j));
X(:,i) = xoutput;
xinput = xoutput;
end
have a silmulation problem with a nonlinear dynamical equations. I have written my matlab code given above. Now, I want to run the code for several times say up to 100 times different initial values using a loop and finally taking the average of these initial values. How can I do it please?

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 8월 28일
편집: KALYAN ACHARJYA 2019년 8월 28일
One way
% Define Intital conditions
% or load in txt/exel file
num_iter= ?? % Define
for i=1:num_iter
% Call the different initial conditions one by one
%your code
end
Other way
Make the main code as function
say
function output parameters=function_name(a,b,c,sigma,beta,rho);
%Main code
end
Now call the function in for loop with passing the different initial conditions,
Another way
Define all initial conditions in array, lets you have 5 values of all initial parameters, then
a=[0.34 0.45 0.56 .066 0.77]; % Example
b=[ ......]
c=[.....];
......
for m=1:5
x= rand(9,1);
g= (0:5:60);
% g= 0.5;
% computing the trajectory
dt = 0.01;
tspan = 100000;
xinput = x;
X = zeros(9,tspan);
deltaDR=zeros(length(g));
deltaRA=zeros(length(g));
for j= 1:length(g)
X(:,1)=x;
for i = 1: tspan
xoutput = rk4angelstepb(@attractor,dt,xinput,a(m),b(m),c(m),sigma(m),beta(m),rho(m),g(j));
X(:,i) = xoutput;
xinput = xoutput;
end
end
But save the xoutput in array, if its output is scalar, if vector, use cell array
Hope you get the hints!
  댓글 수: 1
Samson
Samson 2019년 8월 28일
Thank you so much Kalyan. I chose the first option which I have ran. Find below an example of the code. However my second question is yet to be answered. I needed to take the average of all the initial conditions.
% initial condition
x= rand(9,1);
num_iter = 100;
for k = 1:100
% my code
end
Meanwhile, my initial code was define with a plot of three graphs since I have 3 dynamical equations each having 3 components making a total of 9 initial condition. Since I want to silmulate for a 100 different initial conditions. I guess I should have several graphs too before averaging them. Find below the code also for my first initial condition I had before creating the loop you have adviced.
figure
subplot(3,1,1)
plot3(X(1,:),X(2,:),X(3,:),'b')
subplot(3,1,2)
plot3(X(4,:),X(5,:),X(6,:),'b')
subplot(3,1,3)
plot3(X(7,:),X(8,:),X(9,:),'b')

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by