Declare global variable & share with other function, BUT global variable change every iteration

1 view (last 30 days)
I am trying to share the iteration result that changes every second for 2000 seconds period from a function to another function. I want to avoid getting additional variable back, so I declared the result global in both functions. The code looks like this:
global y
[t,x]=ode45('filename',[ODEstart:ODEspan:ODEend],init);
y=x(end);
in this case, x is an array of numbers, and I just want to pass the last value in the array into the other function, which looks like this:
function filename
global y
A=y^2;
B=....
end
The whole point of doing this is just to pass the value of y into filename.m. But sadly, the value of y wouldn't change (it should change after each iteration). So I'm stuck...
Is there any way to fix the error, or any other way to do this?
Please help me. Thank you.

Answers (1)

Walter Roberson
Walter Roberson on 17 Feb 2012
What do you define as an "iteration" here? ode45() does not return any value until the entire time span is completed.
Have you considered using the options structure with an output function there?
  1 Comment
Peiwen Lau
Peiwen Lau on 17 Feb 2012
The ODE is used to solve for two ordinary differential equations. And, x is the solution, which is an array, in which I would like to pass the last element in the array to another function. If I am to run ODE45 for 100 seconds, and which the ODE45 will be ran for say 5 cycles, that means the value of x(end) will change for 5 times. example:
for cycle=1:1:5
[t,x]=ode45('filename',[ODEstart:ODEspan:ODEend],init);
y=x(end);
function filename
global y
A=y^2;
B=....
end
what do you mean by the options structure with an output function? I do not want to accept any extra variable, just want to pass the variable, i.e. x, to the other function.
Thank you

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!