Simulation of a function with loops
6 views (last 30 days)
Show older comments
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
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?
Accepted Answer
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
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
More Answers (0)
See Also
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!