Question

2.1 Summary In this part, you will create a figure, and use linear transformations (matrices) to move the figure around the s

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

Matlab code for conjugate gradient method clear all close all Running the program with given parameters All A, x0, b and epsifprintf nSolution using conjugate gradient method for different n for i= 1:6 n-2* (rti); %creating tridiagonal matrix A A-fulExample code for Con jugate gradient The A matrix is The b matrix is The solution matrix using manual code is 0.75 -0.3333333

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

Add a comment
Know the answer?
Add Answer to:
2.1 Summary In this part, you will create a figure, and use linear transformations (matrices) to ...
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
  • 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 incluing,3D Graphics. Also, this poster presentation does a pretty good job explaining the same...

  • For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...

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

  • 1. Construct affine linear transformations to do the following to the square in Figure 4.la (a) R...

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

  • D. We will use pinv function to find a linear regression model to map the measurement data to tru...

    d. We will use pinv function to find a linear regression model to map the measurement data to truth data. Our model will be Y-AX. Y is the truth data matrix Y [ yı; y2i yai ya; ysi yg: y7 ]. A is the tmeasurement data matrix A=[ 1 mh; 1 m2. 1 mai 1 m4; 1 ms: 1 mt 6:1 m7 I . and X-[Xo: x1] is the coefficient of your regression model. Truth Data yn -1 0 Measurement...

  • Consider the structure in Figure 3. We will study the forces in the bars of this...

    Consider the structure in Figure 3. We will study the forces in the bars of this structure for various a. Develop the equations of static equilibrium and write them in matrix form. Take F=2 N. The stiffness matrix A should be in terms of . • Hint 1: The supporting structure at E is a pin and at D is a roller, the load on A is F and on B is 5. The length of member ED is 1m...

  • In this exercise, you will create a function trough_plot which will plot the cross-section of a...

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

  • Consider a medical device where blood is circulated in the annular space between two coaxial cyli...

    do the second prob pic Consider a medical device where blood is circulated in the annular space between two coaxial cylinders (Figure 1). The inner cylinder (radius cylinder (radius R) is rotating with constant anacibeNewtonian fluid (density o. are infinitely long, and that blood behaves as an tncompcessiole viscosity . Ignore the effect of gravity. whereas the outer velocity oAssume that the cylinders 1a. Write a conservation equations appropriate to determine the fluid velocity profile insido the annular gap, along...

  • Matlab: PART 3 - Applications Create a script for each of the following four application problems...

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

  • please use matlab Goal: Solve for the loads of the chain saw under different loading conditions....

    please use matlab Goal: Solve for the loads of the chain saw under different loading conditions. The loads on a chain saw as it cuts a log can be represented with the following equations: Ar +B -R cos(60) 0 My-W +By - R sin(60)0 Ar is the reaction load in the x-direction at point A (lb) Ay is the reaction load in the y-direction at point A (Ib) W is the weight of the chain saw (lb) R is the...

  • Exercise 1. [30 points.] A cloud of ultracold atoms trapped in an optical lattice is described...

    Exercise 1. [30 points.] A cloud of ultracold atoms trapped in an optical lattice is described by the Hamiltonian [4.N2-a 4.22 -a -a 4.12-a Ho = fr -a -- 0 - 4.12 -a 4.22 4.N2 where fr is the recoil frequency, a is the normalized lattice depth, and N is the number of momentum states used to approximate the atom's wave function. Define a MATLAB Function H-hamiltonian (fR, al,n) to perform the following: a) Use comments, i.e. %, to desribe...

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