# Help, MatLab - Binomial results

• Feb 7th 2009, 11:08 AM
phildastous
Help, MatLab - Binomial results
Hi,

I use a binomial model to price an European put option. The code is pretty simple.

My problem is I want to test the answer for values of M from 1:100. However, the only answer that is kept in the workspace is for the latest value of the option, when M = 100.

How could I create a table in the workspace that would keep trace of every value of the option for the given M [1:100]

Thanks,

Here is the code

Code:

```% Binomial method for a European put. % % Uses explicit solution based on binomial expansion. % Vectorized and based on logs to avoid overflow. %%%%%%%%%% Problem and method parameters %%%%%%%%%%%%% for M = 1:100 S = 5;E = 10;T = 1;r = 0.06;sigma = 0.3; dt = T/M;h = 1; u = exp(sigma*dt^0.5)+h*sigma^2*dt;d = 1/u;p = (exp(r*dt)-d)/(u-d); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Option values at time T W = max(E-S*d.^([M:-1:0]').*u.^([0:M]'),0); % log/cumsum version csl = cumsum(log([1;[1:M]'])); tmp = csl(M+1) - csl - csl(M+1:-1:1) + log(p)*([0:M]') + log(1-p)*([M:-1:0]'); value = exp(-r*T)*sum(exp(tmp).*W);   disp('Option value is'), disp(value) end```
• Feb 9th 2009, 12:25 AM
CaptainBlack
Code:

```% Binomial method for a European put. % % Uses explicit solution based on binomial expansion. % Vectorized and based on logs to avoid overflow. %%%%%%%%%% Problem and method parameters %%%%%%%%%%%%%   SavedResults=[];  for M = 1:100 S = 5;E = 10;T = 1;r = 0.06;sigma = 0.3; dt = T/M;h = 1; u = exp(sigma*dt^0.5)+h*sigma^2*dt;d = 1/u;p = (exp(r*dt)-d)/(u-d); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Option values at time T W = max(E-S*d.^([M:-1:0]').*u.^([0:M]'),0); % log/cumsum version csl = cumsum(log([1;[1:M]'])); tmp = csl(M+1) - csl - csl(M+1:-1:1) + log(p)*([0:M]') + log(1-p)*([M:-1:0]'); value = exp(-r*T)*sum(exp(tmp).*W);   disp('Option value is'), disp(value)   SavedResults=[SavedResults,value]; end```
and the array SavedResults should contain all the results.

CB