MATLAB实例:⾮线性⽅程数值解法(迭代解)
MATLAB实例:⾮线性⽅程数值解法(迭代解)
很久之前写过⼀篇关于“”,本博⽂相当于之前这⼀篇的延续与拓展,介绍四种求解⼀元⾮线性⽅程的数值解法(迭代解),包括:⽜顿迭代法,Halley迭代法,Houholder迭代法以及预测校正⽜顿-哈雷迭代法(Predictor-Corrector Newton-Halley,PCNH),具体参考⽂献[1],来源于这篇⽂章:THREE-STEP ITERATIVE METHOD WITH EIGHTEENTH ORDER CONVERGENCE FOR SOLVING NONLINEAR EQUATIONS。
1. 迭代更新公式
2. MATLAB程序
第一个登上月球的人
newton.m
function [x1, k]=newton(t1,esp,m)
syms x;
fun=x^3+4*(x^2)-10;
for k=1:m
if abs(subs(diff(fun,'x'),x,t1))<esp
x1=t1;
break;
公务员年龄要求el
if subs(diff(fun,'x',2),x,t1)==0
break;
disp('解题失败!')
el
t0=t1;
t1=t0-subs(fun,x,t0)/subs(diff(fun,'x'),x,t0);
if abs(t1-t0)<esp
x1=t1;
break;
end
end
end
end
% x1=vpa(x1,15);
halley.m
function [x1, k]=halley(t1,esp,m)
syms x;
fun=x^3+4*(x^2)-10;
for k=1:m
if abs(subs(diff(fun,'x'),x,t1))<esp
x1=t1;
break;
el
春节背景图
if subs(diff(fun,'x',2),x,t1)==0
break;
新型隐球菌disp('解题失败!')
el
t0=t1;
t1=t0-(2*subs(fun,x,t0)*subs(diff(fun,'x'), x, t0))/(2*(subs(diff(fun,'x'), x, t0))^2-subs(fun, x, t0)*subs(diff(fun,'x',2),x,t0)); if abs(t1-t0)<esp
x1=t1;
break;
end
end
end
end
% x1=vpa(x1,15);
houholder.m
function [x1, k]=houholder(t1,esp,m)
syms x;
fun=x^3+4*(x^2)-10;
for k=1:m
if abs(subs(diff(fun,'x'),x,t1))<esp
x1=t1;
break;
el
适合学英语的电影
if subs(diff(fun,'x',2),x,t1)==0
break;
disp('解题失败!')
el
t0=t1;
t1=t0-(subs(fun, x, t0))/(subs(diff(fun,'x'),x,t0))-(((subs(fun, x, t0))^2)*subs(diff(fun,'x',2),x,t0))/(2*(subs(diff(fun,'x',2),x,t0))^3); if abs(t1-t0)<esp
x1=t1;
break;
end
end
end
end
% x1=vpa(x1,15);
PCNH.m
function [x1, k]=PCNH(t1,esp,m)
syms x;
fun=x^3+4*(x^2)-10;
for k=1:m
if abs(subs(diff(fun,'x'),x,t1))<esp
x1=t1;
break;
el
if subs(diff(fun,'x',2),x,t1)==0
break;
disp('解题失败!')
el
t0=t1;
w=t0-subs(fun,x,t0)/subs(diff(fun,'x'),x,t0);
y=w-(2*subs(fun,x,w)*subs(diff(fun,'x'), x, w))/(2*(subs(diff(fun,'x'), x, w))^2-subs(fun, x, w)*subs(diff(fun,'x',2),x,w)); t1=y-(subs(fun, x, y))/(subs(diff(fun,'x'),x,y))-(((subs(fun, x, y))^2)*subs(diff(fun,'x',2),x,y))/(2*(subs(diff(fun,'x',2),x,y))^3);
if abs(t1-t0)<esp
x1=t1;
break;
end
end
end
end
% x1=vpa(x1,15);
demo.m
clear
clc
% Input: 初始值,迭代终⽌条件,最⼤迭代次数缘道
[x1, k1]=newton(1,1e-4,20); % ⽜顿迭代法
[x2, k2]=halley(1,1e-4,20); % Halley迭代法
[x3, k3]=houholder(1,1e-4,20); % Houholder迭代法
[x4, k4]=PCNH(1,1e-4,20); % 预测校正⽜顿-哈雷迭代法(PCNH)
fprintf('⽜顿迭代法求解得到的⽅程的根为:%.15f, 实际迭代次数为:%d次\n', x1, k1);
fprintf('Halley迭代法求解得到的⽅程的根为:%.15f, 实际迭代次数为:%d次\n', x2, k2);
庄周梦蝶原文fprintf('Houholder迭代法求解得到的⽅程的根为:%.15f, 实际迭代次数为:%d次\n', x3, k3);
fprintf('预测校正⽜顿-哈雷迭代法(PCNH)求解得到的⽅程的根为:%.15f, 实际迭代次数为:%d次\n', x4, k4); %% 函数图像
x=-5:0.01:5;
y=x.^3+4.*(x.^2)-10;
y_0=zeros(length(x));
plot(x, y, 'r-', x, y_0, 'b-');
xlabel('x');
ylabel('f(x)');
title('f(x)=x^3+4{x^2}-10');
saveas(gcf,sprintf('函数图像.jpg'),'bmp'); %保存图⽚
3. 数值结果
求解$f(x)=x^3+4{x^2}-10=0$⽅程在$x_0=1$附近的根。
⽜顿迭代法求解得到的⽅程的根为:1.365230013435367, 实际迭代次数为:4次
Halley迭代法求解得到的⽅程的根为:1.365230013414097, 实际迭代次数为:3次
Houholder迭代法求解得到的⽅程的根为:1.365230013391664, 实际迭代次数为:3次
预测校正⽜顿-哈雷迭代法(PCNH)求解得到的⽅程的根为:1.365230013414097, 实际迭代次数为:2次
函数图像:
良知
[1] Bahgat, Mohamed & Hafiz, Mohammad. (2014). . International Journal of Pure and Applied Mathematics. 93.