program error :Unbalanced or unexpected parenthesis or bracket

Hi all
I wrote a program for below formula , but when i run it , it face an error (Unbalanced or unexpected parenthesis or bracket.)
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)
I attached the program but for certain I write the code here :
clc;
clear;
r=0.01;
u=0.05;
d=-0.04;
n=4;
j=0;
St=40;
K=25;
p=(r-d)/(u-d)
C=(1/(1+r)^n)*(symsum(nchoosek(n,j)*p^j*(1-p)^(n-j)*max(0,((1+u)^j)*(1+d)^(n-j)*St/K))),j,0,n)
Could anyone help me and fix change the program to work and fix C amount
I'm in hurry and just have 2 hours
plz help me
thank you all

8 件のコメント

amir khalighi
amir khalighi 2018 年 5 月 4 日
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)|
Ameer Hamza
Ameer Hamza 2018 年 5 月 4 日
You are not adding previous values. Change your code like below. C is incremented in each loop.
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;
C = 0;
for i=0:n;
v=nchoosek(n,i);
X=((1+u)^i*(1+d)^(n-i)*St-K);
t=max(0,X);
C=C+1/((1+r)^n)*v*(p^i)*(p^(n-i)*t)
end
amir khalighi
amir khalighi 2018 年 5 月 4 日
Yes that's right
it works well not and for c<0 what should i do?i want them to change to 0 and them sum with other c
Ameer Hamza
Ameer Hamza 2018 年 5 月 4 日
What do you mean? If C < 0, you want to make C = 0?
amir khalighi
amir khalighi 2018 年 5 月 4 日
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 output c is -20 -10 and 10 then put final C=0+0+10=10 and for example when C=20 put C=20 and make no changes) I don't know it was clear or not
Ameer Hamza
Ameer Hamza 2018 年 5 月 4 日
編集済み: Ameer Hamza 2018 年 5 月 4 日
It appears that what you are saying can be done as follow, replace this line in your code.
C = C + max(1/((1+r)^n)*v*(p^i)*(p^(n-i)*t), 0);
replace this line in your code.
amir khalighi
amir khalighi 2018 年 5 月 4 日
awsome
thank you very very much :)
Ameer Hamza
Ameer Hamza 2018 年 5 月 4 日
You are welcome.

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2018 年 5 月 4 日

0 投票

As evident from the error message, you are having unbalanced brackets. You should clearly pay attention to error messages. Also max() function will only work on symbolic expression if you clearly describe its type. Here is the corrected code
clc;
clear;
r=0.01;
u=0.05;
d=-0.04;
n=4;
syms k integer;
St=40;
K=25;
p=(r-d)/(u-d);
C=(1/(1+r)^n)*symsum((nchoosek(n,k).*p.^k*(1-p).^(n-k).*max(0,((1+u).^k).*(1+d).^(n-k).*St/K)),k,0,n);

2 件のコメント

amir khalighi
amir khalighi 2018 年 5 月 4 日
Error using sym/max (line 97) Input arguments must be convertible to floating-point numbers.
Error in math (line 11) C=(1/(1+r)^n)*symsum((nchoosek(n,k).*p.^k*(1-p).^(n-k).*max(0,((1+u).^k).*(1+d).^(n-k).*St/K)),k,0,n);
  • what should i do now?
Ameer Hamza
Ameer Hamza 2018 年 5 月 4 日
Have you ran the following line.
syms k integer;
I also changed variable name from j to k, to avoid conflict with MATLAB internal constant.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by