Question

i am trying to solve a differential equation using ode45, I have a function in which one of the parameters has to vary by specific step, here is my function:

function f=RSJ(y,t,M1,P,M2,E,current)

f=(current/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);

P, M1, M2 & E are numerical constants, current is the parameter which I should resolve this differential equation for several cases, for example current=0:1:10 how can I do such a thing?

Was it helpful?

Solution

Use a closure (a.k.a. anonymous or lambda function):

% declare t, y, tspan and y0
% [...]
current = 6e-7 : 1e-8 : 8.5e-7;
for k=1:length(current)
    f = @(y, t, M1, P, M2, E) (current(k)/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);
    [t{k}, y{k}] = ode45(f, tspan, y0);
end

OTHER TIPS

A quick and dirty solution. Define current as a global variable (you need to do this in both the base workspace and in the function) and use a for loop, e.g.

current_vector=1e-7*(6:0.1:8.5);
global current
for k=1:length(current_vector)
    current = current_vector(k);
    [t{k},y{k}]=ode45(f,<tspan>,<y0>)
end

Replace <tspan> and <y0> by the appropriate values/variables. I assume the other constants are defined in the body of the function, in which case your function should look something like this:

function f=RSJ(t,y)
    global current
    M1 = ... % etc...
    f=(current/P)-(M1/P)*sin(y)+(M2/P)*sin(y+E);
end

BTW, I don't see any explicit dependence on time t in your function...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top