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 日

1 投票

% 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 日

0 投票

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 ExchangeLoops 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