第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

連續(xù)過度放松

連續(xù)過度放松

紅顏莎娜 2021-08-14 21:12:27
這里我有一些 python 腳本,它使用 Gauss-Seidel 方法求解線性方程組:import numpy as npITERATION_LIMIT = 1000#systemA = np.array([[15., -4., -3., 8.],          [-4., 10., -4., 2.],          [-3., -4., 10., 2.],          [8., 2., 2., 12.]          ])# vector bb = np.array([2., -12., -4., 6.])print("System of equations:")for i in range(A.shape[0]):    row = ["{0:3g}*x{1}".format(A[i, j], j + 1) for j in range(A.shape[1])]    print("[{0}] = [{1:3g}]".format(" + ".join(row), b[i]))x = np.zeros_like(b)for it_count in range(1, ITERATION_LIMIT):    x_new = np.zeros_like(x)    print("Iteration {0}: {1}".format(it_count, x))    for i in range(A.shape[0]):        s1 = np.dot(A[i, :i], x_new[:i])        s2 = np.dot(A[i, i + 1:], x[i + 1:])        x_new[i] = (b[i] - s1 - s2) / A[i, i]    if np.allclose(x, x_new, rtol=1e-8):        break    x = x_new它輸出的是:Iteration 379: [-21.36409652 -22.09743    -19.9999946   21.75896845]Iteration 380: [-21.36409676 -22.09743023 -19.99999481  21.75896868]Iteration 381: [-21.36409698 -22.09743045 -19.99999501  21.7589689 ]我的任務(wù)是利用此方法制作連續(xù)過度松弛 (SOR) 方法,該方法使用 omega 值來減少迭代次數(shù)。如果omega = 1,則變?yōu)?Gauss-Seidel 方法、if < 1- 簡單迭代法> 1和< 2- SOR。顯然,隨著 omega 值的增加,迭代次數(shù)應(yīng)該減少。這是維基百科提供的算法:Inputs: A, b, omegaOutput: phi (roots for linear equations)Choose an initial guess phi to the solutionrepeat until convergence  for i from 1 until n do    sigma <- 0    for j from 1 until n do      if j ≠ i then        sigma <- sigma + A[i][j]*phi[j]      end if    end (j-loop)    phi[i] = phi[i] + omega*((b[i] - sigma)/A[i][i]) - phi[i]  end (i-loop)  check if convergence is reachedend (repeat)有人在python上有工作算法嗎?如果您可以對代碼進行一些評論或幫助我如何更改此代碼,那就太好了。謝謝!
查看完整描述

2 回答

?
撒科打諢

TA貢獻1934條經(jīng)驗 獲得超2個贊

這是基于您提供的 Wiki 參考的實現(xiàn)。


import numpy as np


def sor_solver(A, b, omega, initial_guess, convergence_criteria):

  """

  This is an implementation of the pseduo-code provided in the Wikipedia article.

  Inputs:

    A: nxn numpy matrix

    b: n dimensional numpy vector

    omega: relaxation factor

    initial_guess: An initial solution guess for the solver to start with

  Returns:

    phi: solution vector of dimension n

  """

  phi = initial_guess[:]

  residual = np.linalg.norm(np.matmul(A, phi) - b) #Initial residual

  while residual > convergence_criteria:

    for i in range(A.shape[0]):

      sigma = 0

      for j in range(A.shape[1]):

        if j != i:

          sigma += A[i][j] * phi[j]

      phi[i] = (1 - omega) * phi[i] + (omega / A[i][i]) * (b[i] - sigma)

    residual = np.linalg.norm(np.matmul(A, phi) - b)

    print('Residual: {0:10.6g}'.format(residual))

  return phi



#An example case that mirrors the one in the Wikipedia article

residual_convergence = 1e-8

omega = 0.5 #Relaxation factor


A = np.ones((4, 4))

A[0][0] = 4

A[0][1] = -1

A[0][2] = -6

A[0][3] = 0


A[1][0] = -5

A[1][1] = -4

A[1][2] = 10

A[1][3] = 8


A[2][0] = 0

A[2][1] = 9

A[2][2] = 4

A[2][3] = -2


A[3][0] = 1

A[3][1] = 0

A[3][2] = -7

A[3][3] = 5


b = np.ones(4)

b[0] = 2

b[1] = 21

b[2] = -12

b[3] = -6


initial_guess = np.zeros(4)


phi = sor_solver(A, b, omega, initial_guess, residual_convergence)

print(phi)


查看完整回答
反對 回復(fù) 2021-08-14
  • 2 回答
  • 0 關(guān)注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號