Question

For the following two problems, use the built-in simple MATLAB matrix algebra rules, i.e. if you're...

For the following two problems, use the built-in simple MATLAB matrix algebra rules, i.e. if you're multiplying a matrix L by a vector xi, it's just L*xi. You do not have to code matrix multiplication in for loops.

  • Code up the Jacobi Method and use it to solve the large matrix of problem 3 from HW5. Return the iteration count to solve this problem and a plot of the solution.
  • Modify the Jacobi method to use the most current information (i.e. implement the Gauss-Seidel method) and solve problem 3 of HW5 once more. Again include the iteration count and solution plot.

Regarding convergence, I suggested examining the norm of the x vector (norm is the MATLAB function name). This could be absolute change in the form norm(x1-x0) (current iteration minus previous iteration); or relative change in the form norm(x1-x0)/norm(x0). These require different error thresholds (i.e. stopping criteria for the iterative loop) for good convergence.

Help with the code for this please. I can use the code to apply it to the problem from HW 5. just need the as much of the code as possible plsssssss

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

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

clc%clears screen
clear all%clears history
close all%closes all files
format long
A=randi([3,8],10,10);
b=randi([3,8],10,1);
jacobi(A,b,ones(10,1),1e-4,100)
function x = jacobi(A,d,x0,tol,Imax)   
% jacobi: implements the Jacobi iterative method
% for solving a system of linear equations.
% Input: A = square coefficient matrix (nxn)
% d = right hand side vector (nx1)
% x0 = initial guess (nx1) (default = zeros)
% tol = stop criterion (default = 10^-6)
% Imax = maximum iterations (default = 100)
% Output: Solution vector.
E=[];
if nargin<5, Imax=100; end
if nargin<4, tol=10^-6; end
[m,n]=size(A);
if m~=n, error('Matrix A must be square'); end
if nargin<3, x=zeros(n,1); else x=x0; end
b=zeros(n,1);
C=zeros(n,n);
k=0;
for i=1:n
b(i)=d(i)/A(i,i);
end
for i=1:n
for j=1:n
if i==j, C(i,j)=0; end
if i~=j, C(i,j)=A(i,j)/A(i,i); end
end
end
while true
x_old=x;
x=b-C*x;
k=k+1;
E(k)=norm((x-x_old)./x);
if k>=Imax || norm((x-x_old)./x)<tol
fprintf('Total Iterations are: %d\n',k);
plot(E);
return
end
end
end

