Matlab fplot not working

11 views (last 30 days)
Raymond Pistonetti
Raymond Pistonetti on 24 Oct 2020
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

Answers (1)

Walter Roberson
Walter Roberson on 25 Oct 2020
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 Comments
Walter Roberson
Walter Roberson on 25 Oct 2020
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 on 25 Oct 2020
Thank you! Your advice with the arrayfun solved my issue!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!