several initial conditions in a loop

5 vues (au cours des 30 derniers jours)
Samson
Samson le 28 Août 2019
Commenté : Samson le 28 Août 2019
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?

Réponse acceptée

KALYAN ACHARJYA
KALYAN ACHARJYA le 28 Août 2019
Modifié(e) : KALYAN ACHARJYA le 28 Août 2019
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 commentaire
Samson
Samson le 28 Août 2019
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')

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by