Saving data in a loop into a vector (values are not integers)

10 views (last 30 days)
Hello Everyone,
I am facing a little problem and would be very thankful if you can help me with it.
I have a function to calculate a stock price. It can loop up to 40 times to get the final price. I wanted to simulate the function and came into two problems:
1- I added a new variable, say x, that stands for the number of simulations. Now, at the beginning of the function, if x > 0, the main algorithm starts and loops over until x = 0. The problem is when it comes to saving the different stock prices at each run. Suppose x = 5, then I have 5 stock prices which I can see in Matlab command window. Suppose the prices "S" are [100.5 101.3 99.80 97.6 102.3], I can see it as: S = 100.5 ..... S = 102.3 but I want to save them all in one vector; I couldn't use y(S) as S is not an integer! any suggestions?
2- I was aiming to do some big simulation with the model, but noticed that I reached the recursion limit. Since up to 40 loops are required at each simulation/ do you think it is still possible to achieve this goal, say with 10,000 simulations? if yes, any initial guidance for this?
Thanks & Best Ali
  4 Comments
AND
AND on 3 Jan 2013
Edited: AND on 3 Jan 2013
Many thanks for your kind reply guys.
Azzi: I have finished with the algorithm, now comes the simulation. I am using GUI for the purpose. "Just to add: I have searched the whole day for this, didn't come up with what I need but indeed learnt many useful things"
Matt: Thanks for the point, it should have been clarified. The main 40 loops don't call for recursive call, but if I set x, the number of simulation, to any value above 25; that is above 25 simulations of the model which contains the 40 loops, then there will be a recursive call. Note that the number of loops inside the main function can go up to 40 loops but can be any value between 1 and 40.
Thanks again.
Best// Ali
Matt J
Matt J on 3 Jan 2013
Edited: Matt J on 3 Jan 2013
Thanks for the point, it should have been clarified. The main 40 loops don't call for recursive call, but if I set x, the number of simulation, to any value above 25; that is above 25 simulations of the model which contains the 40 loops, then there will be a recursive call.
There's still no way we (those of us who have never seen your code) can understand why that occurs or why we should expect it to break the recursion limit.

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 4 Jan 2013
Edited: Azzi Abdelmalek on 4 Jan 2013
If you want to put several numbers in one vector then create a counter
ii=0
for s=0::0.1:2
ii=ii+1
y(ii)=s
end
  4 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 4 Jan 2013
Edited: Azzi Abdelmalek on 4 Jan 2013
Use this
ii=0.1:0.5;
counter=1:numel(ii)
y(counter)=ii*rand
(avoid using i, it's reserved to complex numbers)
Other thing, you have to save your y variable, like you saved ii
AND
AND on 4 Jan 2013
Thanks Azzi, the problem is solved using the method you provided in the other post// just as a reference for those who might be interested in the answer, here is the link:

Sign in to comment.

More Answers (1)

Matt J
Matt J on 3 Jan 2013
Edited: Matt J on 3 Jan 2013
What does the code that generates S look like? You should change it to something that does this
S(1) = 100.5 ..... S(5) = 102.3
  1 Comment
AND
AND on 4 Jan 2013
Edited: AND on 4 Jan 2013
It is 100+ lines, so I will summarize it below in a simple way, where many variables and mathematical formulas are omitted, as otherwise, it is very complicated.
S=102; %Stock Price
X=105; %Exercise Price
B=100; %Barrier
V=0.01; %Volatility
T=20; %Days to Expiration
Tt=0.4706; %Hours before the close of the day
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',(99.5)) %just an example
else
T=str2double(get(handles.T,'String')
if T=0
set(handles.St,'String',(101.6)) %just an example
else
set(handles.T,'String',(T-1))
FunctionOne
end
end
end
end
end
Kindly note that the real model has more calculations inside and two more sub-loops, but should not affect the purpose of my question.
Dear Matt, I wish your answer would solve my problem but it doesn't as this is what I already have. Before rephrasing my question, here is the exact function that is supposed to do the simulation.
function algosim
No=str2double(get(handles.number,'String'));
if No>0
i=str2double(get(handles.StockP,'String')) %**
clearbutton_Callback(hObject, eventdata, handles)
set(handles.number,'String',(No-1))
FunctionOne %Calling the main algorithm
end
end
**Here is how I am getting the prices. Even if I use disp(i) here, I get the 20 different results if "number" is set to 20 by the user, displayed in the command window as I leave i open (without ;)The thing is that I need to have all these i's in one vector, i couldn't use y(i) because stock values are not integers

Sign in to comment.

Categories

Find more on Data Preprocessing 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!