writing a formula in matlab

Hello all I have a formula that I want to write it in matlab but I don't know about sigma and Combination,can anyone help me please the formula is
i want to calculate 'C' amount but when it is less or equal with 0 'C=0' and when it is bigger than 0 C amount=the calculated amount all of the amounts (r,u,d,n,St-n,k) are given ('p' should calculate) and if it is possible I want to do it with xlsread for given data and rewrite C amount in the main excel file I don't know i explained it well or not :( thanks for all of your helps

1 Comment

I wrote this program but it's not working could anyone tell me what's the problem please?

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 30 Apr 2018
amir - since you have a summation, then consider using a for loop to iterate from 0 to n. See for loops for more details. As for the n choose j, see nchoosek.

11 Comments

thx,I know I should use symsum and nchoosek order but I don't know how I should use them :(
I wrote this code but it's not working:
amir - you will need to clarify what you mean by it's not working. Are you observing any errors? If so, what are they?
could you run it plz, I don't know what's the problem, error says : ndoreyi Error: File: ndoreyi.m Line: 11 Column: 87 Expression or statement is incorrect--possibly unbalanced (, {, or [.
>> ndoreyi Error: File: ndoreyi.m Line: 11 Column: 87 Expression or statement is incorrect--possibly unbalanced (, {, or [.
and also this part : 'C' amount when it is less or equal with 0 'C=0' and when it is bigger than 0 C amount=the calculated amount
I don't know what to do and write in program
thank you
I suspect part of the problem is with the square brackets around
[1/(1+r)^n]
in
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)))
Try replacing with () brackets instead. Also, what is k as it seems undefined.
Unfortunately, I don't have the Symbolic Math Toolbox so cannot run the above code...which is why I suggested using a for loop.
I don'y know how to write for loop :(
Could you write it for me, my time is limited and i have about 8 hours from now :(
and also this part : 'C' amount when it is less or equal with 0 'C=0' and when it is bigger than 0 C amount=the calculated amount .plz
Your for loop would look something like
n = 42;
for j = 0:n
% update C
end
As for when it is bigger than 0 what is it?
error is :Unbalanced or unexpected parenthesis or bracket.
in this line : 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) and 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 exmplae when it become -20 then put C=0 and for example when C=20 put C=20 and make no changes thank you
maybe for help the code is :
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)
also I made another post HERE
you also can help me there
thank you
The error message is telling you that there is a problem with the brackets in your line of code - possibly too many. You may want something like
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)
Please verify which are the inputs to your symsum function call.

Sign in to comment.

Categories

Asked:

on 30 Apr 2018

Commented:

on 5 May 2018

Community Treasure Hunt

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

Start Hunting!