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

 Akzeptierte Antwort

Jan
Jan am 29 Apr. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 29 Apr. 2021

Bearbeitet:

am 29 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by