解非线性方程组的牛顿迭代法(附Python代码)

解非线性方程组的牛顿迭代法(附Python代码)

非线性方程组的一般形式

考虑方程组

其中 均为 的多元函数.

用向量记号记 上式就可写成

当, 且 中至少有一个是自变量 的非线性函数时, 称上述方程组为非线性方程组

推广的牛顿迭代法

非线性方程组求根问题是非线性方程(即 )求根的直接推广, 只要把半变量函数 看成向量函数 则可将单变量方程求根方法推广到上述方程组.

若给出方程组 的一个近似根 , 将函数 的分量 在 用多元函数泰勒展开, 并取其线性部分, 则可表示为

令上式右端为零, 得到线性方程组

其中
F'(x)= \begin{pmatrix} \frac{\partial f_1(x)}{\partial x_1} & \frac{\partial f_1(x)}{\partial x_2} & \cdots & \frac{\partial f_1(x)}{\partial x_n} \\ \frac{\partial f_2(x)}{\partial x_1} & \frac{\partial f_2(x)}{\partial x_2} & \cdots & \frac{\partial f_2(x)}{\partial x_n} \\ \vdots & \vdots & & \vdots \\ \frac{\partial f_n(x)}{\partial x_1} & \frac{\partial f_n(x)}{\partial x_2} & \cdots & \frac{\partial f_n(x)}{\partial x_n} \end{pmatrix} \qquad
称为 的雅可比(Jacobi)矩阵.

求解上式线性方程组, 并记解为 , 则得

这就是解非线性方程组的牛顿迭代法.

数值算例

求解非线性方程组

给定初值 ,用牛顿法求解.

解 先求雅可比矩阵


由牛顿法迭代格式得
x^{(k+1)} = x^{(k)} - \frac{1}{4x_1^{(k)}x_2^{(k)}-3(x_1^{(k)})^2} \begin{pmatrix} -1 & 2x_2^{(k)} \\ -2x_1^{(k)} & 3(x_1^{(k)})^2 \end{pmatrix}\begin{pmatrix} (x_1^{(k)})^3 - (x_2^{(k)})^2 + 1 \\ (x_1^{(k)})^2 - x_2^{(k)} - 1 \end{pmatrix},

