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

In python import numpy as np Given the array b = np.arange(-6, 4) compute and print...
In python import numpy as np Given the array b = np.arange(-6, 4) compute and print 1.) The array formed by \(bi^2 - 1\), where the \(bi\) are the elements of the array b. 2.) The array formed by multiplying b with the scalar 100. 3.)The array formed by 2.0 b i in reverse order. (Note: the base 2.0 must be a floating point number; for integer values a ValueError: Integers to negative integer powers are not allowed. is raised.)...
UsePython (import numpy as np) use containers (Branching if statement, while loop, for loop, numpy Array)...
UsePython (import numpy as np) use containers (Branching if statement, while loop, for loop, numpy Array) Implement an algorithm to guess a random number that the computer generates. The random number must be an integer between 1 and 1000 (inclusive). For each unsuccessful attempt, the program must let the user know whether to deal with a higher number or more. low. There is no limit on the number of attempts, the game only ends when the user succeeds. The user...
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.
y''' −2y' −4y = 0, y(0) = 6, y'(0) = 3, y''(0) = 22 solve the...
y''' −2y' −4y = 0, y(0) = 6, y'(0) = 3, y''(0) = 22 solve the initial value problem You would convert it to m^3-2m-4=0. You find the root (m=2) and use synthetic division to find the other roots. m^2+2m+2 is what you get. I am stuck on what to do next? y = 2e^(−x)*cosx−3e^(−x)*sinx + 4e^(2x) is the answer.
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.
Generate a sample of 1000 random floating numbers (from 0 to 1) using numpy. Then plot...
Generate a sample of 1000 random floating numbers (from 0 to 1) using numpy. Then plot it and compare with a normal distribution plot close to that region (from 0 to 1) as well. Hint: Using Seaborn module
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT