Question

In: Computer Science

IDENTIFY THE ERROR SO THAT THE CODE WILL HAVE NO ERROR USING SUCCESSIVE OVER RELAXATION METHOD...

IDENTIFY THE ERROR SO THAT THE CODE WILL HAVE NO ERROR USING SUCCESSIVE OVER RELAXATION METHOD

function [x,numIter,omega] = gaussSeidel(func,x,maxIter,epsilon)

% Solves Ax = b by Gauss-Seidel method with relaxation.

% USAGE: [x,numIter,omega] = gaussSeidel(func,x,maxIter,epsilon)

% INPUT:

% func = handle of function that returns improved x using

% x = starting solution vector

% maxIter = allowable number of iterations (default is 500)

% epsilon = error tolerance (default is 1.0e-9)

% OUTPUT:

% x = solution vector

% numIter = number of iterations carried out

% omega = computed relaxation factor

if nargin < 4; epsilon = 1.0e-9; end

if nargin < 3; maxIter = 500; end

k = 10; p = 1; omega = 1;

for numIter = 1:maxIter

xOld = x;

x = feval(func,x,omega);

dx = sqrt(dot(x - xOld,x - xOld));

if dx < epsilon; return; end

if numIter == k; dx1 = dx; end

if numIter == k + p

omega = 2/(1 + sqrt(1 - (dx/dx1)ˆ(1/p)));

end

end

error(’Too many iterations’)

*USING MATHLAB

Solutions

Expert Solution

The function is done in Matlab and corrected for errors. Do let me know if your inputs create an error.

function [x,numIter,omega] = gaussSeidel(func,x,maxIter,epsilon)
    % Solves Ax = b by Gauss-Seidel method with relaxation.
    % USAGE: [x,numIter,omega] = gaussSeidel(func,x,maxIter,epsilon)
    % INPUT:
    % func = handle of function that returns improved x using
    % x = starting solution vector
    % maxIter = allowable number of iterations (default is 500)
    % epsilon = error tolerance (default is 1.0e-9)
    % OUTPUT:
    % x = solution vector
    % numIter = number of iterations carried out
    % omega = computed relaxation factor
    
    if nargin < 4
        epsilon = 1.0e-9;
    end
    
    if nargin < 3
        maxIter = 500; 
    end
    
    k = 10; p = 1; omega = 1;
    for numIter = 1:maxIter
        xOld = x;
        x = feval(func,x,omega);
        dx = sqrt(dot(x - xOld,x - xOld));
    
        if dx < epsilon
            return;
        end
        
        if numIter == k
           dx1 = dx; 
        end
        
        if numIter == k + p
            omega = 2/(1 + sqrt(1 - (dx/dx1)^(1/p)));
        end
    end
end


Related Solutions

Code in Java Successive Over-Relaxation (SOR) method.
Code in Java Successive Over-Relaxation (SOR) method.
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method is not static in class StaticInitializationBlock, please define the main method as:    public static void main(String[] args) This is what I'm working on class A { static int i; static { System.out.println(1); i = 100; } } public class StaticInitializationBlock { static { System.out.println(2); } public static void main(String[] args) { System.out.println(3); System.out.println(A.i); } }
For each of the following Visual Basic code snippets, identify the syntax error.   If intX >...
For each of the following Visual Basic code snippets, identify the syntax error.   If intX > 100   lblResult.Text = "Invalid Data" End If   Dim str As String = "Hello" Dim intLength As Integer intLength = Length(str) If intZ < 10 Then   lblResult.Text = "Invalid Data"   Dim str As String = "123" If str.IsNumeric Then   lblResult.Text = "It is a number." End If   Select Case intX   Case < 0     lblResult.Text = "Value too low."   Case > 100     lblResult.Text = "Value too high."   Case Else     lblResult.Text = "Value...
1. Using the following code identify the fault. 2. Using the following code indentify a test...
1. Using the following code identify the fault. 2. Using the following code indentify a test case that results in an error, but not a failure. public static int lastZero (int[] x) { //Effects: if x == null throw NullPointerException //else return the index of the last 0 in x. //Return -1 if 0 does not occur in x for (int i = 0; i < x.length; i++) { if (x[i] == 0) { return i; } } return -1;...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the...
QUESTION: Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line … System.exit(0); skeleton code: /**...
I have an error on my code. the compiler says 'setLastName' was not declared in this...
I have an error on my code. the compiler says 'setLastName' was not declared in this scope. this is my code : #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; class Employee {    public :               Employee(string fname, string lname, int salary)        {            setFirstName(fname);            setLastName(lname);            setMonthlySalary(salary);        }               void setFirstName(string fname)        {       ...
Generate a transmitted dada sequence for “101010” using Hamming code? If an error occurs in the...
Generate a transmitted dada sequence for “101010” using Hamming code? If an error occurs in the third bit, the received data is 101011”, how would Hamming technique fix it?
Discuss how dress code standards for health care companies have changed over the years. Identify some...
Discuss how dress code standards for health care companies have changed over the years. Identify some of the current standards that you believe will change in the near future and explain why.
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT