How to write a code which can decrease the range of the answer until we get the right value?

1 回表示 (過去 30 日間)
yang-En Hsiao
yang-En Hsiao 2019 年 10 月 6 日
コメント済み: darova 2019 年 10 月 6 日
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 件のコメント
darova
darova 2019 年 10 月 6 日
Maybe you can find some more information if you will search for bisection method

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by