필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Number of elements must be the same?

조회 수: 2 (최근 30일)
Phil Whitfield
Phil Whitfield 2018년 5월 16일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (2개)

Steven Lord
Steven Lord 2018년 5월 16일
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 2018년 5월 16일
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.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by