How to repeat a command until a condition is met?

Here is my part of my code thus far. How would I decrease value of Df until v is approximately 70?
Df=100;
V=exp(-r*T)*min(Df*x_non_us(m+1,:),S_us(m+1,:));
v=mean(V);
while
if abs(v-70)>.1
Df=Df-.05;
else
Df=Df;
end
end
Df

 採用された回答

Jan
Jan 2021 年 4 月 29 日

1 投票

Omit expressions like:
Df = Df;
This is confusing only.
As far as I understand, you want a loop over this piece of the code:
Df = 100;
ready = false;
c = exp(-r * T); % Move all repeated expensive
v1 = x_non_us(m + 1, :); % calculations out of the loop
v2 = S_us(m + 1, :);
iter = 0;
while ~ready
V = c * min(Df * v1, v2);
if abs(mean(V) - 70) > 0.1
Df = Df - 0.05;
else
ready = true;
end
% Security limit:
iter = iter + 1;
if iter > 1e6
error('Cannot find Df')
end
end
A binary search between known limits would be more efficient.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2021 年 4 月 29 日

編集済み:

2021 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by