数值分析,二分法、弦截法、Steffensen法的编程代码

二分法-习题2.1:
代码:
Main.h:
clc;clear;fromat long;
secant(0,pi/4,10^(-8));
half .m:
x=half(a,b,tol)
c=(a+b)/2;
k=1;
m=1+round((log(b-a)-log(2*tol))/log(2));
while k<=m
if f©==0
c=c;
return;
elseif f(a)*f©<0
b=(a+b)/2;
else
a=(a+b)/2;
end
c=(a+b)/2;k=k+1;
end
c
f.m:
function f=f(x)
f=x-2^(-x);
end

运行结果:
c= 0.641185741871595

Steffensen法-习题2.2:
代码:
Main.h:
clc;clear;fromat long;
steffensen(0.6,10^(-8));
steffensen.m:
function x=steffensen(x0,tol)
y0=f(x0); x1=x0-y0^2/(y0-f(x0-y0));k=1;
while abs(x1-x0)>=tol
x0=x1; y0=f(x0);
x1=x0-y0^2/(y0-f(x0-y0));k=k+1;
end
x1
k
f.m:
function f=f(x)
f=1+sin(x)-2*cos(x);
end

运行结果:
x1 =0.643501108793284
k=4

弦截法-习题2.2:
代码:
Main.h:
clc;clear;fromat long;
secant(0,pi/4,10^(-8));
secant.m:
function x=secant(x0,x1,tol)
x2=x1-(x1-x0)*f(x1)/(f(x1)-f(x0));
k=1;
while abs(x2-x1)>=tol
x0=x1;
x1=x2;
x2=x1-(x1-x0)f(x1)/(f(x1)-f(x0));
k=k+1;
end
x2
k
f.m:
function f=f(x)
f=1+sin(x)-2
cos(x);
end

运行结果:
x2 = 0.643501108793276
k = 5

上一篇:对象拷贝——systemverilog


下一篇:autoCAD2007 快捷键 标注