Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A matlab question. THX! Problem 1: Poisson\'s Equation Consider the linear syste

ID: 3880734 • Letter: A

Question

A matlab question. THX!

Problem 1: Poisson's Equation Consider the linear system A,O-p, where An is an n × n matrix with 2's on the main diagonal, -1's directly above and below the main diagonal and 0's everywhere else. For example, A5 is 2 -1 0 0 0 1 2 -1 0 0 Ag=10-1 2-1 0 0 -1 2 -1 0 00-1 2 This is a discretized version of Poisson's equation d'o(r) = p which appears very often in physical applications. We will discuss discretizations and differential equations, including the origin of this matrix An, later in the class Construct the matrix Aioo in Matlab. (Look at the help file for the diag com- mand: you can make this matrix in a few lines of code.) In addition, make the 100x1 vector such that the jth entry of is defined according to the formula 56 1 C101 567j 101 COS (a) The Jacobi iteration method for this problem can be written as k-MPk-1-c. Note that k means the kth guess for , and it is an entire vector. It does not mean the kth entry of .) Find the largest (in magnitude) eigenvalue of M and save the magnitude of this eigenvalue in A1.dat. (b) Use Jacobi iteration to solve for . Your initial guess should be a 100 × 1 vector of all ones, and you should use a tolerance of 10-4. That is, you should stop when (Remember, || Ilo denotes the infinity norm. You can find the infinity norm of a vector x in Matlab with the command norm(x, Inf).)

Explanation / Answer

See the code below to create matrix "An" and vector "rho":

-------------------------------------------------

n=50; %dimensions of matrix

%elements of main diagonal
main_diag_elems=2*ones(1,n);

%create diagonal matrix
An=diag(elems);

%set elements above main diagonal
An((n+1):(n+1):(n*n-1))=-1;

%set elements below main diagonal
An((2):(n+1):(n*n-1))=-1;

%calculation of vector rho
j=1:n;
rho=2*(1-cos((23*pi)/51))*sin(((23*pi).*j)/51);

------------------------------------------------

a)Code:

-------------------------------------------

%Jacobi form in matrix notation
%phi=M*phi'+c; where phi is a 1*n order vector
%and c is also a 1*n order vector

phi=ones(1,n); %initial value of phi

C=5; %value of constant. You can specify accoring to yous.
c=5*ones(1,n); %vector of constants

%assuming M to be An, as M not specified.
%You can set or create M as required.
M=An;

%Concatenating M to c
Mc=[M,c'];
dlmwrite("A1.dat",Mc);

-----------------------------------

Note: Look at and do accordingly as per inline comments.

b) Code:

----------------------------------------

%Jacobi iteration
k=1; %first iteration
phi_k=phi; %value of phi for a given iteration
while(1)
phi=M*phi_k'+c;
if(norm((phi-phi_k),Inf)<=1e-4)
break;
end
phi_k=phi;
k=k+1;
end

%final iteration as column vector
dlmwrite("A2.dat",phi');
%total no. of iterations
dlmwrite("A3.dat",k);

------------------------------------------------

c), d) To be soved in the same way as a) and b)


Note: M needs to be specified clearly what it is and how to calculate.

Feel free to reach out if you have any doubts.
Rate if the answer was helpful.
Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote