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

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 commentaires

What have you done so far?
Matt J
Matt J le 3 Jan 2013
Modifié(e) : Matt J le 3 Jan 2013
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?
Clarify the connection between loops and recursion. Does each of the 40 loops initiate a recursive call? Even if so, why would you reach the recursion limit (default = 500) when each simulation requires only 40 recursions.
AND
AND le 3 Jan 2013
Modifié(e) : AND le 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 le 3 Jan 2013
Modifié(e) : Matt J le 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.

Connectez-vous pour commenter.

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 4 Jan 2013
Modifié(e) : Azzi Abdelmalek le 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 commentaires

Thanks Azzi,
Now you know what I am looking for; I will give your solution a try; will update you and the post with the results//
Thanks again
Dear Azzi, Thanks again for your contribution; I really appreciate it, but unfortunately it didn't work. I tried alternative counter method. In the normal case, if "number" of simulations is set to 5, I get in the command window (for illustration):
s =
101
s =
111.2220
s =
99.0393
s =
114.1180
s =
97.2261
But after I added the counter which is this:
s=str2double(get(handles.StockP,'String'));
ii=str2double(get(handles.counter,'String'));
ii=ii+1;
set(handles.counter,'String',(ii));
y(ii)=s
I get this:
y =
101
y =
0 115.0010
y =
0 0 113.1380
y =
0 0 0 99.4598
y =
0 0 0 0 99.9935
I don't really know what is wrong, if the index could be any real number like the prices I am getting, I could just save s as a vector directly.
If I get the solution to the simple example below, I think that the problem will be solved:
Normally, if 'i' is a vector of positive integers, the vector of its product say 'i*rand' would be easy to get:
i=1:5;
y(i)=i*rand
y =
0.4468 0.8935 1.3403 1.7871 2.2339
But if 'i' is just a real number, say:
i=0.1:0.5;
y(i)=i*rand
Attempted to access y(0.1); index must be a positive integer or logical.
Then I am not able to get the vector of the product of 'i' which is 'y' in this case. I think if I can get the vector of the last case, my problem will be solved in the same manner. At least, this what I hope.
Thanks again and all best// Ali
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
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:

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 3 Jan 2013
Modifié(e) : Matt J le 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 commentaire

AND
AND le 4 Jan 2013
Modifié(e) : AND le 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

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by