How do I pass a vector into a user-defined function?
11 views (last 30 days)
Show older comments
I am new to MATLAB and just started writing some preliminary functions, and I would like to knowhow to pass a vector as an argument (some of the arguments or all of them).
function [cprice] = cprice(spot, strike, ir, div, ndays,vol)
%Calculates BlackScholes price for Call Options
tir = ndays / 365;
dis = exp(-ir * tir);
F = spot * exp((ir - div) * tir);
d1 = (log(F / strike) + (((vol ^ 2) / 2) * tir)) / (vol * (tir ^ 0.5));
d2 = d1 - (vol * (tir ^ 0.5));
cprice = dis * ((normcdf(d1) * F) - (normcdf(d2) * strike))
0 Comments
Accepted Answer
Andrew Newell
on 13 Mar 2011
It looks like all you need to do is replace each * by .*, each ^ by .^ and each / by ./
0 Comments
More Answers (1)
Oleg Komarov
on 13 Mar 2011
You have to use:
- ./
- .*
- .^
whenever the operation involves TWO arrays of the same dimension to obtain elementwise division, multiplication or power.
Oleg
0 Comments
See Also
Categories
Find more on Digital Filtering in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!