Info

この質問は閉じられています。 編集または回答するには再度開いてください。

SCRIPT FILE RUNNING ERROR

2 ビュー (過去 30 日間)
Merve Koç
Merve Koç 2016 年 5 月 7 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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

回答 (1 件)

Weird Rando
Weird Rando 2016 年 5 月 7 日
編集済み: Weird Rando 2016 年 5 月 7 日
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 件のコメント
Weird Rando
Weird Rando 2016 年 5 月 7 日
編集済み: Weird Rando 2016 年 5 月 7 日
I look into this and found some mistakes. The initial guesses f(a) and f(b) must have opposite signs. And your if statement changed the wrong variable. I also change your while loop to a for loop because f(m) may have positive and negative values. (https://en.wikipedia.org/wiki/Bisection_method)
%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;
for i = 1:15
if f(m)<0;
a=m;
else
b=m;
end
m=(a+b)/2;
end
m
f(m)
Weird Rando
Weird Rando 2016 年 5 月 8 日
編集済み: Weird Rando 2016 年 5 月 8 日
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)

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by