Question

In: Computer Science

import math import numpy as np import numpy.linalg from scipy.linalg import solve A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0,...

import math
import numpy as np
import numpy.linalg
from scipy.linalg import solve



A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0, 0, 0],
             [-math.sin(math.pi/6), 0, -math.sin(math.pi/3), 0, 0, 0],
             [math.cos(math.pi/6), 1, 0, 1, 0, 0],
             [math.sin(math.pi/6), 0, 0, 0, 1, 0],
             [0, -1, -math.cos(math.pi/3), 0, 0, 0],
             [0, 0, math.sin(math.pi/3), 0, 0, 1]])

b = np.array([0, 2000, 0, 0, 0, 0])
x = [0, 0, 0, 0, 0, 0]



def seidel(a, x, b):
    # Finding length of a(3)
    n = len(a)
    # for loop for 3 times as to calculate x, y , z
    for j in range(0, n):
        # temp variable d to store b[j]
        d = b[j]

        # to calculate respective xi, yi, zi
        for i in range(0, n):
            if (j != i):
                d -= a[j][i] * x[i]
        # updating the value of our solution
        x[j] = d / a[j][j]
    # returning our updated solution
    return x


seidel(A, x, b)

RESULTS:

:\Users\mauri\Anaconda3\python.exe "C:/Users/mauri/PycharmProjects/OCEN 261 APPLIED NUMERICAL METHODS/HW 7.py"
C:/Users/mauri/PycharmProjects/OCEN 261 APPLIED NUMERICAL METHODS/HW 7.py:75: RuntimeWarning: divide by zero encountered in double_scalars
x[j] = d / a[j][j]
C:/Users/mauri/PycharmProjects/OCEN 261 APPLIED NUMERICAL METHODS/HW 7.py:73: RuntimeWarning:

PLEASE HELP MEND MY GAUSS-Seidel Code

Solutions

Expert Solution

CODE:

import math
import numpy as np
import warnings
#import numpy.linalg
#from scipy.linalg import solve

A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0, 0, 0],
[-math.sin(math.pi/6), 0, -math.sin(math.pi/3), 0, 0, 0],
[math.cos(math.pi/6), 1, 0, 1, 0, 0],
[math.sin(math.pi/6), 0, 0, 0, 1, 0],
[0, -1, -math.cos(math.pi/3), 0, 0, 0],
[0, 0, math.sin(math.pi/3), 0, 0, 1]])

b = np.array([0, 2000, 0, 0, 0, 0])
x = [0, 0, 0, 0, 0, 0]

def seidel(a, x, b):
# Finding length of a(3)
n = len(a)
# for loop for 3 times as to calculate x, y , z
for j in range(0, n):
# temp variable d to store b[j]
d = b[j]

# to calculate respective xi, yi, zi
for i in range(0, n):
if (j != i):
d -= a[j][i] * x[i]
# updating the value of our solution
warnings.filterwarnings("ignore")
x[j] = d / a[j][j]
# returning our updated solution
return x
seidel(A, x, b)

OUTPUT:


Related Solutions

Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path):...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path): """ Retrieve list of students and their expected grade from file, generate a sampled test grade for each student drawn from a Gaussian distribution defined by the student expected grade as mean, and the given standard deviation. If the sample is higher than 100, re-sample. If the sample is lower than 0 or 5 standard deviations below mean, re-sample Write the list of student...
(x+3)^2/3 + (x+3)^1/3 - 6 = 0 Solve the equation.
(x+3)^2/3 + (x+3)^1/3 - 6 = 0 Solve the equation.
Please fix all the errors in this Python program. import math def solve(a, b, c): """...
Please fix all the errors in this Python program. import math def solve(a, b, c): """ Calculate solution to quadratic equation and return @param coefficients a,b,class @return either 2 roots, 1 root, or None """ #@TODO - Fix this code to handle special cases d = b ** 2 - 4 * a * c disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 if...
?" + 3?′ + 2? = ????, ?(0) = 0, ?′(0) = 2 1) Please solve...
?" + 3?′ + 2? = ????, ?(0) = 0, ?′(0) = 2 1) Please solve using an annihilator 2) Please solve using the Method of Variation of Parameters Thank you.
solve using variation of parameters. y'' + 4y = 6 sint; y(0)=6, y'(0) = 0
solve using variation of parameters. y'' + 4y = 6 sint; y(0)=6, y'(0) = 0
1. Use the Laplace transform to solve the initial value problem. ?"+4?′+3?=1−?(?−2)−?(?−4)+?(?−6), ?(0)=0, ?′(0)=0 2. Use...
1. Use the Laplace transform to solve the initial value problem. ?"+4?′+3?=1−?(?−2)−?(?−4)+?(?−6), ?(0)=0, ?′(0)=0 2. Use the Laplace transform to solve the initial value problem. ?"+4?=?(?), ?(0)=1, ?′(0)=−1     = { 1, ? < 1 where ?(?) =   {0, ? > 1.
1) Load the data from lab6_precipitation.csv into a numpy array; 2) Make a numpy array with...
1) Load the data from lab6_precipitation.csv into a numpy array; 2) Make a numpy array with the years 1916-2016 and dtype=int; 3) Make a numpy array with the months 1-12 and dtype=int; 4) Print out the shape of the data array and the lengths of the years and month array. If the length of your year array does not equal the number of rows in your data array, or the length of your months array does not equal the number...
solve the inital value problem ?′′ + 6?′ + 9? = 2?^(-t) ; ?(0) =4, ?’(0)...
solve the inital value problem ?′′ + 6?′ + 9? = 2?^(-t) ; ?(0) =4, ?’(0) = −6
({s^3} + 6{s^2} + 15s + 15)=0 Solve for s - please show all work
({s^3} + 6{s^2} + 15s + 15)=0 Solve for s - please show all work
Solve the following ODE using Laplace Transforms ?̈+?̇+3?=0;?(0)=1; ?̇(0)=2
Solve the following ODE using Laplace Transforms ?̈+?̇+3?=0;?(0)=1; ?̇(0)=2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT