%---------- f.m -------------------
function z =f(x)
z =exp(5*x)-sin(x)+x^3-20;
End
%------ f1.m ---------
function z =f1(x)
z =5*exp(5*x)-cos(x)+3*x^2;
end
主程序
%----------NewtonDiedai.m -----------
function x =NewtonDiedai(x0,k,M,eps)
k=0;
x=x0;while(k<M)
c1=f(x);
c2=f1(x);if(c1==0)
x;breakelseif(c2==0)break;
end
x1=x-c1/c2;if(abs(x1)<1)
res=abs(x1-x);else
res=abs(x1-x)/abs(x1);
end
k=k+1;
x=x1;if(res<eps)break
end
end
end
命令台输入
>> eps =10^-10%控制精度
eps =1.000000000000000e-10>> M =40%最大迭代次数
M =40>>NewtonDiedai(1,7,M,eps)
若迭代格式为Xn-1 = a*Xn(1-Xn)编写程序并绘图
函数程序
%-----------f.m -----------
function z =f(x,a)
z = a*(x-x.^2);
End
命令台输入
>> u =-5:0.1:5;%横坐标间距0.1>>plot(u,f(u,0.7),'b.',u,f(u,2.9),'g*',u,f(u,3.4),'mo',u,f(u,3.7),'r+');