\left\{ \begin{aligned} x_1^{(k+1)} &= x_1^{(k)} - \frac{-(x_1^{(k)})^3 + (x_2^{(k)})^2 - 1 + 2(x_1^{(k)})^2x_2^{(k)} - 2(x_2^{(k)})^2 - 2x_2^{(k)}}{4x_1^{(k)}x_2^{(k)} - 3(x_1^{(k)})^2}, \\ x_2^{(k+1)} &= x_2^{(k)} - \frac{-2(x_1^{(k)})^4 + 2x_1^{(k)}(x_2^{(k)})^2 - 2x_1^{(k)} + 3(x_1^{(k)})^4 - 3(x_1^{(k)})^2x_2^{(k)} - 3(x_1^{(k)})^2}{4x_1^{(k)}x_2^{(k)} - 3(x_1^{(k)})^2}, \end{aligned} \right.
其中, .

由 逐次迭代即可得到收敛至真解 的迭代序列

解非线性方程组的牛顿迭代法Python代码

解非线性方程组的牛顿迭代法主程序newton_iteration.py

# 开发者:    Leo 刘
# 开发环境: macOs Big Sur
# 开发时间: 2021/10/6 12:52 下午
# 邮箱  : [email protected]
# @Software: PyCharm
# ----------------------------------------------------------------------------------------------------------
import math
import numpy as np
import matplotlib.pyplot as plt
from Dynamic_drawing import image2gif


def newton_iteration(x_k):
    """
    :param x_k: 第k次迭代得到的近似根
    :return x:第k+1次迭代得到的近似根x_{k+1}
    """
    x = [0, 0]
    x[0] = ((x_k[0] - (-x_k[0] ** 3 + x_k[1] ** 2 - 1 + 2 * x_k[0] ** 2 * x_k[1] - 2 * x_k[1] ** 2 - 2 * x_k[1]))
            / (4 * x_k[0] * x_k[1] - 3 * x_k[0] ** 2))
    x[1] = ((x_k[1] - (
            -2 * x_k[0] ** 4 + 2 * x_k[0] * x_k[1] ** 2 - 2 * x_k[0] + 3 * x_k[0] ** 4 - 3 * x_k[0] ** 2 * x_k[
        1] - 3 * x_k[0] ** 2)) / (4 * x_k[0] * x_k[1] - 3 * x_k[0] ** 2))

    return x


# 最大模范数
def norm(ture_u, uh):
    ture_u = np.array(ture_u)
    uh = np.array(uh)
    e = max(abs(ture_u - uh))
    return e


if __name__ == '__main__':
    N = 100
    accurate = 1e-3
    item = 0
    x = [-1.5, 0.5]
    s = [x]
    Loss_list = []
    print("迭代次数:%d" % item, end="\n")
    print("近似根:", x)
    x = newton_iteration(x)
    s.append(x)
    plt.scatter(s[item][0], s[item][1], s=20, c='b', marker='x', label='Initial value')
    plt.legend()
    plt.title("iteration times:" + str(item))
    plt.savefig('./iteration_process/iteration_' + str(item) + '.png')
    item += 1
    print("迭代次数:%d" % item, end="\n")
    print("近似根:", x)
    # print("s:", s)
    print("max|x_%d - x_%d| =" % (item, item - 1), norm(s[item], s[item - 1]))
    loss = norm(s[item], s[item - 1])
    Loss_list.append(loss)
    plt.scatter(s[item][0], s[item][1], s=20, c='k', marker='o')
    plt.title("iteration times:" + str(item))
    plt.savefig('./iteration_process/iteration_' + str(item) + '.png')
    while norm(s[item], s[item - 1]) >= accurate and item < N:
        x = newton_iteration(x)
        s.append(x)
        item += 1
        print("迭代次数:%d" % item, end="\n")
        print("近似根:", x)
        # print("s:", s)
        print("max|x_%d - x_%d| =" % (item, item - 1), norm(s[item], s[item - 1]))
        loss = norm(s[item], s[item - 1])
        Loss_list.append(loss)
        plt.scatter(s[item][0], s[item][1], s=20, c='k', marker='o')
        plt.title("iteration times:" + str(item))
        plt.savefig('./iteration_process/iteration_' + str(item) + '.png')

    # 绘图
    print('=' * 55)
    print('迭代结束'.center(55))
    print('-' * 55)
    plt.close('all')
    plt.title('Error curve')
    plt.xlabel('loss vs. epoches')
    plt.ylabel('loss')
    plt.plot(range(0, item), Loss_list, label='Loss')
    plt.savefig('Error_curve.png')
    # plt.show()
    print('已生成"误差曲线图",请打开文件"Error_curve.png"查看')
    print('-' * 55)
    print('准备绘迭代过程动态图')
    image2gif.image2gif('iteration')
    print('=' * 55)

绘制迭代过程gif图像模块image2gif.py

注意:为保重程序正常运行,请在当前目录新建文件夹Dynamic_drawing,并将image2gif.py放入其中

image2gif.py

# 开发者:    Leo 刘
# 开发环境: macOs Big Sur
# 开发时间: 2021/10/6 11:33 上午
# 邮箱  : [email protected]
# @Software: PyCharm
# ----------------------------------------------------------------------------------------------------------
import glob
import imageio
import os


def image2gif(project_name):
    """
将多张图片生成gif动图
    Args:
        project_name:图片名称
    """
    outfilename = str(project_name) + ".gif"  # 转化的GIF图片名称
    filenames = glob.glob('./iteration_process/'+str(project_name)+'*.png')
    filenames = sorted(filenames, key=lambda x: int((os.path.basename(x).split('.')[0]).split('_')[-1]))
    frames = []
    num = 0
    for image_name in filenames:
        im = imageio.imread(image_name)
        frames.append(im)
        print("\r正在读取第%d张图片!" % num, end=" ")
        num += 1
    print('读取完成!')
    print('正在绘制训练过程动态图···')
    imageio.mimsave(outfilename, frames, 'GIF', duration=0.1)  # 生成方式也差不多
    print('绘制完成,请打开文件"'+str(project_name)+'.gif"查看')


结果:

迭代次数:0
近似根: [-1.5, 0.5]
迭代次数:1
近似根: [0.5, -0.33974358974358976]
max|x_1 - x_0| = 2.0
迭代次数:2
近似根: [-0.8610152926296423, -0.6838277566977118]
max|x_2 - x_1| = 1.3610152926296424
迭代次数:3
近似根: [-2.940093974078419, -11.037286752738847]
max|x_3 - x_2| = 10.353458996041136
迭代次数:4
近似根: [2.5340129709230634, 3.5082649398943953]
max|x_4 - x_3| = 14.545551692633243
迭代次数:5
近似根: [-0.3635655221888668, -0.5023820511906301]
max|x_5 - x_4| = 4.010646991085025
迭代次数:6
近似根: [-0.09336275468497639, -2.592803894595418]
max|x_6 - x_5| = 2.090421843404788
迭代次数:7
近似根: [2.64086081379899, -1.6621495605949999]
max|x_7 - x_6| = 2.734223568483966
迭代次数:8
近似根: [-1.1611399161010523, 1.9091494878848119]
max|x_8 - x_7| = 3.8020007299000422
迭代次数:9
近似根: [-0.0455783344039754, -1.3940798819968485]
max|x_9 - x_8| = 3.3032293698816604
迭代次数:10
近似根: [0.465531874549026, -5.2859774842266924]
max|x_10 - x_9| = 3.891897602229844
迭代次数:11
近似根: [-2.0229222498782264, 3.164280809254022]
max|x_11 - x_10| = 8.450258293480715
迭代次数:12
近似根: [0.49781528135449504, -1.9536254973153508]
max|x_12 - x_11| = 5.117906306569373
迭代次数:13
近似根: [-0.5392912163984817, 1.1930943769343427]
max|x_13 - x_12| = 3.1467198742496936
迭代次数:14
近似根: [-0.9922620407756075, -1.0094424963342579]
max|x_14 - x_13| = 2.202536873268601
迭代次数:15
近似根: [0.017690034736698015, -1.8703768977189545]
max|x_15 - x_14| = 1.0099520755123055
迭代次数:16
近似根: [-5.825192846950037, 14.701999093991555]
max|x_16 - x_15| = 16.57237599171051
迭代次数:17
近似根: [2.148451698880959, -6.679778929452079]
max|x_17 - x_16| = 21.381778023443633
迭代次数:18
近似根: [-1.4875443768200582, 4.12710090383621]
max|x_18 - x_17| = 10.806879833288288
迭代次数:19
近似根: [-0.10396414259380489, -2.5954421257499143]
max|x_19 - x_18| = 6.722543029586124
迭代次数:20
近似根: [2.384601436737606, -1.3893787739133392]
max|x_20 - x_19| = 2.4885655793314108
迭代次数:21
近似根: [-1.052301026316862, 1.478095879319023]
max|x_21 - x_20| = 3.436902463054468
迭代次数:22
近似根: [-0.06809826980747986, -1.1502584131023548]
max|x_22 - x_21| = 2.6283542924213776
迭代次数:23
近似根: [-0.11745794184712476, -3.701829765705078]
max|x_23 - x_22| = 2.551571352602723
迭代次数:24
近似根: [4.289517529232492, -0.488608042695902]
max|x_24 - x_23| = 4.406975471079616
迭代次数:25
近似根: [-1.5956782106260916, 4.785639574837905]
max|x_25 - x_24| = 5.885195739858583
迭代次数:26
近似根: [-0.09021500981885265, -2.943509193510134]
max|x_26 - x_25| = 7.729148768348039
迭代次数:27
近似根: [3.59825467612765, -1.5496258115117878]
max|x_27 - x_26| = 3.6884696859465027
迭代次数:28
近似根: [-1.4819567735817334, 3.2809815472746475]
max|x_28 - x_27| = 5.080211449709384
迭代次数:29
近似根: [0.03153804264244818, -2.1355770120375137]
max|x_29 - x_28| = 5.416558559312161
迭代次数:30
近似根: [-4.865615388925959, 8.677068828438106]
max|x_30 - x_29| = 10.81264584047562
迭代次数:31
近似根: [1.8226576265017924, -3.578359723679716]
max|x_31 - x_30| = 12.255428552117822
迭代次数:32
近似根: [-1.0622961579288184, 2.3115639584723744]
max|x_32 - x_31| = 5.889923682152091
迭代次数:33
近似根: [-0.26411232182749805, -1.626095120266025]
max|x_33 - x_32| = 3.9376590787383994
迭代次数:34
近似根: [0.22293013822302402, -0.5922508425871555]
max|x_34 - x_33| = 1.0338442776788694
迭代次数:35
近似根: [-0.6779756943432664, 0.3609751936717679]
max|x_35 - x_34| = 0.9532260362589234
迭代次数:36
近似根: [-0.22511734292673657, -0.35928435234136163]
max|x_36 - x_35| = 0.7202595460131296
迭代次数:37
近似根: [1.2269221880535293, -3.8285275930954774]
max|x_37 - x_36| = 3.469243240754116
迭代次数:38
近似根: [-0.9697769113057997, 2.2476461379556025]
max|x_38 - x_37| = 6.07617373105108
迭代次数:39
近似根: [-0.38454148398333066, -1.5931166875539557]
max|x_39 - x_38| = 3.8407628255095583
迭代次数:40
近似根: [0.1901168361707068, -0.3464283146006738]
max|x_40 - x_39| = 1.2466883729532818
迭代次数:41
近似根: [-1.7456873114560372, -0.15525144213567618]
max|x_41 - x_40| = 1.935804147626744
迭代次数:42
近似根: [0.6708333348399808, 0.6361658906006241]
max|x_42 - x_41| = 2.416520646296018
迭代次数:43
近似根: [8.619740287145092, 9.639526945127173]
max|x_43 - x_42| = 9.003361054526549
迭代次数:44
近似根: [-6.12242027550251, -43.15644464340846]
max|x_44 - x_43| = 52.795971588535636
迭代次数:45
近似根: [5.057947812165906, 17.581636005427974]
max|x_45 - x_44| = 60.73808064883644
迭代次数:46
近似根: [-1.505037317535872, -8.343990723235185]
max|x_46 - x_45| = 25.92562672866316
迭代次数:47
近似根: [1.998786751637963, 3.2962390353761903]
max|x_47 - x_46| = 11.640229758611376
迭代次数:48
近似根: [0.14643018247601663, -0.04241669724389197]
max|x_48 - x_47| = 3.3386557326200825
迭代次数:49
近似根: [-11.981135577858486, -3.4883353379867565]
max|x_49 - x_48| = 12.127565760334504
迭代次数:50
近似根: [2.7486219170428483, 81.2756477841808]
max|x_50 - x_49| = 84.76398312216756
迭代次数:51
近似根: [6.389494157864395, -39.52014561303216]
max|x_51 - x_50| = 120.79579339721298
迭代次数:52
近似根: [-4.395387497428389, 23.28416735180574]
max|x_52 - x_51| = 62.804312964837905
迭代次数:53
近似根: [0.8543527949087242, -12.442303073338929]
max|x_53 - x_52| = 35.72647042514467
迭代次数:54
近似根: [-3.367641537777537, 6.728852601775285]
max|x_54 - x_53| = 19.171155675114214
迭代次数:55
近似根: [1.0784859793393573, -3.5237826210172876]
max|x_55 - x_54| = 10.252635222792573
迭代次数:56
近似根: [-0.9041687899155572, 2.0496386626218106]
max|x_56 - x_55| = 5.573421283639098
迭代次数:57
近似根: [-0.4364431328780126, -1.4849060558392355]
max|x_57 - x_56| = 3.534544718461046
迭代次数:58
近似根: [0.13917511752840786, -0.3694007703574021]
max|x_58 - x_57| = 1.1155052854818335
迭代次数:59
近似根: [-2.0998170452412244, 0.3517091924463481]
max|x_59 - x_58| = 2.238992162769632
迭代次数:60
近似根: [0.7806806256966288, 0.30217659502315847]
max|x_60 - x_59| = 2.880497670937853
迭代次数:61
近似根: [-2.9203075077161142, -4.216230334802662]
max|x_61 - x_60| = 4.5184069298258205
迭代次数:62
近似根: [2.300015402870657, -2.58794033263237]
max|x_62 - x_61| = 5.220322910586772
迭代次数:63
近似根: [-1.1182014849592814, 2.066111639621678]
max|x_63 - x_62| = 4.654051972254049
迭代次数:64
近似根: [-0.1322185462795065, -1.4865900225748792]
max|x_64 - x_63| = 3.552701662196557
迭代次数:65
近似根: [0.21016865666579965, -1.6251046834401155]
max|x_65 - x_64| = 0.3423872029453061
迭代次数:66
近似根: [-0.5029518824334255, 1.6011569535978836]
max|x_66 - x_65| = 3.2262616370379993
迭代次数:67
近似根: [-1.3381014280870533, -1.2773729469034347]
max|x_67 - x_66| = 2.8785299005013183
迭代次数:68
近似根: [0.6259109953337735, -2.922424973306596]
max|x_68 - x_67| = 1.9640124234208267
迭代次数:69
近似根: [-0.8074233753478917, 1.7398442688977631]
max|x_69 - x_68| = 4.662269242204359
迭代次数:70
近似根: [-0.5154370426591931, -1.3131152398718688]
max|x_70 - x_69| = 3.052959508769632
迭代次数:71
近似根: [0.07506196300958572, -0.46413277785560614]
max|x_71 - x_70| = 0.8489824620162627
迭代次数:72
近似根: [-2.3542426228631244, 2.158756541255383]
max|x_72 - x_71| = 2.622889319110989
迭代次数:73
近似根: [0.794300579952309, -1.1147180761876936]
max|x_73 - x_72| = 3.2734746174430764
迭代次数:74
近似根: [-0.4996254116907548, 0.3892390081840155]
max|x_74 - x_73| = 1.5039570843717092
迭代次数:75
近似根: [-0.72788388391677, -0.34021950684436697]
max|x_75 - x_74| = 0.7294585150283825
迭代次数:76
近似根: [0.530504681922837, 1.4351741199998924]
max|x_76 - x_75| = 1.7753936268442594
迭代次数:77
近似根: [2.635910806538799, 1.0392797387809352]
max|x_77 - x_76| = 2.105406124615962
迭代次数:78
近似根: [-1.0789762476124767, 0.5210414844640849]
max|x_78 - x_77| = 3.7148870541512755
迭代次数:79
近似根: [0.21505877696405748, -0.5061443200759247]
max|x_79 - x_78| = 1.2940350245765342
迭代次数:80
近似根: [-0.8982214116348299, 0.20870948420818247]
max|x_80 - x_79| = 1.1132801885988874
迭代次数:81
近似根: [0.15730615989838187, -0.24135346120360232]
max|x_81 - x_80| = 1.0555275715332118
迭代次数:82
近似根: [-3.311295588315879, -0.48933188116287857]
max|x_82 - x_81| = 3.468601748214261
迭代次数:83
近似根: [1.0838355184709314, 4.124993795013835]
max|x_83 - x_82| = 4.614325676176714
迭代次数:84
近似根: [1.3184142401761538, -0.968750102650017]
max|x_84 - x_83| = 5.0937438976638525
迭代次数:85
近似根: [-0.6760179200204675, 0.3550114257151548]
max|x_85 - x_84| = 1.9944321601966213
迭代次数:86
近似根: [-0.2259211862141872, -0.3527487593551177]
max|x_86 - x_85| = 0.7077601850702725
迭代次数:87
近似根: [1.3129334496763774, -3.935134036793709]
max|x_87 - x_86| = 3.5823852774385916
迭代次数:88
近似根: [-0.9969128794317387, 2.3269027091579058]
max|x_88 - x_87| = 6.262036745951615
迭代次数:89
近似根: [-0.3634050835022121, -1.6361699704717476]
max|x_89 - x_88| = 3.9630726796296534
迭代次数:90
近似根: [0.21464776928936882, -0.34646504066178363]
max|x_90 - x_89| = 1.289704929809964
迭代次数:91
近似根: [-1.5689292327557594, -0.27429460072971557]
max|x_91 - x_90| = 1.7835770020451283
迭代次数:92
近似根: [0.6275381593499003, 0.6844489432970754]
max|x_92 - x_91| = 2.1964673921056597
迭代次数:93
近似根: [5.912425742880391, 5.937675578834564]
max|x_93 - x_92| = 5.284887583530491
迭代次数:94
近似根: [-4.3427237400372345, -25.132364632178838]
max|x_94 - x_93| = 31.070040211013403
迭代次数:95
近似根: [3.8002788748072445, 9.819091880817089]
max|x_95 - x_94| = 34.951456512995925
迭代次数:96
近似根: [-1.0183612597949852, -4.297056512367747]
max|x_96 - x_95| = 14.116148393184837
迭代次数:97
近似根: [1.2304011829525325, 1.3854614922768833]
max|x_97 - x_96| = 5.68251800464463
迭代次数:98
近似根: [2.015189255299888, 3.366156805362683]
max|x_98 - x_97| = 1.9806953130857996
迭代次数:99
近似根: [0.1285770132534683, -0.10505278900808002]
max|x_99 - x_98| = 3.471209594370763
迭代次数:100
近似根: [-9.023892515080862, -1.866100756664504]
max|x_100 - x_99| = 9.15246952833433
=======================================================
                          迭代结束                         
-------------------------------------------------------
已生成"误差曲线图",请打开文件"Error_curve.png"查看
-------------------------------------------------------
准备绘迭代过程动态图
正在读取第100张图片! 读取完成!
正在绘制训练过程动态图···
绘制完成,请打开文件"iteration.gif"查看
=======================================================

迭代序列 后一项与前一项差值的 范数图像

迭代过程动态图, 其中蓝色标记 为给定的迭代初值

你可能感兴趣的:(解非线性方程组的牛顿迭代法(附Python代码))