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
2.1 Summary In this part, you will create a figure, and use linear transformations (matrices) to ...
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...
For this project, each part will be in its oun matlab script. You will be uploading a total 3 m files. Be sure to make your variable names descriptive, and add comments regularly to describe what your code is doing and hou your code aligns with the assignment 1 Iterative Methods: Conjugate Gradient In most software applications, row reduction is rarely used to solve a linear system Ar-b instead, an iterative algorithm like the one presented below is used. 1.1...
linear algebra Remember we were able to express rotations and reflections, which are geometric transformations, using a linear transformation T, the coef- ficient matrix corresponding to the geometric transformation (r. y) (r', ) (a) What problem do you encounter with translations (r. y) (r+ h.y+k)? To handle this problem, We let the vector (x, y1 ) in R2 correspond to the vector (x1, y1, 1), and conversely. (In effect, we're projecting the :xy-plane onto the plane 1) introduce homogeneous coordinates....
Example 1: Least Squares Fit to a Data Set by a Linear Function. Compute the coefficients of the best linear least-squares fit to the following data. x2.4 3.6 3.64 4.7 5.3 y| 33.8 34.7 35.5 36.0 37.5 38.1 Plot both the linear function and the data points on the same axis system Solution We can solve the problem with the following MATLAB commands x[2.4;3.6; 3.6;4.1;4.7;5.3]; y-L33.8;34.7;35.5;36.0;37.5;38.1 X [ones ( size (x)),x); % build the matrix X for linear model %...
A unit cube as shown in Figure Q1 is undergoing the transformations described in (i) and (ii) respectively. Sketch the resultant object with coordinates of each vertex after each transformation. (a) Z (0,1,1) (1,1,1) (0,0,1) (1,0,1) (0,0,0) (1,1,0) (1,0,0) Figure Q1 Transformation (i) (6 marks) 1. A Uniform scale by a factor of 2 2. Followed by a rotation about the-axis in counter-clockwise direction by 90 degrees 3. Followed by a transformation moving in the direction of < 2, 1,...
In this exercise, you will create a function trough_plot which will plot the cross-section of a trough outlines by the functions y0 and y. Input variables: x – a vector representing the x co-ordinates for the outline of the trough. y – a vector representing the y co-ordinates for the bottom of the trough. y0 – a vector representing the y co-ordinates for the top of the trough. Output variable: n/a – this function has no output variables. Process: The...
please do the number 2 1. Construct affine linear transformations to do the following to the square in Figure 4.la (a) Rotate the square 180° counterclockwise around the origin (in the plane) width. unchanged) (b) Move the square 7 units to the right, 3 units up, and double its (c) Make the vertical lines of the square slant at a 45 angle (height (d) Reflect the square about the y-axis. u Au + b, giving A and b. 4.la for...
You may use a calculator and/or computer to carry out calculations. However you must show a sufficient amount of work to clearly communicate your solution to the reader. [A] Consider the L-shaped polygon with vertices: (-1,0), (1,0), (1,1), (0,1), (0,3), and (-1,3) shown to the right. Find the standard matrix for each of the transformations described below. Then use matrix multiplication to obtain the coordinates for the 6 vertices that result from applying the transformation to the vertices of the...
c++ help please! Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces. Create a function to print the board on the screen using a nested for loop. The function header is: void printBoard (char board [][3]) Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false....
MATLAB Any guidance is appreciated! Use meshgrid() function to create 2D grid coordinates with x- and y- coordinates both from -12 to +12 in the increment 1/32. B) Calculate the function z shown below at every (x, y) point and store the result in a 2D array z Z = sin(squareroot x^2 + y^2)/ squareroot x^2 + y^2 c) Find the maximum value in array z and its corresponding index. The array may have more than one maximum points with...