陕西科技大学matlab实验1 解非线性方程实验
- 格式:pdf
- 大小:115.07 KB
- 文档页数:5
实验1 解非线性方程实验
成绩
实验类型:●验证性实验 ○综合性实验 ○设计性实验
实验目的:进一步熟练掌握解非线性方程二分法算
法、弦截法算法,提高编程能力和解算非线性方程问题的
实践技能。
实验内容:用二分法算法、牛顿迭代法,弦截法算法解算非线性方程,,计算=0的根
实验原理二分法算法
牛顿迭代法
弦截法算法
实验步骤
1 要求上机实验前先编写出程序代码
2 编辑录入程序
3 调试程序并记录调试过程中出现的问题及修改
程序的过程
4 经反复调试后,运行程序并验证程序运行是否
正确。
5 记录运行时的输入和输出。
实验总结
实验报告:根据实验情况和结果撰写并递交实验报告。
参考程序
一.二分法算法
1.建立二分法的函数文件bisect.m
function [c,err,yc]=bisect(f,a,b,delta)
%Iput - f is the function input as a string 'f'
% -a and b are the left and right end points % -delta is the tolerance
%Output -c is the zero point
% -yc=f(c)
% -err is the error estimate for c
ya=feval(f,a);
yb=feval(f,b);
if ya*yb > 0,return,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*yc>0
b=c;
yb=yc;
else
a=c;
ya=yc;
end
if b-a < delta,break,end
end
c=(a+b)/2;
err=abs(b-a);
yc=feval(f,c);
2.建立f(x)=x^2-5的matlab函数文件fff.m
function y=fff(x);
y=x.^2-5;
3.在命令窗口中准备调用bisect函数的实参
>> a=2;
>> b=3;
>> delta=0.0001;
4.在命令窗口中调用bisect函数
>> [x,err,yx]=bisect('fff',a,b,delta)
x =
2.2361
err =
6.1035e-005
yx =
-6.4894e-005
>> [x,err,yx]=bisect('fff',a,b,delta)
x =
-2.2361
err =
6.1035e-005
yx =
-6.4894e-005
二.牛顿迭代法
1.建立牛顿迭代法的函数文件newton.m
function [p0,err,k,y]=newtow(f,df,p0,delta,epsilon,max1)
%Input -f is the object function input as a string 'f'
% -df is the derivative of f input as a string 'df'
% -p0 is the initial approximation to a zero o f
% -delta is the tolerance for the function values y
% -max1 is the maximum number of iterations
%Output -p0 is the Newton-Raphson approximation to the zero % -err is the error estimate for p0
% -k is the number of iteration
% -y is the function value f(p0)
for k=1:max1
p1=p0-feval(f,p0)/feval(df,p0);
err=abs(p1-p0);
relerr=2*err/(abs(p1)+delta);
p0=p1;
y=feval(f,p0);
if (err 3.在命令窗口中准备调用newton函数的实参 >> p0=2; >> delta=0.0001; >> epsilon=0.00001; >> max1=1000 max1 = 1000 >> p0=-2; 4.在命令窗口中调用newton函数 >> [x,err,k,y]=newton('fff','ff',p0,delta,epsilon,max1) x = 2.2361 err = 4.3133e-005 k = 3 y = 1.8605e-009 >> [x,err,k,y]=newton('fff','ff',p0,delta,epsilon,max1) x = -2.2361 err = 4.3133e-005 k = 3 y = 1.8605e-009 三.弦截法算法 1.建立弦截法算法的函数文件secant.m function [p1,err,k,y]=secant(f,p0,p1,delta,epsilon,max1) for k=1:max1 p2=p1-feval(f,p1)*(p1-p0)/(feval(f,p1)-feval(f,p0)); err=abs(p2-p1); relerr=2*err/(abs(p2)+delta); p0=p1; p1=p2; y=feval(f,p1); if(err 2.建立f(x)=x^2-5的matlab函数文件fff.m