Question

In: Advanced Math

Write a MATLAB code for the conjugate gradient method and apply it to solve the system...

Write a MATLAB code for the conjugate gradient method and apply it to solve the system Hx = b, where H is the n×n Hilbert matrix, and b is A times the vector of all ones, for (a) n = 4; (b) n = 8. Compare your numerical solutions with the exact solution (which is the vector of all ones), and report your numerical errors.

Solutions

Expert Solution


%%Matlab code for conjugate gradient method
clear all
close all

%Running the program with given parameters
%All A, x0, b and epsilon values
%Hilbert matrix of size (4X4)
A=hillbrt(4);
xx=ones(4,1);
b=A*xx;
x0=zeros(4,1);

fprintf('Example code for Conjugate gradient \n')
fprintf('The A matrix is \n')
disp(A)

fprintf('The b matrix is \n')
disp(b)


%Running the program with conjugate gradient function
[x,iter]=conjugategradient(A,x0,b);

fprintf('The solution matrix using conjugate gradient function is \n')
disp(x)

fprintf('Error in conjugate gradient method is %e.\n',norm(x-A\b))
  
%Running the program with given parameters
%All A, x0, b and epsilon values
%Hilbert matrix of size (4X4)
A=hillbrt(8);
xx=ones(8,1);
b=A*xx;
x0=zeros(8,1);

fprintf('Example code for Conjugate gradient \n')
fprintf('The A matrix is \n')
disp(A)

fprintf('The b matrix is \n')
disp(b)


%Running the program with conjugate gradient function
[x,iter]=conjugategradient(A,x0,b);

fprintf('The solution matrix using conjugate gradient function is \n')
disp(x)

fprintf('Error in conjugate gradient method is %e.\n',norm(x-A\b))  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Function for creating Hilbert Matrix
function A=hillbrt(n)
%creating Hilbert matrix of size nXn
    for i=1:n
        for j=1:n
            A(i,j)=1/(i+j-1);
        end
    end
end
  

%%Function for Conjugate Gradient method
function [x,iter]=conjugategradient(A,x0,b)
    epsi=10^-8;
    x=x0;
    r_old=b-A*x;
    p=r_old;
    epsi=epsi*norm(r_old);
    cnt=0;
    while norm(r_old)>epsi

        cnt=cnt+1;
        alpha=(r_old'*r_old)/(p'*A*p);
        x=x+alpha*p;
        r_new=r_old-alpha*A*p;
        beta=(r_new'*(r_new-r_old))/(r_old'*r_old);
        p=r_new+beta*p;
        r_old=r_new;

    end
    iter=cnt;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

       %%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%


Related Solutions

Write your own MATLAB code to solve the system 10 − x + sin(x + y)...
Write your own MATLAB code to solve the system 10 − x + sin(x + y) − 1 = 0 8y − cos2 (z − y) − 1 = 0 12z + sin(z) − 1 = 0 using a residual tolerance of 10^−6 and the initial guess, ~x 0 = [0.1, 0.25, 0.08]^T . Print out the values for x, y and z for each iteration in a table similar to the one you created for the problems of the...
solve in MATLAB and screenshot code ?′′ −??′ +??= ???(????−?????)
solve in MATLAB and screenshot code ?′′ −??′ +??= ???(????−?????)
Solve using matlab code!! Use the graphical method to solve 4x1 − 8x2 = −24 x1...
Solve using matlab code!! Use the graphical method to solve 4x1 − 8x2 = −24 x1 + 6x2 = 34 Check your results by substituting them back into the equations.(include this on the code)
write a matlab code to simulate fiber optics communication system on matlab simulink
write a matlab code to simulate fiber optics communication system on matlab simulink
write a matlab code of current distribution in wire antenna using method of moments (pocklington)
write a matlab code of current distribution in wire antenna using method of moments (pocklington)
matlab code that performs overlap add method.
matlab code that performs overlap add method.
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
Solve Kepler's Equations Using Euler's Method in matlab.
Solve Kepler's Equations Using Euler's Method in matlab.
Use Matlab to solve the system x2+xy3=9 , 3x2y-y3 =4 using Newton's method for nonlinear system....
Use Matlab to solve the system x2+xy3=9 , 3x2y-y3 =4 using Newton's method for nonlinear system. use each of initial guesses (x0,y0)=(1.2,2.5), (-2,2.5), (-1.2,-2.5), (2,-2.5) observe which root to which the method converges, the number of iterates required and the speed of convergence. Write the system in the form f(u) = 0, and report for each case the number of iterations needed for ||f(u)||2≤ 10-12−.
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT