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

 Accepted Answer

% 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

More Answers (1)

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

Tags

Asked:

on 20 Oct 2022

Commented:

on 26 Oct 2022

Community Treasure Hunt

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

Start Hunting!