用二分法和割线法求非线性方程cos(x??01?sin?)d??0在[2,3]中
的解,使误差不超过10?4, 可按如下方法进行:()用区间法确定一个包含解的小区间;1(2)用割线法求该方程在此区间中的根。
主程序为:
%计算方法上机第八题 clc;clear; syms x a f
f=cos(x*sin(a)); %定义积分函数
t1=0;t2=pi;l1=2;l2=3;e1=10^-5;e2=10^-4;e3=10^-3; f1=subs(f,x,l1);f2=subs(f,x,l2);
s1=romberg(t1,t2,f1,e1); %调用Romberg积分函数 s2=romberg(t1,t2,f2,e1); if s1*s2>0
disp('There is no root in the range of [2,3].'); else
while (l2-l1)>e3 %二分法确定包含解的小区间 l3=(l1+l2)/2; f3=subs(f,x,l3);
s3=romberg(t1,t2,f3,e1); if s1*s3<0
l2=l3;s2=s3; else
l1=l3;s1=s3; end end
disp(['The root is in the range of [',num2str(l1),',',num2str(l2),'].']); while (l2-l1)>e2 %割线法确定具体的解 l3=l2;
l2=l2-(l2-l1)*s2/(s2-s1); f2=subs(f,x,l2);
s2=romberg(t1,t2,f2,e1); l1=l3;s1=s2; end
disp(['The approximate solution is:',num2str(l2)]); %输出解 end