非线性方程求程--二分法.docVIP

  • 28
  • 0
  • 约1.89千字
  • 约 4页
  • 2018-05-24 发布于河南
  • 举报
非线性方程求程--二分法

非线性方程求程--二分法 1、实验目的: 理解二分算法原理, 学会编写该算法的Matlab程序, 并能用该算法求解非线性方程。 2、实验内容与要求: 二分法原理   根据根的存在性定理:如果一个函数f(x)在区间[a,b]上连续, 且f(a)*f(b) 0, 则函数f(x)在区间[a,b]上至少有一个实根。   二分法就是根据此定理, 将原始区间不断二分, 从而将区间不断缩小, 即将方程根的范围不断缩小。在此过程中, 要求算法每一步所产生的区间满足上述定理条件, 即在每个保留下来的区间上要求函数在区间两端点处的函数值异号。具体算法如下:   Setp 0:初始化变量a、b、k = 0、f(x)、δ和ε。   Step 1:如果f(a)*f(b) 0, 则退出。否则goto Step 2。   Step 2:取[a,b]中点c =(a+b)/2, k = k+1; 如果f(c) = 0, 由c就是根, goto Step 3。 如果f(a)*f(c) 0, b = c。 如果f(c)*f(b) 0, a = c。 如果|b-a| δ或者|f(c)| ε, 则goto Step 3; 否则goto Step 2。 Step 3:输出结果。 程序说明与要求 程序输入参数为区间左右端点、误差控制和求根函数;输出参数为方程的近似根及方程在此根处的函数值。要求输入区间内包括方程的根。 二分法程序与实例 参考程序 function [c,err,yc,k]=bisect(f,a,b,delta) % Input -f is the function input as a string f % -a and b are the left and the right end points. % -delta is the tolerance %Output -c is a root of the equation f(x)=0 % -yc=f(c) % -err is the error estimate for c % - k is the number of iterations. ya=feval(f,a); yb=feval(f,b); if ya*yb0 error(Note:f(a)*f(b)0); end max1=1+round((log(b-a)-log(delta))/log(2)); for k=1:max1 c=(a+b)/2; yc=feval(f,c); if yc==0 a=c; b=c; elseif yb*yc0 b=c; yb=yc; else a=c; ya=yc; end if b-a 2*delta break end end c=(a+b)/2; err=abs(b-a)/2; yc=feval(f,c); 数值实例 数值实例 function Bisect_main( ) a=1; b=1.5; delta=1e-3; clc; [x,err,y]=bisect(@f,a,b,delta) ezplot(x^3-x-1,[-2 2]); grid; function y=f(x) y=x^3-x-1; return 运行结果: x = 1.3252 err = 9.7656e-004 y = 0.0020 3、练习、思考与应用 已知方程, 请编写程序, 先画出此函数图像, 并用二分法求此方程实根的近似值, 使误差不超过0.0001。注意先用相应内容求出该方程的有根区间。 function Bisect_main( ) a=-9; b=9; delta=1e-4; clc; [x,err,y]=bisect(@f,a,b,delta) ezplot(x^3+1.1*x^2+0.9*x-1.4,[-2 2]); grid; function y=f(x) y=x^3+1.1*x^2+0.9*x-1.4; return 运行结果: x =0.6706 err = 6.8665e-005 y =-3.9643e-005

文档评论(0)

1亿VIP精品文档

相关文档