%%Matlab code for conjugate gradient method
clear all
close all
%Running the program with given parameters
%All A, x0, b and epsilon values
A=[4 3 0;3 4 -1;0 -1 4];
b=[2;1;0];
epsi=10^-8;
x0=[0;0;0];
fprintf('Example code for Conjugate gradient \n')
fprintf('The A matrix is \n')
disp(A)
fprintf('The b matrix is \n')
disp(b)
x=x0;
r_old=b-A*x;
p=r_old;
epsi=epsi*norm(r_old);
cnt=0;
%loop for conjugate gradient method
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
fprintf('The solution matrix using manual code is \n')
disp(x)
%Running the program with conjugate gradient function
[x,iter]=conjugategradient(A,x0,b);
fprintf('The solution matrix using conjugategradient function is
\n')
disp(x)
fprintf('Error in conjugate gradient method is %e.\n',norm(x-A\b))
%Running program for iteration count
r=4;
fprintf('\nSolution using conjugate gradient method for different
n.\n')
for i=1:6
n=2^(r+i);
%creating tridiagonal matrix A
A=full(gallery('tridiag',n,-1,2,-1));
%considering exact solution value as 1
x=ones(n,1);
%creating b vector of length n
b=A*x;
%initial guess
x0=zeros(n,1);
%finding solution using conjugategradient
solution
[x1,iter]=conjugategradient(A,x0,b);
fprintf('\tFor n=2^%d the iteration count is
%d.\n',(r+i),iter)
fprintf('\tError in conjugate gradient method is
%e.\n\n',norm(x1-A\b))
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 %%%%%%%%%%%%%%
For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...
2.1 Summary In this part, you will create a figure, and use linear transformations (matrices) to move the figure around the screen. In the end, your figure should move up 8 steps, then turn and face left. Reference material for this part can be found in Linear Algebra, and its applications, David Lay, Section 2.7 starting at the beginning of the section up to, but not incluing,3D Graphics. Also, this poster presentation does a pretty good job explaining the same...
2.1 Summary In this part, you will create a figure, and use linear transformations (matrices) to move the figure around the screen. In the end, your figure should move up 8 steps, then turn and face left. Reference material for this part can be found in Linear Algebra, and its applications David Lay, Section 2.7 starting at the beginning of the section up to, but not including, 3D- Graphics. Also, this poster presentation does a pretty good job explaining the...
2 Homogeneous coordinates Recall that an affine function is of the form f^x) Mx + t for a matrix M and vector t. Homogeneous coordinates are frequently used to represent affine functions in robotics and 3D graphics. We define the function H by and if f-x) Mxtt where then C0 a. Some vectors are valid homogeneous representations of vectors, and some are not. Explain how to tell if some vector y-0 is the homogeneous representation of some other vector -y...
Matlab: PART 3 - Applications Create a script for each of the following four application problems (A7P3Alastname.m, A7P3Blastname.m, A7P3Clastname.m and A7P3Dlastname.m). Each program will call your function A7GAUSSlastname.m to solve the linear algebra problem. Use the following output statements to display formatted output in the command window: fprintf('In Assignment 7, Part 3a - Materials/Mixtures In(Note: Edit this line for each problem) fprintf ('\n Coefficient Matrix A : \n\n'); for i-1:size(A, 1) fprintf("%10.2f',A(i, :)); fprint'In') end fprintf'In Vector b: Inln') fprintf...
MATLAB SCRIPT PLEASE Matlab MATH 210 in 2020 Homework Assignment 8- Due 3/25, 11:59PM Each plot should have its own figure associated with it. In all questions, give the figure a title, and label the acis. Save your matlab script as drill 10.m Do not use the fplot command. 1. Plot the function f(x) = (x + 5)2 for -5 <<<10. Include a plot title, and label both aris. 2. Use the subplot command to make two plots of...
MATLAB Question: Write a script for the problem pictured. Please send the script, not just the printout. Make sure your script allows you to input a value for n so when prompted, in the command window, user types n value. The n value should be the f_k if k ==1 .... Please also send print outs if you can. BELOW IS A SAMPLE ANSWER THAT DID NOT PUT THE INPUT SCRIPT NEEDED FOR THIS QUESTION TO BE CORRECT. YOU CAN...
Please follow following scripts: Script for part A:Script for Part B: Matlab Exercises 1. Find the determinant (command: det(A)) and the condition number (command: cond(A)) of the Hilbert matrix H of order k (command: hilb(k)), for k = 1,2, ..., 10. Plot the determinant and the condition number as a function of k using a logarithmic scale for the vertical axis. Hint: The template to help you with this homework assignment is homework/hw15.m. 2. Determine if y is in the...
DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the following calculations. Your script will use the functions you create. Label all graphs appropriately. For this project, do not have your homemade functions fprintf anything, instead have all your fprintf commands within your script. Attach your published script file along with .m files for your functions. Exercise 1. A fundamental iterative method for finding the roots of equations of one-variable is known as Newton's...
Could someone explain these four promblems on matlab and if you do, could you write what you wrote on matlab I.e. on the command window or script. Also if you have written anything by hand can you write neatly. Also an explain of how you did it would be greatly appreciated. 1] 5 points) Write the following set of equations in Matrix form and use Matlab to find the solution. 50 -5x3-6x2 2x2 + 7x3-30 x1-7x3-50-3x2 + 5x1 [2] (10...
1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...