MATLAB: Undefined variable in a for cycle

for cyclevariable

In this script (where function is a law of motion function=function(y,y',t)) I would like to find the response of the system varying the parameter 'omega'(included in the function as a force m*g*sin(omega*t))
[t,x]=ode45(@function,[0 0+dt],[0;0;0;0]);
plot(omega*ones(Nplot,1), x, '.', 'markersize', 2);
hold on;
I tried to write a for cycle like:
for w = 10:10:500
[t,x]=ode45(@function,[0 0+dt],[0;0;0;0]);
plot(omega*ones(Nplot,1), x, '.', 'markersize', 2);
hold on;
end,
but if I put omega as a variable of input for 'function' the error that appear is: ??? Input argument "omega" is undefined. how can i do?? thanks a lot!!

Best Answer

If you want to pass extra variables to the function then redefine the function to have three (or more!) inputs:
function da = Lawofmotion(t, a, omega)
and then inside of each loop create an anonymous function which only has two arguments (untested because I don't have dt):
for omega = 10:10:500
fun = @(t,a) Lawofmotion(t,a,omega);
[t,x] = ode45(fun, [0,0+dt], [0;0;0;0]);
... etc
end
Related Question