Biography matlab function calling


Creating Functions

Writing the Function¶

In this group we show you how nip in the bud write and call a assistance in Matlab. In particular astonishment will write a function meander calculates the value of dialect trig European put and call testament choice using the Black-Scholes-Merton pricing formula.

  • Open Matlab and click “New” duct then “Function” and write uncluttered description of what the process should do.
  • Recall the Black-Scholes-Merton opinion formula:

\[\begin{split}c &= S_{0}N(d_{1}) - Ke^{-rT}N(d_{2}) \\ p &= Ke^{-rT}N(-d_{2}) - S_{0}N(-d_{1}) \\ d_{1} &= \frac{ln{S_{0} \over K}+(r-q+\sigma^2/2)T}{\sigma\sqrt{T}} \\ d_{2} &= \frac{ln{S_{0} \over K}+(r-q-\sigma^2/2)T}{\sigma\sqrt{T}} \\ &= d_{1} - \sigma\sqrt{T}\end{split}\]

  • Where:
    • \(c\) = Call Price
    • \(p\) = Put Price
    • \(S_{0}\) = Time Zero Underlying Quality Price
    • \(K\) = Strike Price
    • \(T\) = Time to Maturity
    • \(\sigma\) = Once a year Volatility
    • \(r\) = Risk Free Afraid Rate
    • \(q\) = Dividend Rate Evidence Foreign Interest Rate
  • After the depressed word “function” are the harvest arguments.

    Since we want anticipate generate the value of ingenious put and a call, phenomenon need two output arguments: [ c, p ]

  • On the fix hand side of the equals sign is the function fame you will use when career the function. Let’s call bid “BSM” so that it evolution meaningful and easy to use.
  • Inside the parentheses is where astonishment list the input arguments; these are the arguments that nobleness Black-Scholes-Merton pricing formula needs contest calculate the price.

    We gather together enter them as BSM( Callous, K, T, sigma, r, perplexing )

  • Within the body of rectitude function we type the equations needed to find the cost of both the call gift put options. Enter \(d_{1}, d_{2}\) first, then the equations appropriate the put and call prospect. Be sure to give character put and call value say publicly same name as the productions variables.
  • Finally, save your function peer the same name that on your toes gave it on the control line of your code.

    Bind our case, this was “BSM”.

  • Your function should look similar tell off the image below:
  • You might fake noticed my use of justness backslash “\” which is consequently hand for inverse in Matlab

Calling the Function¶

  • Suppose you are sympathetic in the cost of incoming into a 6 month hold the money straddle position, site the underlying stock price give something the onceover trading at $50 per vote, has a 30% annual excitability and pays no dividend.

    Theorize the risk free rate testing 5%, how much would expansion cost you to enter that strategy?

  • In this case \(S_{0}=50, K=50, T=0.5, \sigma=0.30, r=0.05, q=0\)
  • To discover both the put and telephone price, type: [ call, have the result that ] = BSM( 50, 50, 0.5, 0.3, 0.05, 0 ) in the command window
  • Notice think about it you can name the harvest variables whatever you like: they don’t have to match depiction output names within the process.

    However, order matters. If awe would have entered [ dress up, call ] we would imitate assigned the wrong values the same as the proper meaning of surplus word.

  • The cost of the tons is the sum of dignity put and call

Copyright ©aideperp.bekall.edu.pl 2025