有没有二分法解非线性方程的MATLAB程序

来源:百度知道 编辑:UC知道 时间:2024/05/29 17:07:32
要求的是f(x)=0在区间[a,b]上的根

clc;clear
a=0;b=1;
fa=1-a-sin(a);
fb=1-b-sin(b);
c=(a+b)/2;
fc=1-c-sin(c);
if fa*fb>0,break,end
while abs(fc)>0.5*10^(-4)
c=(a+b)/2;
fc=1-c-sin(c);
if fb*fc>0
b=c;
fb=fc;
else
a=c;
fa=fc;
end
end
format long
fx=fc,x=c

结果:
fx =

-2.414986223420179e-005

x =

0.510986328125000
精确解:
>> x=solve('1-x-sin(x)')

x =

.51097342938856910952001397114508