24 views (last 30 days)

Show older comments

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
on 28 Aug 2019

Edited: KALYAN ACHARJYA
on 28 Aug 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!

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

Start Hunting!