Thread: jacobi iterative residue

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    22

    jacobi iterative residue

    void prob2(void)
    {
    /* In void prob2(voud) you are asked to solve the system of
    linear equations:

    a[i][0]*x[0]+a[i][1]*x[1]+...+a[i][4]*x[4] = b[i]
    for i=0, 1, ..., 4 (2a)

    using the Jacobi iterative method, where a[5][5] and b[5]
    are defined in prob2().
    In the method, one starts with an approximate x0[] and
    obtains an improved solution x1[]:

    x1[i] =1/a[i][i] *(b[i] -(sum on j, except j=i)a[i][j]*x0[j])
    for i=0,...,4. (2b)

    Perform 20 iterations to find an improved approximate solution
    x1[] starting from the initial guess x0[0]=x0[1]=...=x0[4]=0.0.
    After each iteration, print the values of x1[] by using %15.4e.
    Finally, compute the residue of x1[]: residue[]=(b[]-a[][]*x1[])
    and print residue using %12.4e.*/
    double a[5][5]={ 5., 0., 1., 0., -1.,
    0., 4., 3., -1., 0.,
    0., 0., 7., 1., -5.,
    1., -1., 0., 7., 0.,
    1., 1., 0., 0., -8.};
    double b[5]={8., -3., -4., 2.,-1.}, x0[5]={0.}, x1[5], sig;
    int i, j, k;
    /* Put prob2 statements here */
    for(k=0; k<20; k++)
    {
    for(i=0; i<5; i++)
    {
    sig=0.;
    for(j = 0; j <5; j++)
    {
    if(j!=i)
    {
    sig = a[i][j] * x0[j];
    }
    }
    x1[i] = 1/a[i][i] *(b[i] - sig);
    printf("x1[%d] = %15.4e\n", i, x1[i]);
    }
    for(j=0; j<5; j++){
    x0[j]=x1[j];
    }
    printf("\n");
    }
    return;
    }
    void prob3(void)
    {
    using the Jacobi iterative method, where a[5][5] and b[5]
    are defined in prob2().
    In the method, one starts with an approximate x0[] and
    obtains an improved solution x1[]:

    x1[i] =1/a[i][i] *(b[i] -(sum on j, except j=i)a[i][j]*x0[j])
    for i=0,...,4. (2b)

    Perform 20 iterations to find an improved approximate solution
    x1[] starting from the initial guess x0[0]=x0[1]=...=x0[4]=0.0.
    After each iteration, print the values of x1[] by using %15.4e.
    Finally, compute the residue of x1[]: residue[]=(b[]-a[][]*x1[])
    and print residue using %12.4e.*/
    double a[5][5]={ 5., 0., 1., 0., -1.,
    0., 4., 3., -1., 0.,
    0., 0., 7., 1., -5.,
    1., -1., 0., 7., 0.,
    1., 1., 0., 0., -8.};
    double b[5]={8., -3., -4., 2.,-1.}, x0[5]={0.}, x1[5], sig;
    int i, j, k;
    /* Put prob2 statements here */
    for(k=0; k<20; k++)
    {
    for(i=0; i<5; i++)
    {
    sig=0.;
    for(j = 0; j <5; j++)
    {
    if(j!=i)
    {
    sig = a[i][j] * x0[j];
    }
    }
    x1[i] = 1/a[i][i] *(b[i] - sig);
    printf("x1[%d] = %15.4e\n", i, x1[i]);
    }
    for(j=0; j<5; j++){
    x0[j]=x1[j];
    }
    printf("\n");
    }
    return;
    }
    how do you find the residue of this?
    when i compile what i have so far i get
    x1[0] = 1.6000e+00
    x1[1] = -7.5000e-01
    x1[2] = -5.7143e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

    x1[0] = 1.6250e+00
    x1[1] = -7.5000e-01
    x1[2] = -4.8214e-01
    x1[3] = 2.8571e-01
    x1[4] = 1.2500e-01

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean, "how do you compute the residue?" The formula for the residue is given right there in the bit that you posted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using both a Recursive and Iterative Functions
    By belkins in forum C Programming
    Replies: 12
    Last Post: 11-02-2008, 12:31 PM
  2. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  3. splitting linked list recursive vs. iterative
    By Micko in forum C Programming
    Replies: 7
    Last Post: 03-17-2005, 05:51 PM
  4. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  5. iterative equation function
    By whatman in forum C Programming
    Replies: 6
    Last Post: 07-31-2003, 08:43 AM