Question

Write a python function to compute the solution to a linear system using the conjugate gradiant algorithm.

Here is the sudo code, but I can't figure out what T is:1x(0)-initial guess 4for k-0,1,2, 5 α(k) 6 X(k+1)-X(k)+α(k)s(k)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find below the python code to implement the above pseudocode along with necessary details in comment. Also, note that 'T' in the above code refers to the transpose of a matrix.

import numpy as np

# define the matrices
A=np.array([[3,2],[2,6]]) # should be a positive definite matrix
b=np.array([[2],[-8]])

# initilaize the method
x0=np.array([[0],[1]])
r0=b-(A.dot(x0))
s0=r0
imax=30
iter=1
diffx=100

# iteratively update the solution
while (iter<=imax) and (diffx>0.01):
alpha=((np.transpose(r0).dot(r0))/((np.transpose(s0).dot(A)).dot(s0)))
x=x0+alpha*s0
r=r0-alpha*(A.dot(s0))
beta=(np.transpose(r).dot(r))/(np.transpose(r0).dot(r0))
s=r+beta*(s0)
r0=r
s0=s
iter=iter+1
diffx=np.linalg.norm(np.absolute(x-x0))
x0=x

  
print(x)

====================== SCREENSHOT OF CODE

1 import numpy as np 3# define the matrices 4A-np. array([ [3,2], [2,6]]) # should be a positive definite matrix 5 b-np.array

=========================== SAMPLE OUTPUT

L 2.1 2 In [32]:

Add a comment
Know the answer?
Add Answer to:
Write a python function to compute the solution to a linear system using the conjugate gradiant a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using python with only using import math and copy no other powerful functions. Write a function...

    using python with only using import math and copy no other powerful functions. Write a function defined as: def GaussSeidel(Aaug, x, Niter = 15): Purpose: use the Gauss-Seidel method to estimate the solution to a set of N linear equations expressed in matrix form as Ax = b. Both A and b are contained in the function argument – Aaug. Aaug: an augmented matrix containing [A | b ] having N rows and N+1 columns, where N is the number...

  • Let the mathematical function f(x) be defined as: f(x) = exp(-0.5x) cos(5x)-0.5 , x 〉 0 Write a ...

    Let the mathematical function f(x) be defined as: f(x) = exp(-0.5x) cos(5x)-0.5 , x 〉 0 Write a Matlab function called Newton1 that would find the zero based on a passing initial guess as an input argument x0. The function returns the estimated zero location x, the function value at the zero location (f) and the number of iteration k. The iteration function converges if f(%) < 5*eps and it should diverge if the iteration number k>10000. When it diverges,...

  • Let the mathematical function flu) be defined as: f(x)-exp-0.5x)cos(5x) - 0.5 .x>0 Write a Matlab...

    Please follow the instructions without using other solutions. Thank you in advance Let the mathematical function flu) be defined as: f(x)-exp-0.5x)cos(5x) - 0.5 .x>0 Write a Matlab function called Newton1 that would find the zero based on a passing initial guess as an input argument x0. The function returns the estimated zero location x, the function value at the zero location (f) and the number of iteration k. The iteration function converges if f(%) < 5"eps and it should diverge...

  • The function should starts as: function [x,fs,k]=Newton1(x0) % enter your code here end >> ...

    The function should starts as: function [x,fs,k]=Newton1(x0) % enter your code here end >> [x,f,k]=Newton1(0)  x = 1.1641, f = -1.6653e-16, k = 7 >> [x,f,k]=Newton1(0.1)  x = 0.1972, f = 1.1102e-16, k = 5 >> [x,f,k]=Newton1(1.5)  x = 1.3111, f = -1.1102e-16, k = 6 >> [x,f,k]=Newton1(2)  Warning iteration diverged, x = 2.8808, f = -0.5624, k = 1000 Let the mathematical function f(x) be defined as: f(x) = exp(-0.5x) cos(5x)-0.5 , x 〉 0...

  • Write the code in python 5. (10 points) Write a function that takes a square matrix...

    Write the code in python 5. (10 points) Write a function that takes a square matrix and compute the summation of items on both diagonals. Example: 9 13 5 27 1 11X76 6 0 7 102

  • Consider the linear system 11 0.5.01 21 + 12 0.5.22 + 13 0.25.13 13 0.2 -1.425...

    Consider the linear system 11 0.5.01 21 + 12 0.5.22 + 13 0.25.13 13 0.2 -1.425 2 whose solution is (0.9,-0.8, 0.7). (b) Approximate the solution of the system by performing two iterations of the Gauss-Seidel algorithm, using (10) (0) = (0,0,0)' as the initial guess. (c) Approximate the solution of the system using one iteration of the SOR scheme, with w = 0.7 and (5) x() = (0,0,0)

  • QUESTION 2 Consider the linear system Ti 0.521 + 21 2 0.5x2 + 13 0.25.13 23...

    QUESTION 2 Consider the linear system Ti 0.521 + 21 2 0.5x2 + 13 0.25.13 23 0.2 -1.425 2 whose solution is (0.9,-0.8, 0.7). (a) Determine whether the coefficient matrix is strictly diagonally dominant. (5) (b) Approximate the solution of the system by performing two iterations of the Gauss-Seidel algorithm, using (10) x(0) - (0,0,0)' as the initial guess. (c) Approximate the solution of the system using one iteration of the SOR scheme, with w = 0.7 and (5) x(0)...

  • QUESTION 2 Consider the linear system T 0.50 + 0.2 -1.425 12 0.5x2 + 0.25.73 23...

    QUESTION 2 Consider the linear system T 0.50 + 0.2 -1.425 12 0.5x2 + 0.25.73 23 whose solution is (0.9,-0.8,0.7). (a) Determine whether the coefficient matrix is strictly diagonally dominant. (5) (b) Approximate the solution of the system by performing two iterations of the Gauss-Seidel algorithm, using (10) x(0) = (0,0,0)' as the initial guess. (c) Approximate the solution of the system using one iteration of the SOR scheme, with w = 0.7 and (5) x) = (0,0,0) [20]

  • Find the solution to the linear system of differential equations {?′?′==−2?+12?−?+5?{x′=−2x+12yy′=−x+5y   satisfying the initial conditions ?(0)=1x(0)=1...

    Find the solution to the linear system of differential equations {?′?′==−2?+12?−?+5?{x′=−2x+12yy′=−x+5y   satisfying the initial conditions ?(0)=1x(0)=1 and ?(0)=0y(0)=0. د (1 point) Find the solution to the linear system of differential equations { -2x + 12y -x + 5y satisfying the initial conditions x(0) = 1 y د and y(0) = 0. x(t) = yt) =

  • Write three functions that compute the square root of an argument using three different methods. The...

    Write three functions that compute the square root of an argument using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number is the values such that x . For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that is in the range 0.0 to 100.0 You program should have a main() that asks the user for x and an accuracy...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT