error running the code in while command in bisection method

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

 采纳的回答

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 个评论

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
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 Exchange 中查找有关 Function Creation 的更多信息

产品

版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by