MATLAB: How not to overwrite the values of a variable

overwrite variable

my code is resolving an algebraic-differential system of equations. thus, it is built as the main file (main), that is calling another file (sis_dif), where there are the differential equations and that is calling anothere file (sis_alg), where there are the algebric equations. my problem is that when I call in the main the value of a variable that is calculated in the sis_dif file it gives me only the last value because it overwrites its value at each step of integration. i am looking for a way to tell to Matlab not to overwrite this value, but to create, for example an empty vector at the beginning and to fill it with each value given by the resolution of both the file which form my algebraic-differential system. basically, in the main i call the differential system:
[t,y] = ode45(@Sisdif, tspan, y0, options);
and the system is 4 equations like dy/dt, where y are the differential variables. in these equations compare the algebric variables, so in the code Sisdif i call the algebric system:
[sol,exitflag] = fsolve('sisalg',v,options);
and the system is 2 non linear agebric equations. and one of these two "sol" is the solution i am looking for and i need to plot its value with the time in the main file. Thus, i put it in the global variables in order to have it in the main file, but it only gives me the last value since it is calculated in the algebraic system and not in the differential. i hope that is clear this time.

Best Answer

Without seeing the code, it is very unlikely that a useful answer can be guessed. Please post at least a minimal example, which reproduces the explained behavior. Usually it is very easy to store results in a vector instead of overwriting them. Instead of something like:
for k = 1:n
result = YourOperations;
end
write:
result = zeros(1, n);
for k = 1:n
result(k) = YourOperations;
end
But perhaps you need a cell array or a matrix instead.
Please post more details by editing the original question, not by adding a comment or pseudo-answer. Reader should find all required information in one piece. Thanks.