umerfunction='x.^3';
x_lower=0;
x_upper=1;
x_mid=(x_lower+x_upper)/2;
i=0;
while(abs(umerfunction(x_mid))>0.0001)
fx=umerfunction(x_mid);
X=[i,fx,x_upper,x_lower];
disp X;
if(umerfunction(x_mid)*umerfunction(x_upper))<0
x_lower=x_mid;
else x_upper=x_mid;
end
ERROR:
Array indices must be positive integers or logical values.
Error in bisection (line 7)
while(abs(umerfunction(x_mid))>0.0001)
how to solve that issue? i m very frustated now regarding solving this.Thanks and honours in advance

 採用された回答

madhan ravi
madhan ravi 2018 年 12 月 22 日

0 投票

umerfunction=@(x)x.^3;
Note: Also your loop is infinite loop since each time the condition will be satisfied either you need to add a break or change the condition.

2 件のコメント

umer ansar
umer ansar 2018 年 12 月 22 日
thanks dear madhan. Your correction has recently solved my problem. but the loop ran infinitely as u pointed out. what can be the solution to make it adequntly finite?
note:i have checked from calculator that root is 3.57 so i have updated my lower and upper bounds to 3 and 4 respectively.i have been mentioned in my assignment question that the stopping criteria(accuracy of the root after which we should stop iterating) is 0.01.
thanks in advance and return
madhan ravi
madhan ravi 2018 年 12 月 23 日
Anytime :) , before going in depth the first line doesn't seem to work as you think though :
x_mid=(x_lower+x_upper)/2; => ( 0 + 1 ) / 2 => 0.5 , ( 0.5 ) ^ 3 => 0.125
or
( 3 + 4 ) / 2 => ( 3 + 4 ) / 2 => 3.5 , ( 3.5 ) ^ 3 => 42.8750
so what do we observe from the above both the bounds that you try are always greater than 0.0001 , you need to rethink about your formula plus you don't seem to iterate x_mid over and over forever so basically the result becomes useless..

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

製品

リリース

R2015a

質問済み:

2018 年 12 月 22 日

コメント済み:

2018 年 12 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by