Change a variable till a condition is met

Hi, how can i say to matlab to change the variable x till the condition varptf=0 is met? Note that y is (1-x). I did it on excel by analysis tool solver (the solution is rougly x=0.65), idk how to do this on matlab (and for an assignment I have to do this on matlab). Thank you for your time
% point 1
SDa = 0.08 % std of A
SDa = 0.0800
SDb = 0.15 % std of B
SDb = 0.1500
x = 0 % weight of A
x = 0
y = 1-x % weight of B
y = 1
p = -1 % correlation coefficient
p = -1
varptf = x^2*SDa^2+y^2*SDb^2+2*x*y*p*SDa*SDb
varptf = 0.0225
while varptf == 0
x = x+0.01
end
x
x = 0

 Akzeptierte Antwort

Voss
Voss am 20 Okt. 2022
% point 1
SDa = 0.08 % std of A
SDa = 0.0800
SDb = 0.15 % std of B
SDb = 0.1500
x = 0 % weight of A
x = 0
y = 1-x % weight of B
y = 1
p = -1 % correlation coefficient
p = -1
varptf = x^2*SDa^2+y^2*SDb^2+2*x*y*p*SDa*SDb
varptf = 0.0225
while abs(varptf) >= 1e-6
x = x+0.01;
y = 1-x;
varptf = x^2*SDa^2+y^2*SDb^2+2*x*y*p*SDa*SDb;
end
x
x = 0.6500

2 Kommentare

Luca
Luca am 20 Okt. 2022
Thank you very much !!
Voss
Voss am 26 Okt. 2022
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David Hill
David Hill am 20 Okt. 2022
SDa = 0.08;
SDb = 0.15;
x = 0;
y = 1-x;
p = -1;
varptf =@(x,y)x^2*SDa^2+y^2*SDb^2+2*x*y*p*SDa*SDb;
v=varptf(x,y);
while v>1e-10%~=0 is problematic (unlikely it is going to exactly equal zero)
x=x+.0001;%change additional amount for more accuracy
y=1-x;
v=varptf(x,y);
end
x
x = 0.6522

Kategorien

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

Tags

Gefragt:

am 20 Okt. 2022

Kommentiert:

am 26 Okt. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by