Info

This question is closed. Reopen it to edit or answer.

SCRIPT FILE RUNNING ERROR

1 view (last 30 days)
Merve Koç
Merve Koç on 7 May 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
When I run the script file, a can not receive any command on Matlab either any error. However the program stars and runs smoothly. Please help.
The code was:
%bisection method
f = inline('x.^3-0.165.*x.^2+3.993*10^-4');
x= -5.5:0.1:5.5;
y=f(x);
plot(x,y)
grid %from the plot we can see that we have a root between -1 and 1.5 but the height x can not be negative.
%We know that we have a root between 1 and 2(initial guesses)
a=1;
b=2;
m=(a+b)/2;
while abs(f(m))>1e-8
if f(a)*f(m)>0;
a=m;
else
b=m;
end
m=(a+b)/2;
end
m

Answers (1)

Weird Rando
Weird Rando on 7 May 2016
Edited: Weird Rando on 7 May 2016
I ran your code and your stuck in an infinite loop. The f(m) doesn't changes value. Because the variable m doesn't change after it becomes a 2.
  3 Comments
Weird Rando
Weird Rando on 8 May 2016
And here is the while loop
%bisection method
%f = inline('x.^3-x-2');
f = inline('x.^3-0.165.*x.^2+3.993*10^-4');
x= -5.5:0.1:5.5;
y=f(x);
plot(x,y)
grid %from the plot we can see that we have a root between -1 and 1.5 but the height x can not be negative.
%We know that we have a root between 1 and 2(initial guesses)
a=-1; %f(a) must be negative
b=2; %f(b) must be positive
m=(a+b)/2;
while abs(f(m)) > 1*10^-8
if f(m)<0;
a=m;
else
b=m;
end
m=(a+b)/2;
end
m
f(m)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by