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

 Réponse acceptée

Jan
Jan le 29 Avr 2021
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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by