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

 Accepted Answer

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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 29 Apr 2021

Edited:

on 29 Apr 2021

Community Treasure Hunt

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

Start Hunting!