Simulation of a function with loops

6 views (last 30 days)
Dear All,
I have a script that calculates a stock price. It has multiple loops inside that can go up to 250 and it is fine, but if I increase the number to say 350, I get recursion break. This is okay since I don't need more than 180 as a maximum (half year).
The thing is that I want to simulate this model (large simulation, 1000000) to get valid results. I have tried to include a loop script which loops according to a defined number, but I get the recursion limit when I set the number of simulations to 50+. This is obvious as the model is consuming memory continuously.
Since it is not really practical to run 50 simulations every time, record the results and run again till I get large simulations, my problem becomes how to get about this in the best way; I need guidelines as I don't have much time left, otherwise I would go and try all possible solutions. Although I believe that it has to do with Simulink, not sure though about the process or whether this is achievable in Matlab or not. I am grateful for your suggestions that would assist me in getting what I am looking for.
Please refer to the first two comments for Per's answer below for the process and script.
I do really appreciate any help or suggestions provided.
Thanks & Best// Ali
  2 Comments
Jan
Jan on 5 Jan 2013
Edited: Jan on 5 Jan 2013
What exactly is the question?
It should be a problem, when you set the 'String' property to a double value, e.g. in set(handles.St,'String',(101.6)). Then the number 101.6 is converted to a CHAR implicitly, such that char(101) is stored, which is an 'e'. Is this the wanted behavior?
AND
AND on 5 Jan 2013
Thanks Jan,
What exactly is the question?
The scenario is as follows: I have a function that loops inside to get a value. The function works fine and I am getting the required answer, and the question is how to apply large simulation to this function so I get large set of results.
It should be a problem, when you set the 'String' property to a double value, e.g. in set(handles.St,'String',(101.6)). Then the number 101.6 is converted to a CHAR implicitly, such that char(101) is stored, which is an 'e'. Is this the wanted behavior?
In my model, 101.6 is just an output of some calculations and the final result is correct.
I just need to know how to simulate this function.
Thanks again for your time// Ali

Sign in to comment.

Accepted Answer

per isakson
per isakson on 5 Jan 2013
  • with Matlab this is a script, not a function
  • I'm not sure I understand your question
  • the code doesn't explain why the recursion limit is reached. There is no recursion
  • setting the String-value of a uicontrol might take some time
Tentative answer:
  • convert the code to a function
  • call that function from another function, which contains the loop
Hint:
function some_output = pythia( )
some_output = zeros( zillion, 1 );
for ii = 1 : zillion
some_output(ii) = your_code( S, X, B, V, T, Tt )
end
end
function some_output = your_code( S, X, B, V, T, Tt )
FunctionOne;
St= S*(exp(randn));
if St<B
set(handles.St,'String',(0)) %set the value and exit
else
St=str2double( get(handles.St,'String')*exp(randn) );
if St<B
set(handles.St,'String',(0))
else
if tt<somecalculations
set(handles.St,'String',num2str(99.5)) %just an example
else
T=str2double( get(handles.T,'String') );
if T == 0
set(handles.St,'String', num2str(101.6) ) %just an example
else
set(handles.T,'String', num2str(T-1))
FunctionOne
end
end
end
end
end
function out = FunctionOne
out = 1;
end
  16 Comments
AND
AND on 5 Jan 2013
Just a quick add that might be useful for others for future reference, I just added a for loop at the beginning of the script and it is now working :)// Thanks again Per, your contribution helped me get into the solution.
per isakson
per isakson on 5 Jan 2013
Ali
Perfect. I'm glad I could help. Please accept the answer and we regard this thread as closed. //per

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!