Note: Brother According to Chegg's policy we are only allowed to answer first part if there are many. So, I request you to post other part as separate posts.

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
For the following two problems, use the built-in simple MATLAB matrix algebra rules, i.e. if you're...
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. 3x 25」LX2 (a) Perform three iterations for the following iterative methods using initial guess x0. Compute relative residual for each iteration. (You can use a calculator) · Jacobi method » Gauss...

    2. 3x 25」LX2 (a) Perform three iterations for the following iterative methods using initial guess x0. Compute relative residual for each iteration. (You can use a calculator) · Jacobi method » Gauss-Seidel method · SOR method with ω 1.2 (b) For each iterative method, express its iteration procedure in the following matrix form: In other words, determine B and c for (2). 2. 3x 25」LX2 (a) Perform three iterations for the following iterative methods using initial guess x0. Compute relative...

  • 1. [12 marks] In the following parts of this question, write a MATLAB code to solve a linear syst...

    1. [12 marks] In the following parts of this question, write a MATLAB code to solve a linear system A b (A is a square nonsingular matrix) using Jacobi and Gauss-Seidel algorithms. Do not use the built-in Matlab functions for solving linear systems (a) Write a Matlab function called Jacobi that consumes a square n x n matrix A, and an n x 1 vector b, and uses the Jacobi technique to solve the system Ax-b, starting with the zero...

  • just 1,2,4 Problem 1 Consider the linear system of equations Ax = b, where x €...

    just 1,2,4 Problem 1 Consider the linear system of equations Ax = b, where x € R4X1, and A= 120 b = and h= 0.1. [2+d -1 0 0 1 1 -1 2+d -1 0 h2 0 -1 2 + 1 Lo 0 -1 2+d] 1. Is the above matrix diagonally dominant? Why 2. Use hand calculations to solve the linear system Ax = b with d=1 with the following methods: (a) Gaussian elimination. (b) LU decomposition. Use MATLAB (L,...

  • Question 1 (10 marks) For a linear system Ax b with 1 0-1 A-1 2-1 2-13 and b4 18 compute by hand ...

    Question 1 (10 marks) For a linear system Ax b with 1 0-1 A-1 2-1 2-13 and b4 18 compute by hand the first four iterations with the Jacobi method, usg0 Hint: for the ease of calculation, keep to rational fractions rather than decimals. (10 marks) Question 2 For the same linear svstem as in Question 1. compute by hand the first three iterations with the Gauss Seidel method, us0 Hint: for the ease of calculation, keep to rational fractions...

  • I am new to Matlab so details would be appreciated! Use Matlab or any other Language...

    I am new to Matlab so details would be appreciated! Use Matlab or any other Language /Tool to implement the project work Algorithm for Gauss-Seidel Method to solve the linear (n x n) system Ax = b in matrix form is given by x(0) = initial vector x(4+1) = D-1 (b – Lx(k+1) – Ux(k)), k = 0, 1, 2, ...... where A = L+D+U. Here L, D, U are respectively lower, diag- onal and upper matrices constructed from A....

  • PLEASE USE MATLAB. The code is basically given to you with the algorithm above. Write a...

    PLEASE USE MATLAB. The code is basically given to you with the algorithm above. Write a program for solving the system Ar-b by using Gauss-Seidel iterative method discussed in class that uses a computer memory for one iterate only. This could be summarized as follows: Input n -- the size of the system, matrix A-(a(i,j), the vectoir b (b (1), . . . , b(n)), the intitial guess x (x(1), ..., x(n)) (you can use x=b) maximum number of iterations...

  • Using MATLAB, develop an M-file to determine LU factorization of a square matrix with partial pivoting....

    Using MATLAB, develop an M-file to determine LU factorization of a square matrix with partial pivoting. That is, develop a function called mylu that is passed the square matrix [A] and returns the triangular matrices [L] and [U] and the permutation P. You are not to use MATLAB built-in function lu in your codes. Test your function by using it to solve a system of equations listed below in part 3. Confirm that your function is working properly by verifying...

  • Use the attached Matlab code as a basis to solve the following ordinary differential equation usi...

    Question 1 QUESTION 2 Use the attached Matlab code as a basis to solve the following ordinary differential equation using Euler's method, with timestep of 0.1, from t-0to t-100. d)0) -0 - sin (5vt cos(у Plot y versus t from t=0 to t=100. How many local maxima are on this interval(do not include end points). Be careful to count them all! Answer should be an integer 1 w% Matlab code for the solution of Module 2 3 dt-9.1; %dt is...

  • A state space linear system is shown below. Use Matlab to solve the following problems. Requirement...

    A state space linear system is shown below. Use Matlab to solve the following problems. Requirement for project report: (1) Results; (2) Matlab code. dx1/dt=-x1(t)+u(t) dx2/dt=x1(t)-2x2(t)-x3(t)+3u(t) dx3/dt=-3x3(t) y(t)=-x1(t)+2x2(t)+x3(t)+u(t) (1) Assume the system has input u(t)=e-3t if t>t0 and zero initial state x(0)=[0;0;0]. Using the transition matrix obtained, compute the system’s output (analytical solution), and plot the output as a function of time (t within 0 to 10). (2) Using the function lsim to simulate the system’s output (analytical solution), and...

  • -Create a "f(p)" MATLAB function (name it Price.m) so that it outputs the vlue of the...

    -Create a "f(p)" MATLAB function (name it Price.m) so that it outputs the vlue of the function f(P)= P2_19P+ 24/P -Create a plot of f(P) over the range $0.00 to $20.00 -Finish code by writing own Secant Method to converge on the final solution for P: %%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%% % This script solves for the Equilibrium Price P, from the equation f(P)=0 % % THIS IS JUST THE INITIAL CODE FRAGMENT. YOU NEED TO COMPLETE THE SCRIPT. % *** ADD...

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