How to write a code which can decrease the range of the answer until we get the right value?
1 次查看(过去 30 天)
显示 更早的评论
I want to write a code which can help me decrease the range of answer until i find it.For example,S=0:0.001:1;and i have to find a value which its square will be the 0.09,that is,x^2=0.09,and the answer is 0.3
Now,if every time i guess median,that is ,
First time i guess "0.5",but 0.5^2=0.25 ,not 0.09,so the range become S=0:0.001:0.5
Second time i guess "0.25",but 0.25^2=0.0625 ,not 0.09.so the range become S=0.25:0.001:0.5
Third time i guess (0.25+0.5)/2=0.375,but 0.375^2=0.140625 ,not 0.09,so the range become S=0.25:0.001:0.375
So the range will keep decreasing until i find the answer,and one of upper bound and lower bound of the range will be my wrong number which i just guess.but i don't know how to modify the code
My original code
%first time
S=0:0.001:1;
x=0.5;
y=x^2;%not 0.09
%Second time
S=0:0.001:x
q=0.25;
y=q^2;%not 0.09
%Third time
S=q:0.001:x
w=0.375;
y=w^2%not 0.09
%Fourth time
S=q:0.001:w %S=0.25:0.001:0.375
e=0.3125;
y=e^2 %not 0.09
%Fifth time
S=e:0.001:w;
r=0.34;
y=r^2 %not 0.09
.
.
.
until i found that y=0.09,so i can know the answer,however,this code is too long,i want to write a while or for loop to find the answer,does anyone know how to modify my code?
1 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!