Running Function through For Loop

Hi! I'm fairly new to MATLAB so I was wondering if someone could help me out. I'm coding a dynamic mathematical model with one function that consists of various time dependant variables.
I'm trying to run my function through a four loop multiple times. The function consists of 15 steady variables over time and the output I want is a plot with one of the variables from the function on the graph. I keep getting an error saying the variable I'm trying to plot is undefined - what would I do?
I tried something like this but I am not sure it's correct:
for GLC = 5:50:200
plot (t,BOLD)
hold on
end

15 Comments

Hima - you may want to post some of your code. How are you capturing the output of your function and plotting these results? From the error message, it sounds like you are trying to use variables that have not been defined...
Hima Sheth
Hima Sheth on 31 Mar 2019
Edited: Hima Sheth on 31 Mar 2019
I added something that I've tried. I defined the variable in the initial function but when I run the function through the loop, it says the the variable is not defined. The output of the function is various graphs that include the 15 varibles over time.
Geoff Hayes
Geoff Hayes on 31 Mar 2019
Edited: Geoff Hayes on 31 Mar 2019
Where are t and BOLD defined? Can you clarify what you mean by "I defined the variable in the initial function but when I run the function through the loop". Is t created by your initial function?
If you had a code sequence such as
function driver
t = 1:50;
plotme
end
function plotme
BOLD = sin(1:50);
plot(t, BOLD);
end
then t would be undefined in the workspace of plotme. MATLAB does not look in the environment of the caller of a function to try to find variables
well... except for the case of nested functions, in which case it does. The following would be valid:
function driver
t = 1 : 50
plotme
function plotme
BOLD = sin(1:50);
plot(t,BOLD);
end
end
Notice in this situation that the second function is defined inside the first function, and that the shared variable (t) was defined before the second function was defined.
t and BOLD are both defined within the original function. This is how the code is set up:
function model
%define parameters
%% there are numerous parameter values here
%set initial condition
S0=[15,1.2,0.0057,0.02,0.16,1,0.026,2.2,5,0.0262,7.01,4.56,0.35,0.0237,0.063];
%declare right-hand-side function (defined below)
ODEFUN=@netexddt;
%perform simulation over time-interval [0,800]
[t,S]=ode15s(ODEFUN, [0,900], S0);
%plots are defined
end
function dS=netexddt(t,S)
%define right-hand-side of differential equations
%% all 15 steady, time dependant variables are defined (i.e S(1) = NAi)
%parameters are defined again in terms of their values
%rate equations are defined (i.e vLeak = a+b/c)
%differential equations are solved in terms of the rates within function ds = [...] (i.e Nai = vLeak +vLDH)
end
And where are you calling the following code from?
for GLC = 5:50:200
plot (t,BOLD)
hold on
end
In the above for loop, the iterating parameter is GLC...but that isn't used within the body of the loop so you end up trying to plot the same thing a handful of times...
Hima's answer moved here
I'm calling the code within the same script - from the model function.
How would I structure the code so that I'm changing the values of GLCa but I'm getting the output for BOLD (which is a variable within the model). BOLD is dependant on the value of GLCa.
Thank you again for the help I appreciate it!
How are the GLC used? Is that something passed in to the netexddt function?
The GLCa is a parameter in one of the rate equations for BOLD (BOLD is defined in rates).
you may need to post more or all of your code so we can see how the two are used together.
I have attached a file with my code
You define BOLD as a nested function, but you try to call it outside of the function it is defined inside. Also you define BOLD as returning no values, but when you call plot(t,BOLD) then that requires calling BOLD and having it return a value.
All the varibles in the function (model) are interconnected so how would I define BOLD in way that it works both in the original model and in the for loop?
I was wrong that BOLD is called outside of the function it is defined in. Still, it is defined as returning no values and that is a problem.
After you define your two nested functions, then near line 280 you define
y(t) = (VvO(k1*(dHb/dHb0))) etc
However, in that scope, none of the variables used in that expression are defined.
When you define nested functions with shared variables, you need to use the form
function outer_function
assigments to variables intended to be shared
function inner_result = inner_function
references and possibly assignments to shared variables
end
call to inner_function
end
or else the form
function outer_function
assignments to variables intended to be shared
call to inner_function
function inner_result = inner_function
references and possibly assignments to shared variables
end
end
That is, the definition of the nested function can be before or after the calls to the nested function, but the initial assignments to the shared variables must be before the definition of the nested function. If you use the form
function outer_function
assignments to variables intended to be shared
%but *no* assignment to Variable42
function inner_result = inner_function
references to and possibly assignments to shared variables
references to and possibly assignments to Variable42
end
assignment to Variable42
call to inner_function
end
then Variable42 will not be shared. Only variables clearly assigned to in the outer function before the definition of the nested function will be shared.
Note: I firmly recommend that you get rid of those global variables. Convert them to shared variables, or use parameterize your function: http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Okay, thank you so much!

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 31 Mar 2019

Commented:

on 31 Mar 2019

Community Treasure Hunt

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

Start Hunting!