Info

This question is closed. Reopen it to edit or answer.

Number of elements must be the same?

1 view (last 30 days)
Phil Whitfield
Phil Whitfield on 16 May 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
function Sf = FreeBoundary(S,t,V,K)
Sf = zeros(1,length(t));
eps_star = K*1e-5;
for j = 1:length(t)
Sf(j) = S(find(abs(V(:,j)-K+S)< eps_star, 1, 'last'));
end
end
I am trying to calculate an american put free boundary before plotting it on my graph. However when I use :
FreeBoundary(1:120,1/3,0.25,60)
I keep getting the error
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in FreeBoundary (line 7) Sf(j) = S(find(abs(V(:,j)-K+S)< eps_star, 1, 'last'));
Any chance you know how to fix it? Thanks

Answers (2)

Steven Lord
Steven Lord on 16 May 2018
Are you certain that at each iteration of your for loop at least one element in abs(V(:,j)-K+S) is strictly less than eps_star?
Did you intend to subtract K and add S to V(:, j) or did you mean to subtract the quantity (K+S) from V(:, j)? In the latter case, I would write abs(V(:, j)-(K+S)).

Jan
Jan on 16 May 2018
Sf = NaN(1,length(t));
for j = 1:length(t)
index = find(abs(V(:,j)-K+S)< eps_star, 1, 'last');
if ~isempty(index)
Sf(j) = S(index);
end
end
Now the value of S remains NaN, if no matching values are found.

This question is closed.

Community Treasure Hunt

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

Start Hunting!