I've changed it to code below but gives me different C and doesn't sum them
clc;
clear;
r=0.01;
u=0.05;
d=-0.04;
n=4;
St=30;
K=25;
p=(r-d)/(u-d);
pi=1-p;
for i=0:n;
v=nchoosek(n,i);
X=((1+u)^i*(1+d)^(n-i)*St-K);
t=max(0,X);
C=1/((1+r)^n)*v*(p^i)*(p^(n-i)*t)
end
and also for C calculated amount I need a fixation : when calculated C from above formula is less than 0 put C=0 and when C is amount from above formula is bigger than 0 put C=calculated amount ( i mean for exmple when it become -20 then put C=0 and for example when C=20 put C=20 and make no changes)|