Question

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 v
1.3 Rewrite as Function Once, you have completed the above section, rewrite the algorithm as a function (in a separate file)
You can then use this function in your script by using the command plot-points(P) Where P is the matrix created above. (It wi
2.6 Translate to Origin Create a matrix that represents the following transformation Tender(x, y, y cy, Where c,y are the coo
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 Set up Linear System ● Create an N × N matrix M. Set A = M1 M, A will be the matrix we work with. N can be any value for this step Create a vector b with all values equal to some constant (your choice). This will be the right hand side of the linear system. Create a vector ro of all zeros. This will be our "initial guess 1.2 Implement Algorithm In the same script, implement the following algorithm: Given: A, ro.b,e 10- while oldl E do Told n end while Result: Here k ← k + 1 means that we are assigning the value k + 1 to the variable k. You can test that your script is correct by checking that Ar b.
1.3 Rewrite as Function Once, you have completed the above section, rewrite the algorithm as a function (in a separate file) so that the following command will solve the linear system Ar-bin your original script: x-ConjugateGradient (A, o, b) I will run this exact command to check that your function is correct. Remember that the file name needs to be the same as the function name. In your original script, display the error lr- Al after solving the linear system. 1.4 Bonus: Count Iterations Modify the function to count the number of iterations needed for the loop to finish and return as a second argument. The following command should work in your original script: x, iter ConjugateGradient (A, ro. b) In your original script, use a for-loop to run your script for N-2,26,27,2,2,2o. Create a vector that stores the ratio of #iterations/N for each value of N. Also create a vector that stores the error which is computed as r-Abll for each N. Display both of these vectors after all of the values have been computed. Tip: Loop through the values i-1:5, and use i to compute N 2 Linear Transformations: Computer Graphics Linear Transformations play a very important part in computer graphics due to the fact that they can be used to represent rotation, scaling, as well as translation of vectors. For this problem, we will think of points in a plane, as a vectors in R2 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.7starting at the beginning of the section up to, but not including, 3D Graphics. Also, this poster presentation does a pretty good job explaining the same concepts http://web.csulb.edu/~jchang9/m247/m247-sp12 Daniel.Kris James.Walter.pdf 2.2 Create Figure in Plane Recreate the image below by creating a matrix whose first row is the x-coordinates of the vertices in clockwise order, and whose second row are the corresponding y-coordinates. Place the following function at the bottom of your script to plot the points in the matrix function plot-points(points) figure(1) fill(points(1, :), points(2, set (gca,'color". [0, o, o)); axis ([-10, 10, -10, 10]); :),'g'); end
You can then use this function in your script by using the command plot-points(P) Where P is the matrix created above. (It will look a bit different than shown). 1.8 1.6 1A 1.2 08 0.4 02 1 08 06 04 02 0 02 04 06 8 2.3 Homogeneous Coordinates Convert the points stored in your matrix to homogeneous coordinates by adding a row of all ones to the bottom of your matrix of coordinates 2.4 Translation Create a matrix that represents the following transformation i.e. Translation in the y direction by one unit. You can test that you have the correct matrix by applying the transformation to each column in the matrix of homogeneous coordinates and then plotting the new points using the function from above. Use a for-loop to apply this transformation 8 times and plot the result each iteration using the function from part 1 2.5 Find Center Write a function (at the bottom of your script) that will compute the center of your figure The x-coordinate of the center is just the average of each of the x-coordinates (of the vertices) similarly the y-coordinate of the center is the average of each of the y-coordinates. You should be able to use the function in the following way [centerx, center-y] -FindCenter (P) Where P is the matrix of homogeneous coordinates.
2.6 Translate to Origin Create a matrix that represents the following transformation Tender(x, y, y cy, Where c,y are the coordinates of the center found in the previous part. You can test that you have the correct matrix by applying the transformation to each column in the matrix of homogeneous coordinates and then plotting the new points using the function from part 1 you should see the figure move to be centered at the origin. Comment out any tests, before moving on to the next section 2.7 Rotation Create a matrix (Linear Transformation) that has the effect of rotating the figure by 90 Apply the following transformation to cach of the columns in the matrix of homogeneous coordinates and then plot the new points TwnterTwy Tornter Aside: Check what happens if only Too is used. The above transformation is used so that the center is kept in the same place 2.8 Bonus Make the figure travel in a complete square
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:
For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...
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...

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

  • 2 Homogeneous coordinates Recall that an affine function is of the form f^x) Mx + t for a matrix M and vector t. Homoge...

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

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

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

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

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

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

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

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

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