Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Number of elements must be the same?

1 vue (au cours des 30 derniers jours)
Phil Whitfield
Phil Whitfield le 16 Mai 2018
Clôturé : MATLAB Answer Bot le 20 Août 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

Réponses (2)

Steven Lord
Steven Lord le 16 Mai 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 le 16 Mai 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.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by