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

 채택된 답변

Voss
Voss 2022년 10월 20일
% 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

Luca
Luca 2022년 10월 20일
Thank you very much !!
Voss
Voss 2022년 10월 26일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
David Hill 2022년 10월 20일
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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2022년 10월 20일

댓글:

2022년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by