主程序为:%计算方法上机第八题clc;clear;symsxaff=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);ifs1*s2>0disp('Thereisnorootintherangeof[2,3].');elsewhile(l2-l1)>e3%二分法确定包含解的小区间l3=(l1+l2)/2;f3=subs(f,x,l3);s3=romberg(t1,t2,f3,e1);ifs1*s3<0l2=l3;s2=s3;elsel1=l3;s1=s3;endenddisp(['Therootisintherangeof[',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;enddisp(['Theapproximatesolutionis:',num2str(l2)]);%输出解end
二分法和割线法求非线性方程的解 来自淘豆网m.daumloan.com转载请标明出处.