Reputation: 15
I have a dynamical system ode
, which I need to solve for various values of one of its parameters, the parameter r
. However, the function sigm
(sigmoid function), appears many times in the system and it was considered preferable to code it as a separate function outside of the system.
r = 0:+0.01:1;
time = 0:.01:10;
y0 = [0 0 0 0 0 0 0 0];
y = NaN(length(time),length(y0),length(r));
for i=1:length(r)
[t,y(:,:,i)] = ode45(@(t,y) ode(t,y,r(i)),time, y0);
...
end
function dydt = ode(~,y,r)
dydt = NaN(8,1);
dydt(1) = y(5);
dydt(2) = y(6);
dydt(3) = y(7);
dydt(4) = y(8);
dydt(5) = sigm(y(3)-y(4)) - y(5)- y(1);
dydt(6) = sigm(y(3)-y(4)) - y(6) - y(2);
dydt(7) = sigm(y(1)) - y(7) - y(3);
dydt(8) = sigm(y(2)) - y(8) - y(4);
end
function X = sigm(u,r)
X = 1/(1+exp(r*(6-u)));
end
The point where i am having a difficulty is that, the parameter r
, only appears in the sigmoid function and not in the ode. Thus, when I am trying to solve the system for many values of this parameter, i get error not enough input arguments
.
How can I pass this parameter into the second function?
A potential way, would be to put the whole sigm
function, within the ode
function:
function dydt = ode(~,y,r)
dydt = NaN(8,1);
dydt(1) = y(5);
dydt(2) = y(6);
dydt(3) = y(7);
dydt(4) = y(8);
dydt(5) = sigm(y(3)-y(4)) - y(5)- y(1);
dydt(6) = sigm(y(3)-y(4)) - y(6) - y(2);
dydt(7) = sigm(y(1)) - y(7) - y(3);
dydt(8) = sigm(y(2)) - y(8) - y(4);
function X = sigm(u)
X = 1/(1+exp(r*(6-u)));
end
end
, but I am guessing it is not a good coding tacticts
Upvotes: 0
Views: 32
Reputation: 25992
Within the function declaration
function dydt = ode(~,y,r)
the parameter r
is defined as a local variable that can then be used like any other scalar value, such as passing it as the second parameter to sigm
, like in
dydt(5) = sigm(y(3)-y(4), r) - y(5)- y(1);
Upvotes: 1