《数值分析课程设计》
报 告
学号: 学生姓名:姚梅 指导教师:缪红益
一、 题目:
二分法求方程根
用二分法求:
(1).x*x-exp(x)=0 在x0=1 附近的根;
(2) x*exp(x)-1=0 在x0=1 附近的根;
(3)log10(x)+x-2=0 在x0=2 附近的根;
二、理论
应用二分法求方程根主要应用在讨论单变量非线性方程f(x)=0的求根问题。若f(x) c[a,b]且f(a)f(b)<0,根据连续函数性质可知,f
(x)=0在(a,b)内至少有一个实根,这时称[a,b]为方程的有根区间。通常可通过逐次搜索法求得方程的有根区间。考察有根区间[a,b],取中点x0=(a+b)/2将它分为两半,假设中点x0不是f(x)的零点,然后进行根的搜索,即检查f(x0)与f(a)是否同号,如果确系同号,说明所求的根x*在x0的右侧,这时令a1=x0,b1=b;否则x*在x0的左侧,这时令a1=a,b1=x0。不管出现哪种情况,新的有根区间[a1,b1]的长度仅为[a,b]的一半。对压缩了的有根区间[a1,b1]又可施行同样的手续,即用中点x1=(a1+b1)/2将区间[a1,b1]在分为两半,然后通过根的搜索判定所求的根在x1的哪一侧,从而又确定一个新的有根区间
[a2,b2],其长度是[a1,b1]的一半。如此反复下去,即可得出一系列有根区间[a,b] [a1,b1] [a2,b2] 。。。 [ak,bk] 。。。,其中每个区间都是前一个区间的一半,因此[ak,bk]的长度bk-ak=(b-a)/2k 。当k 时趋于零,就是说,如果二分过程无限的进行下去,这些区间最终必缩于一个点x*,该点显然就是所求的根。
三、方法、算法与程序设计
1. 用二分法求方程 f(x)=0的根 x*的近似值 x* 的步骤
步骤1. 若对于a<b, 有f(a)f(b)<0, 则在(a, b)内f(x)=0至少有一个根.
步骤2. 取a, b的中点x1=(a+b)/2计算f(x1)
步骤3. 若f(x1)=0,则x1是f(x)=0的根, 停止计算,运行后输出结果x*=x1.
f(a)f(x1)<0,则在(a, x1 )内f(x)=0至少有一个根,取a1=a, b1=x1;
f(a)f(x1)>0,则取a1=x1, b1=b;
步骤4. 若|bk-ak|/2??,退出计算, 运行后输出结果x*?(ak+bk)/2,反之, 返回步
骤1, 重复步骤1,2,3.
2. 二分法的matlab主程序算法
求解方程f(x)=0在开区间(a,b)内的一个根的前提条件是f(x)在闭区间[a,b]上连续, 且f(a)f(b)<0
输入的量: a和b是闭区间[a,b]的左右端点, abtol是预先给定的精度.
运行后输出的量: k是使用二分法的次数. x是方程在(a,b)内的实根x*的近似值, 其精度是abtol.
wuca=|bk-ak|/2是使用k次二分法所得到的小区间的长度的一半, 即实根x*的近似值x的绝对精度限, 满足wuca≤abtol. yx=f(xk), 即方程f(x)=0在实根x*的近似值x处的函数值.
3.二分法的matlab主程序
function [k,x,wuca,yx]=erfen(a,b,abtol)
a(1)=a; b(1)=b;
ya=fun(a(1)); yb=fun(b(1)); %程序中调用的fun.m 为函数
if ya* yb>0,
disp('注意:ya*yb>0,请重新调整区间端点a和b.'), return
end
max1=-1+ceil((log(b-a)- log(abtol))/ log(2));
for k=1: max1+1
a;ya=fun(a); b;yb=fun(b); x=(a+b)/2;
yx=fun(x); wuca=abs(b-a)/2; k=k-1;
[k,a,b,x,wuca,ya,yb,yx]
if yx==0
a=x; b=x;
elseif yb*yx>0
b=x;yb=yx;
else
a=x; ya=yx;
end
if b-a< abtol , return, end
end
k=max1; x; wuca; yx=fun(x
4. 用二分法求解方程f(x)=0在 (a,b)内的近似根的步骤
步骤1. 建立名为fun.m的M文件如: function y1=fun(x), y1=f(x);
步骤2. 将二分法的主程序保存名为erfen.m的M文件;
步骤3. 在matlab工作窗口输入程序: >>[k, x, wuca, yx]=erfen(a, b, abtol)
其中输入的量: 区间端点的值a, b和精度是abtol都是具体给定的数值, 然后按运行键. 运行后输出计算次数k、使用k次二分法所得到的小区间
[ak, bk]的中点的值x和它的函数值y(x)及wuca=|bk-ak|/2.
四、算例、应用实例
ans =
Columns 1 through8
0 -1.0000 1.5000 0.2500 1.2500 0.6321 -2.2317 -1.2215 ans =
Columns 1 through 8
1.0000 -1.0000 0.2500 -0.3750 0.6250 0.6321 -1.2215 -0.5467 ans =
Columns 1 through 8
2.0000 -1.0000 -0.3750 -0.6875 0.3125 0.6321 -0.5467 -0.0302 ans =
Columns 1 through 8
3.0000 -1.0000 -0.6875 -0.8438 0.1563 0.6321 -0.0302 0.2818 ans =
Columns 1 through 8
4.0000 -0.8438 -0.6875 -0.7656 0.0781 0.2818 -0.0302 0.1211 ans =
Columns 1 through 8
5.0000 -0.7656 -0.6875 -0.7266 0.0391 0.1211 -0.0302 0.0443
ans =
Columns 1 through8
6.0000 -0.7266 -0.6875 -0.7070 0.0195 0.0443 -0.0302 0.0068 ans =
Columns 1 through 8
7.0000 -0.7070 -0.6875 -0.6973 0.0098 0.0068 -0.0302 -0.0118 ans =
Columns 1 through 8
8.0000 -0.7070 -0.6973 -0.7021 0.0049 0.0068 -0.0118 -0.0025 ans =
Columns 1 through 8
9.0000 -0.7070 -0.7021 -0.7046 0.0024 0.006 -0.0025 0.0021 ans =
Columns 1 through 8
10.0000 -0.7046 -0.7021 -0.7034 0.0012 0.0021 -0.0025 -0.0002 ans =
Columns 1 through 8
11.0000 -0.7046 -0.7034 -0.7040 0.0006 0.0021 -0.0002 0.0010 ans =
Columns 1 through 8
12.0000 -0.7040 -0.7034 -0.7037 0.0003 0.0010 -0.0002 0.0004 k = 12
x = -0.7037
wuca = 3.0518e-004
yx = 3.9350e-004
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 ans =
Columns 1 through 8
0 0 1.5000 0.7500 0.7500 -1.0000 5.7225 0.5878
ans =
Columns 1 through 8
1.000 0 0.7500 0.3750 0.3750 -1.0000 0.5878 -0.4544 ans =
Columns 1 through 8
2.0000 0.3750 0.7500 0.5625 0.1875 -0.4544 0.5878 -0.0128 ans =
Columns 1 through 8
3.0000 0.5625 0.7500 0.6563 0.0938 -0.0128 0.5878 0.2650 ans =
Columns 1 through8
4.0000 0.5625 0.6563 0.6094 0.0469 -0.0128 0.2650 0.1208 ans =
Columns 1 through 8
5.0000 0.5625 0.6094 0.5859 0.0234 -0.0128 0.1208 0.0527
ans =
Columns 1 through 8
6.0000 0.5625 0.5859 0.5742 0.0117 -0.0128 0.0527 0.0197 ans =
Columns 1 through8
7.0000 0.5625 0.5742 0.5684 0.0059 -0.0128 0.0197 0.0034 ans =
Columns 1 through 8
8.0000 0.5625 0.5684 0.5654 0.0029 -0.0128 0.0034 -0.0047 ans =
Columns 1 through 8
9.0000 0.5654 0.5684 0.5669 0.0015 -0.0047 0.0034 -0.0007 ans =
Columns 1 through 8
10.0000 0.5669 0.5684 0.5676 0.0007 -0.0007 0.0034 0.0013 ans =
Columns 1 through 8
11.0000 0.5669 0.5676 0.5673 0.0004 -0.0007 0.0013 0.0003 k =11
x = 0.5673
wuca =3.6621e-004
yx =3.2458e-004
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 ans =
Columns 1 through 8
0 1.5000 2.5000 2.0000 0.5000 -0.3239 0.8979 0.3010 ans =
Columns 1 through 8
1.0000 1.5000 2.0000 1.7500 0.2500 -0.3239 0.3010 -0.0070 ans =
Columns 1 through 8
2.0000 1.7500 2.0000 1.8750 0.1250 -0.0070 0.3010 0.1480 ans =
Columns 1 through8
3.0000 1.7500 1.8750 1.8125 0.0625 -0.0070 0.1480 0.0708 ans =
Columns 1 through 8
4.0000 1.7500 1.8125 1.7813 0.0313 -0.0070 0.0708 0.0320 ans =
Columns 1 through 8
5.0000 1.7500 1.7813 1.7656 0.0156 -0.0070 0.0320 0.0125 ans =
Columns 1 through 8
6.0000 1.7500 1.7656 1.7578 0.0078 -0.0070 0.0125 0.0028
ans =
Columns 1 through 8
7.0000 1.7500 1.7578 1.7539 0.0039 -0.0070 0.0028 -0.0021 ans =
Columns 1 through 8
8.0000 1.7539 1.7578 1.7559 0.0020 -0.0021 0.0028 0.0003 ans =
Columns 1 through 8
9.0000 1.7539 1.7559 1.7549 0.0010 -0.0021 0.0003 -0.0009 ans =
Columns 1 through 8
10.0000 1.7549 1.7559 1.7554 0.0005 -0.0009 0.0003 -0.0003 k = 10
x = 1.7554
wuca =4.8828e-004
yx = -2.5996e-004
五、参考文献
1. 数值分析,李庆扬,王能超,易大义,2001,清华大学出版社(第四版)。
2. 数值方法,关治,陆金甫,2006,清华大学出版社。
3. 数值分析与实验学习指导,蔡大用,2001,清华大学出版社。
六、附录
function [k,x,wuca,yx]=erfen(a,b,abtol)
a(1)=a; b(1)=b;
ya=fun(a(1)); yb=fun(b(1)); %程序中调用的fun.m 为函数
if ya* yb>0,
disp('注意:ya*yb>0,请重新调整区间端点a和b.'), return
end
max1=-1+ceil((log(b-a)- log(abtol))/ log(2));
for k=1: max1+1
a;ya=fun(a); b;yb=fun(b); x=(a+b)/2;
yx=fun(x); wuca=abs(b-a)/2; k=k-1;
[k,a,b,x,wuca,ya,yb,yx]
if yx==0
a=x; b=x;
elseif yb*yx>0
b=x;yb=yx;
else
a=x; ya=yx;
end
if b-a< abtol , return, end
end
k=max1; x; wuca; yx=fun(x);
end
第二篇:数值分析课程设计:最佳平方逼近与最小二乘拟合
数值分析课程设计:最佳平方逼近与最小二乘拟合
给出函数f(x)=1/(1+25x^2)
一:
求f(x)在[-1,1]上的三次最佳平方逼近多项式以及均方误差。
Matlab编程代码如下:
syms x;
p=[1 x (1./2)*(3*x^2-1) (1./2)*(5*x^3-3*x) 1./(1+25*x^2)];
for i =1:1:4
jf = int((p(i)*p(5)),x,-1,1);
a(i)=(2*(i-1)+1)/2*jf;
end
a
f3=0;
for i = 1:1:4
f3= f3+a(i)*p(i);
end
simplify(f3)
f3
syms x
fun=(f(x)-f3)^2;
int(fun,x,-1,1)
输出结果为
a =
[ 1/5*atan(5), 0, 3/10-14/25*atan(5), 0]
ans =
12/25*atan(5)+9/20*x^2-3/20-21/25*atan(5)*x^2
f3 =
1/5*atan(5)+(3/10-14/25*atan(5))*(3/2*x^2-1/2)
(最佳平方逼近多项式)
ans =
4/1625-642/3125*atan(5)^2+209/625*atan(5)
化简均方误差可得ans =
0.0742
二.在[-1,1]上取5个和9个等距节点,做最小二乘拟合,得出均方误差。 五个节点时,matlab编码为:
首先建立M文件,并保存
function y=f(x)
y=1/(1+25*x^2);
end
x=[-1 -0.5 0 0.5 1];
for i=1:5
y(i)=f(x(i));
end
a=polyfit(x,y,3)
syms x
f1=a(1)*x^3+a(2)*x^2+a(3)*x+a(4)
x=[-1 -0.5 0 0.5 1];
for i=1:5
E(i)=(f(x(i))-(a(1)*x(i)^3+a(2)*x(i)^2+a(3)*x(i)+a(4)))^2;
end
sum(E)
输出结果为
a =
-0.0000 -0.6063 -0.0000 0.5737
f1 =
-4878849915647781/12980742146xxxxxxxxxxxx4082305024*x^3-1600/2639*x^2-534xxxxxxxxxxxx/6490371073168534xxxxxxxxxxxx2512*x+1514/2639
(拟合的多项式)
ans =
0.3534(均方误差)
九个点的时候,matlab编码为:
x=[-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1];
for i=1:9
y(i)=f(x(i));
end
a=polyfit(x,y,3)
syms x
f2=a(1)*x^3+a(2)*x^2+a(3)*x+a(4)
x=[-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1];
for i=1:5
E1(i)=(f(x(i))-(a(1)*x(i)^3+a(2)*x(i)^2+a(3)*x(i)+a(4)))^2;
end
sum(E1)
输出结果为:
a =
-0.0000 -0.5609 0.0000 0.4855
f2 =
-728732707776039/2535301200456458802993406410752*x^3-1263030908712921/2251799813685248*x^2+4915246442354361/2028240960xxxxxxxxxxxx251286016*x+1093229300764671/2251799813685248
(最小二乘拟合多项式)
ans =
0.3350
(均方误差)
三:比较三个均方误差
经比较发现,最佳平方逼近多项式拟合程度高,最小二乘逼近中,9点的比5点的均方误差小。