方程求根——牛顿迭代法

更新时间:2023-04-26 17:42:33 阅读: 评论:0


2023年4月26日发(作者:护理等级分级标准)

⽅程求根——⽜顿迭代法

这段代码实现了⽜顿切线法、简化⽜顿法和⽜工作心态 顿下⼭法这三种⽅程求解法,由于输出结果较长,只以⽜顿下⼭法为例写⼀段例题

1.代码

%%⽜顿迭代法

%%method-1时为⽜顿切线法,method0时为简化⽜顿法,method1时为⽜顿下⼭法

%%f是表达式f(x) = 0X0是初值,epsilon是精度,interval是包含解的区间

function NM = Newton_method(f,X0,epsilon,interval,me椴树蜜多少钱一斤 thod)

Y0 = subs(f,X0);

%%作图

t = interval(1):(interval(2)-interval(1))/1000:interval(2);

T = subs(f,t);

T1 = zeros(1,max(size(t)));

Y1 = subs(f,X0)+subs(diff(f),X0)*(t-X0);

h = figure;

t(h,'color','w');

plot(t,T,'c',t,Y1,'g',X0,Y0,'ro',t,T1,'y');

grid on;

xlabel('x shaft');ylabel('y shaft');

title('函数图像');

hold on

x(1) = X0;

ub = 100;e = floor(abs(log(epsilon)));

if method == -1

disp('⽜顿切线法');

for i = 2:ub

x(i) = x(i-1)-subs(f,x(i-1))/subs(diff(f),x(i-1));

delta = x(i)-x(i-1);

if abs(delta) < epsilon

break;

end

end

disp('迭代次数为:');

i-1

disp('迭代解为:');

NM = vpa(x,e);

X_end = x(i);

Y_end = subs(f,X_end);

X = double([X0 X_end]);Y = double([Y0 Y_end]);

Y2 = Y_end+subs(diff(f),X_end)*(t-X_end);

plot(t,Y2,'b',X_end,Y_end,'mo');

legend('T:函数图像','Y1:初始点处切线','Y0:初始值处切点','T1:直线y=0','Y2:迭代解处的切线','Y_end:迭代解处切点');

for i = 1:2

text(X(i),Y(i),['(',num2str(X(i)),',',num2str(Y(i)),')'],'color',[0.02 0.79 0.99]);

end

elif method == 0

disp('简化⽜顿法湘西边城 ');

for i = 2:ub

x(i) = x(i-1)-subs(f,x(i-1))/subs(diff(f),x(1));

delta = x(i油炸虾 )-x(i-1);

if abs(delta) < epsilon

break;

end

end

disp('迭代次数为:');

i-1

disp('迭代解为:');

NM = vpa(x,e);

X_end = x(i);

Y_end = subs(f,X_end);

X = double([X0 X_end]);Y = double([Y0 Y_end]);

Y2 = Y_end+subs(diff(f),X_end)*(t-X_end);

plot(t,Y2,'b',X_end,Y_end,'mo');

legend('T:函数图像','Y1:初始点处切线','Y0:初始值处切点','T1:直线y=0','Y2:迭代解处的切线','Y_end:迭代解处切点');

for i = 1:2

text(X(i),Y(i),['(',num2str(X(i)),',',num2str(Y(i)),')'],'color',[0.02 0.79 0.99]);

end

elif method == 1

disp('⽜顿下⼭法');

lambda = input('输⼊下⼭因⼦:');

for i = 2:ub

x(i) = x(i-1)-lambda*subs(f,x(i-1))/subs(diff(f),x(1));

delta = x(i)-x(i-1);

if abs(delta) < epsilon

break;

end

end

disp('迭代次数为:');

i-1

disp('迭代解为:');

NM = vpa(x,e);

X_end = x(i);

Y_end = subs(f,X_end);

X = double([X0 X_end]);Y = double([Y0 Y_end]);

Y2 = Y_end+subs(diff(f),X_end)*(t-X_end);

plot(t,Y2,'b',X_end,Y_end,'mo');

legend('T:函数图像','Y1:初始点处切线','Y茶杯贵宾犬 0:初始值处切点','T1:直线y=0','Y2:迭代解处的切线','Y_end:迭代解处切点');

fo函数公式初中 r i = 1:2

text(X(i),Y(i),['(',num2str(X(i)),',',num2str(Y(i)),')'],'color',[0.02 0.79 0.99]);

end

end

2.例⼦

clear all

clc

syms x;

f = x^exp(x)-1;

X0 = 0.8;

epsilon=1e-6;

interval = [0,2];

method = 1;

%%⽜顿下⼭法

X = Newton_method(f,X0,epsilon,interval,method)

结果如下

⽜顿下⼭法

输⼊下⼭因⼦:0.8

迭代次数为:

ans =

21

迭代解为:

X =

[ 0.8, 1., 0.9839179688712, 1., 0.995101056432, 1., 0.9984619264687, 1., 0.99951321039, 1., 0.9998455622605, 1., 0.9999509665425, 1., 0.99

由于迭代函数原因,图象上的数据显⽰出现了遮挡,这⼀部分的代码以后再进⾏优化


本文发布于:2023-04-26 17:42:33,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/515860.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:牛顿下山法
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图