global variable inside a function

2 views (last 30 days)
Babak on 25 Aug 2014
Edited: per isakson on 26 Aug 2014
Hello every one, In my program I used a solver (ODE45) and as you know, for this purpose I need to define a function. I have an element in my function that change with respect to time. For having time in my function I defined global time but unfortunately I got array of my time. I need to have exact time that solver is using!!. What should I do?
[tout,y]=ode45(@(t,X) sys1(X,B,L,Rr),t,zeros(50,1));
%%my function %%
function xdot=sys1(X,B,L,Rr)
global t;
Thanks in advance.

Answers (1)

AJ von Alt
AJ von Alt on 25 Aug 2014
There is no need to use a global. You can pass t in as an argument to your ode function. Example 3 of the ode45 documentation demonstrates this usage.

Community Treasure Hunt

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

Start Hunting!