Checking parameter value in a function
5 views (last 30 days)
Show older comments
Clement Koh
on 8 Nov 2020
Edited: Sreeranj Jayadevan
on 11 Nov 2020
Hi all!
Suppose I have a few parameters in a function file of ODEs. And given that I have set a parameter (for e.g 'Var_A') to change according to time. Are there any ways that I can retrieve the value of the parameter over a period of time and plot them? I just want to show that the parameter change over a period of time.
Pls advise whoever knows this. Thank you :)
0 Comments
Accepted Answer
Sreeranj Jayadevan
on 11 Nov 2020
Edited: Sreeranj Jayadevan
on 11 Nov 2020
An easy way to achieve this is to create a scalar variable and then to gradually increase the size over time while you execute the code. For example, if you wish to store the value of 'Var_A' after each iteration (with respect to time in this case) you can concatenate the new values over time and create a 1-D array. i,e
Arr=0;
for i=1:n
% Various operations your function performs. Var_A gets updated here
Arr(i)=Var_A;
end
'Arr' will contain the required values which you can plot with respect to time.
Note: This approach is however not recommended if 'n' is a large value as this leads to repeated reallocation of memory by MATLAB. You can go through the following link for more information: https://in.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html
A alternative method which is more efficient would be to initialize the array/vector beforehand and populating the initialized array during execution. If you know how many elements the vector will contain after execution, you can initialize the vector.
0 Comments
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations 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!