Question

in MATLAB

3. Write a function called gauss_seidel that inputs an n x n matrix, A, a column vector, b, an initial guess xo), an error to

to within an accuracy of € = 10-5, with a maximum of N = 100 iterations. If your method succeeds, report the number of iterat

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

`Hey,

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

clc
clear all
close all
format long
A=[-2,1,0,0,0;2,10,-2,0,0;0,2,-4,0,0;0,0,0,2,1;0,0,0,-3,-7];
b=[-5;3;3;-7;-8];
[x,err,N]=gauss_seidel(A,b,[1;1;1;1;1],1e-5,100)
function [x,err,N] = gauss_seidel(A,d,x0,tol,Imax)
% gauss_seidel: 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.
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
C=A;
for i=1:n
C(i,i)=0;
end
for i=1:n
C(i,1:n)=C(i,1:n)/A(i,i);
end
b=zeros(1,n);
for i=1:n
b(i)=d(i)/A(i,i);
end
iter=0;
while true
x_old=x;
for i=1:n
x(i)=b(i)-C(i,:)*x;
if x(i)~=0
err(i)=abs((x(i)-x_old(i))/x(i));
end
end
iter=iter+1;
if iter>=Imax || norm((x-x_old)./x)<tol
err=norm((x-x_old)./x);
N=iter;
return
end
end
end

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
in MATLAB 3. Write a function called gauss_seidel that inputs an n x n matrix, A,...
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
  • 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...

  • Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...

    Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root ^n and the number of steps n taken to reach the solution. Use function mynewton.m to perform...

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

  • Write a function named “NewtonRaphson” that implements the Newton-Raphson method. The inputs to this function are...

    Write a function named “NewtonRaphson” that implements the Newton-Raphson method. The inputs to this function are the name of the function, name of the function’s derivative function, initial guess, maximum number of iterations, and tolerance for the relative convergence error. The output is the root. Use the problem in Homework #3 to test your function.    Hw 3 that we are pulling from %Newton-Raphson method to find upward velocity of a rocket clear all; clc; u=2200; %m/s m0=160000; %kg q=2680;...

  • Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...

    Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max.iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root n and the number of steps n taken to reach the solution. Use function mynewton.m to perform the...

  • Programming Language: MATLAB Problem 3: (5 Points) Write a function with the header: function [R] -myNewtonRaphson...

    Programming Language: MATLAB Problem 3: (5 Points) Write a function with the header: function [R] -myNewtonRaphson (f, fp, x0, tol) which takes as input f: a function handle fp: a function handle to the derivative of f (note how I do it in the test case). x0: the initial guess of the root tol: a tolerance above which the algorithm will keep iterating. Tips: . Be sure to include an iteration counter which will stop the while-loop if the number...

  • 3 Linear systems 18. Solve the linear system of equations using the Naive Gauss elimination metho...

    3 Linear systems 18. Solve the linear system of equations using the Naive Gauss elimination method x,+x: + x) = 1 +2x, +4x1 x 19. Solve the linear system of equations using the Gauss elimination method with partial pivoting 12x1 +10x2-7x3=15 6x, + 5x2 + 3x3 =14 24x,-x2 + 5x, = 28 20. Find the LU decomposition for the following system of linear equations 6x, +2x, +2, 2 21. Find an approximate solution for the following linear system of equations...

  • MATLAB QUESTION please include function codes inputed Problem 3 Determine the root (highest positive) of: F(x)=...

    MATLAB QUESTION please include function codes inputed Problem 3 Determine the root (highest positive) of: F(x)= 0.95x.^3-5.9x.^2+10.9x-6; Note: Remember to compute the error Epsilon-a after each iteration. Use epsilon_$=0.01%. Part A Perform (hand calculation) 3 iterations of Newton's Raphson method to solve the equation. Use an initial guess of x0=3.5. Part B Write your own Matlab function to validate your results. Part C Compare the results of question 1 to the results of question 2, what is your conclusion ?

  • Write a MATLAB function (IncNR) combining the capabilities of both the Incremental Search Method and the...

    Write a MATLAB function (IncNR) combining the capabilities of both the Incremental Search Method and the Newton-Raphson Method to find all the roots in a given function [xR, err, n] = IncNR(AF, xb, ed) AF = anonymous function xb = initial guess x bracket = [xL xU], where xL = lower x and xU = upper x ed = desired error Outputs: xR = vector of roots err = vector of errors corresponding to the roots n = vector of...

  • Consider the linear system 5x1 - 21 + X1 - 22 + x3 = 1 5.22...

    Consider the linear system 5x1 - 21 + X1 - 22 + x3 = 1 5.22 - 23 = 2 22 5 5x3 = 3 (a) Discuss the convergence of the iterative solutions of this system generated by the Jacobi and Gauss-Seidel methods, by considering their iterative matrices. (b) If both methods converge, which one of them converges faster to the exact solution of this system? (c) Starting with the initial approximation x(0) = [0,0,0], find the number of iterations...

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