Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Number of elements must be the same?

2 visualizzazioni (ultimi 30 giorni)
Phil Whitfield
Phil Whitfield il 16 Mag 2018
Chiuso: MATLAB Answer Bot il 20 Ago 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

Risposte (2)

Steven Lord
Steven Lord il 16 Mag 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 il 16 Mag 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.

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by