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...

  • 1. Consider the following function F(x) x 2 where x = [x1 x2]T (d) Write a code to implement conj...

    1. Consider the following function F(x) x 2 where x = [x1 x2]T (d) Write a code to implement conjugate gradient method on this function. In each case, start with an initial guess of [1 1]T and plot both the solution at each iteration and the contour plots of the function on the same plot to show the trajectory towards the solution. Does it matter what the initial guess is? 1. Consider the following function F(x) x 2 where x...

  • Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our...

    Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our textbook) E.g.: Input: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Constraints: - List elements are integers - The function should return a reversed array, not print its elements You may use the following code as template: def reverse(my_list, index = None): # your code here

  • Code in Python Problem 1 (2 Points) 1. Write a function randomWalk(.. .) which simulates one...

    Code in Python Problem 1 (2 Points) 1. Write a function randomWalk(.. .) which simulates one path (or trajectory) of a simple symmetric random walk with 2N time steps (i.e. from 0,1,2,...,2N) starting at So-0 nput: lengthofRandomWalk2N Output: samplePath: Array of length 2N+1 with the entire path of the random walk on 0,1,2,..,2N In def randomwalk(lengthofRandomwalk): ## WRITE YOUR OWN CODE HERE # HINT: USE np. random . choice ( ) TO SIMULATE THE INCREMENTS return samplePath In [ ]:...

  • 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...

  • Code in Python Problem 1 (2 Points) 1. Write a function randomWalk(.. .) which simulates one path (or trajectory) of a s...

    Code in Python Problem 1 (2 Points) 1. Write a function randomWalk(.. .) which simulates one path (or trajectory) of a simple symmetric random walk with 2N time steps (i.e. from 0,1,2,...,2N) starting at So-0 nput: lengthofRandomWalk2N Output: samplePath: Array of length 2N+1 with the entire path of the random walk on 0,1,2,..,2N In def randomwalk(lengthofRandomwalk): ## WRITE YOUR OWN CODE HERE # HINT: USE np. random . choice ( ) TO SIMULATE THE INCREMENTS return samplePath In [ ]:...

  • 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,...

  • Problem 4 (5 pt) Compute a root of the function f(x) = x2-2 using the secant method with initial ...

    Problem 4 (5 pt) Compute a root of the function f(x) = x2-2 using the secant method with initial guess xo - 1.5 and xj 1 Choose a different initial guess and compute another root of the function f(x) Problem 4 (5 pt) Compute a root of the function f(x) = x2-2 using the secant method with initial guess xo - 1.5 and xj 1 Choose a different initial guess and compute another root of the function f(x)

  • 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...

  • 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) =

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