필터 지우기
필터 지우기

How do I pass a vector into a user-defined function?

조회 수: 4 (최근 30일)
KKR
KKR 2011년 3월 13일
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))

채택된 답변

Andrew Newell
Andrew Newell 2011년 3월 13일
It looks like all you need to do is replace each * by .*, each ^ by .^ and each / by ./

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2011년 3월 13일
You have to use:
  • ./
  • .*
  • .^
whenever the operation involves TWO arrays of the same dimension to obtain elementwise division, multiplication or power.
For details look at arithmetic operators
Oleg

카테고리

Help CenterFile Exchange에서 Formula Manipulation and Simplification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by