# calling three different functions in one script

7 views (last 30 days)
Samson on 9 Jul 2019
Answered: Chidvi Modala on 17 Jul 2019
I have written three functions and I would like to call them in one script. How the call this functions in one file. I need to silmulate them using runge kutta 4th order. I have the code to silmulate the three functions but I get an error message . What am I doing wrong?
function dx = rosslerbb(x,a,b,c)
% x is a three dimensional state vector
dx = [
-(x(2)+ x(3));
x(1) + a*x(2);
b + x(3)*(x(1)-c);
];
function dy = lorenzbbb(y,sigma,beta,rho)
% z is a three dimensional state vector
dy = [
sigma*(y(2)- y(1))-g(y(1)-x(1));
y(1) *( rho-y(3))-y(2);
y(1)*y(2)-beta*y(3);
];
function dz = lorenzbb(z,sigma,beta,rho)
% z is a three dimensional state vector
dz = [
sigma*(z(2)- z(1))-g(z(1)-x(1));
z(1) *( rho-z(3))-z(2);
z(1)*z(2)-beta*z(3);
];
Code to silmulate them, I created a file called Angela and I saved all the functions in it and then use this
a= 0.2; b= 0.2; c = 5.7;sigma = 16;beta = 4;rho = 45.92;
% initial condition
yo = [0.75;0.75;1];
% couplin strength
g = (0:0.1:1);
% computing the trajectory
dt = 0.01;
tspan = (dt:dt:1000);
yinput = yo;
Y = zeros(3,tspan(end)/dt);
for i = 1: tspan(end)/dt
for j = 1:length(g)
time = i*dt;
youtput = rk4angelstepb(@Angela,dt,yinput,a,b,c,sigma,beta,rho);
Y(:,i) = youtput;
yinput = youtput;
end
end

Roy Kadesh on 9 Jul 2019
Your code is very confusing, and not valid Matlab syntax. Also, your first function doesn't call the second or third function. The second and third function are also almost the same, and have the same name.
Can you explain what you mean?
Samson on 13 Jul 2019
I have three functions which are actually equations. The third function is a replica of the second function. I need to integrate them together using an integrator. Here I used the runge kutta integrator. How should I call the functions together and then integrate

Chidvi Modala on 17 Jul 2019
I understand that you want to call 3 functions together and then use the outputs of the functions to integrate.
I assume that "rk4angelstepb" function is already defined.
To call the functions together, create a function named Angela with all the 3 functions as nested functions as shown below
function dout=Angela(a,b,c,x,y,z,sigma,beta,rho)
dx=rosslerbb(x,a,b,c);
dy=lorenzbbb(y,sigma,beta,rho);
dz=lorenzbb(z,sigma,beta,rho);
dout=[dx,dy,dz];
function dx = rosslerbb(x,a,b,c)
% x is a three dimensional state vector
dx = [-(x(2)+ x(3));x(1) + a*x(2);b + x(3)*(x(1)-c) ];
end
function dy = lorenzbbb(y,sigma,beta,rho)
dy = [ sigma*(y(2)- y(1))-g(y(1)-x(1)); y(1) *( rho-y(3))-y(2); y(1)*y(2)-beta*y(3) ];
end
function dz = lorenzbb(z,sigma,beta,rho)
% z is a three dimensional state vector
dz = [ sigma*(z(2)- z(1))-g(z(1)-x(1));z(1) *( rho-z(3))-z(2); z(1)*z(2)-beta*z(3) ];
end
end
Now create a function handle for 'Angela' and pass this function handle along with the other input arguments to the defined "rk4angelstepb" function as shown below.
function_handle=@Angela;
y=rk4angelstepb(function_handle(a,b,c,x,y,z,sigma,beta,rho),dt,yinput,a,b,c,sigma,beta,rho);