undefined variable in a for cycle

1 view (last 30 days)
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!!
  5 Comments
Steven Lord
Steven Lord on 16 Jul 2015
Stephen: if you find it possible to create a variable named function or to call a function you've created that is named function, contact Technical Support immediately. The identifier "function" is a keyword, and with one exception (that doesn't apply to "function") you should NOT be able to define variables or call functions with the same name as a keyword.
Stephen23
Stephen23 on 16 Jul 2015
@Steven Lord: I have never tried... but that sounds like an interesting challenge :)

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Jul 2015
Edited: Stephen23 on 16 Jul 2015
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

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!