• 沒有找到結果。

matlab 实现牛顿迭代法求解非线性方程组

N/A
N/A
Protected

Academic year: 2021

Share "matlab 实现牛顿迭代法求解非线性方程组"

Copied!
5
0
0

加載中.... (立即查看全文)

全文

(1)

matlab 实现牛顿迭代法求解非线性方程组

已知非线性方程组如下 3*x1-cos(x2*x3)-1/2=0

x1^2-81*(x2+0.1)^2+sin(x3)+1.06=0 exp(-x1*x2)+20*x3+(10*pi-3)/3=0 求解要求精度达到 0.00001

————————————————————————————————

首先建立函数 fun

储存方程组编程如下将 fun.m 保存到工作路径中:

function f=fun(x);

%定义非线性方程组如下

%变量 x1 x2 x3

%函数 f1 f2 f3

syms x1 x2 x3

f1=3*x1-cos(x2*x3)-1/2;

f2=x1^2-81*(x2+0.1)^2+sin(x3)+1.06;

f3=exp(-x1*x2)+20*x3+(10*pi-3)/3;

f=[f1 f2 f3];

(2)

————————————————————————————————

建立函数 dfun

用来求方程组的雅克比矩阵将 dfun.m 保存到工作路径中:

function df=dfun(x);

%用来求解方程组的雅克比矩阵储存在 dfun 中

f=fun(x);

df=[diff(f,'x1');diff(f,'x2');diff(f,'x3')];

df=conj(df');

————————————————————————————————

编程牛顿法求解非线性方程组将 newton.m 保存到工作路径中:

function x=newton(x0,eps,N);

con=0;

%其中 x0 为迭代初值 eps 为精度要求 N 为最大迭代步数 con 用来记录结果是否收敛

for i=1:N;

f=subs(fun(x0),{'x1' 'x2' 'x3'},{x0(1) x0(2) x0(3)});

df=subs(dfun(x0),{'x1' 'x2' 'x3'},{x0(1) x0(2) x0(3)});

x=x0-f/df;

for j=1:length(x0);

(3)

il(i,j)=x(j);

end

if norm(x-x0)<eps con=1;

break;

end x0=x;

end

%以下是将迭代过程写入 txt 文档文件名为 iteration.txt

fid=fopen('iteration.txt','w');

fprintf(fid,'iteration');

for j=1:length(x0) fprintf(fid,' x%d',j);

end for j=1:i

fprintf(fid,'\n%6d ',j);

for k=1:length(x0)

fprintf(fid,' %10.6f',il(j,k));

end end

(4)

if con==1

fprintf(fid,'\n 计算结果收敛!');

end if con==0

fprintf(fid,'\n 迭代步数过多可能不收敛!');

end

fclose(fid);

————————————————————————————————

运行程序

在 matlab 中输入以下内容

newton([0.1 0.1 -0.1],0.00001,20)

————————————————————————————————

输出结果

ans =

0.5000 0.0000 -0.5236

———————————————————————————————————————————————

(5)

在 iteration 中查看迭代过程

iteration x1 x2 x3

1 0.490718 0.031238 -0.519661 2 0.509011 0.003498 -0.521634 3 0.500928 0.000756 -0.523391 4 0.500227 0.000076 -0.523550 5 0.500019 0.000018 -0.523594 6 0.500005 0.000002 -0.523598 7 0.500000 0.000000 -0.523599 计算结果收敛!

參考文獻

相關文件

。如何是奪境不奪人。濟曰。王令已行天下徧。將軍塞外絕煙塵。師曰。如何是人境

• 課程由現代的普遍取向轉向後現代的多元 取向,從根據學界所預訂的學科知識,轉

MASS::lda(Y~.,data) Linear discriminant analysis MASS::qda(Y~.,data) Quadratic Discriminant Analysis class::knn(X,X,Y,k,prob) k-Nearest Neighbour(X 為變數資料;Y 為分類)

蒲公英(水印木刻)  现代  吴凡 挤牛奶(石版画)  现代 

Sample a point in the plane, iterates Newton's Method on f(z) using that point as the initial value, and colors the point based on which of the four roots the iterates converge

• 孤波解的性质:1- x 趋向无穷时 u 趋于0;2- 波是单向传播的;3- 波幅与波速成正比;4- KdV方程的色散与非线性刚好抵消,导致

stochastic activator-inhibitor systems without saturation and ones with saturation in time-invariant or time-variant domains

非线性物理: 分形物理 分形物理 动力系统映射:.