Matlab fplot not working

조회 수: 14 (최근 30일)
Raymond Pistonetti
Raymond Pistonetti 2020년 10월 24일
댓글: Raymond Pistonetti 2020년 10월 25일
I'm new to matlab, and I'm trying to write a function that will compute the value of an option using black scholes and monte carlo. However when I try and graph the output of the what the function returns vs the input (option price vs number of simulation in monte carlo) using this command:
fh = @solv;
fplot(fh,[1 10000])
I get this error:
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an
output with the same size and shape as the input arguments.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function/FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine.set.Function_I
In matlab.graphics.function.FunctionLine.set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 245)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 200)
In fplot>vectorizeFplot (line 200)
In fplot (line 166)
My code is below:
function v = solv(x)
S = 28;
r = .04;
o = .25;
T = .5;
K = 30;
Z = randn(1,x);
val2 = [];
pickmax = [];
val3 = exp(-1.*r.*T);
for i=1:x
% return a value
val = (S.*exp((r-((o.^2)./2)).*T - (o.*sqrt(T).*Z(i)))) - K;
pickmax = [pickmax, val];
pickmax = [pickmax, 0];
% pick between the max of the value or 0
m = max(pickmax);
% empty vector
pickmax = [];
% get option price
val4 = val3.*m;
% insert into an array
val2 = [val2, val4];
end
% the above gives us our Xj as a vector called val2
% now we compute running avg
summer = 0;
for h=1:x
summer = summer + val2(h);
end
runavg = (1./x).*summer;
v = runavg;
end

답변 (1개)

Walter Roberson
Walter Roberson 2020년 10월 25일
randn(1,x)
is going to error out when x is not an integer.
When you use fplot you need to accept a vector of continuous input values that are in the plot range. fplot will never try to restrict to integers, and would instead spend a long time figuring out all the places that the function was discontinuous in real inputs.
Emphasis on vector of input values. If your function cannot handle a vector then what you pass to fplot should be the handle to something that does arrayfun to the real code.
  댓글 수: 7
Walter Roberson
Walter Roberson 2020년 10월 25일
You could be reorganizing your function completely.
Run one big montecarlo loop once but store the current result at each timestep that you intend to plot. Do the appropriate calculation on each recorded value, and then plot.
Otherwise, if you instead run with a certain number of random tries, and then run again with a larger number of random tries, then you are not measuring how additional tries refines the values: you are instead comparing different runs and you have to face the hypothesis that the one with more steps might have been better "already" by the shared number of steps.
Do not use fplot for any of this, not even if you decide that re-running with the different number of steps is acceptible: in that case you would either round(linspace()) or use the colon operator to get a list of round lengths to evaluate for.
Raymond Pistonetti
Raymond Pistonetti 2020년 10월 25일
Thank you! Your advice with the arrayfun solved my issue!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by