Jacobi iteration! Have 90% of the code done need QUICK help please!

This is a discussion on Jacobi iteration! Have 90% of the code done need QUICK help please! within the C Programming forums, part of the General Programming Boards category; I need the "//TO DO" part near the end, im very stuck on it... thanks in advance! Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    3

    Jacobi iteration! Have 90% of the code done need QUICK help please!

    I need the "//TO DO" part near the end, im very stuck on it... thanks in advance!

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include<stdlib.h>
    
    
    
    
    /*
      Solving a system of linear equations using the Jacobi method.
      This program reads in data of the form
      3
      4.0  0.3  0.5  1.0
      2.0  4.0  0.8  2.0 
      1.0  0.6  9.0  3.0
      where the first number N (in this case 3) indicates that the next
      N lines will be an augmented matrix N x N+1 
      The N x N part of the augmented matrix is the matrix of coefficients S
      of vector X, and the last column of the augmented matrix is vector T,
      such that SX = T
      This program uses the Jacobi method to iteratively solve for X.
     */
     
     
    #define N 8
    #define EPSILON 0.0001
    
    
    
    
    
    
    
    
    void in(double s[][N], double t[], int * n);
    void out2D(char * what, double s[][N], int n);
    void out1D(char * what, double t[], int n);
    void jacobi(double s[][N], double x[], double t[], int n);
    void mult(double a[][N], double b[], double result[], int n) ;
    char * check(char * name, double sx[], double t[], int n);
    main()
    {
       char name[25] = "Jacobi" ;
    double s[N][N], x[N], t[N], sx[N] ; 
    int i, j, n,q;
    in(s, t, &n);
    printf("-----------------------------------------\n") ;
    printf("SOLVING S * X = T USING THE JACOBI METHOD\n") ;
    printf("-----------------------------------------\n") ;
    out2D("S", s, n);
    /*
    jacobi(s, x, t, n);
    mult(s, x, sx, n);
    out1D("Jacobi S * X", sx, n);
    out1D("original T", t, n);
    printf("%s\n", check(name, sx, t, n));*/
    printf("\n\n\n");
    }
    
    
    /*
      Reads a value *n which is the dimension of a two dimensional 
      square matrix, and then reads the n x n matrix s and the vector t
      where the data have a shape as shown in the top comment.
     */
     
    void in(double s[][N], double t[],int * n)
    {
    int i,j,both,row,col,row2;
    double *matrixs, row3,col3;
    FILE *f1;
    
    
    f1=fopen("matrix20.txt", "r");
    fscanf (f1, "%lf", &both);
    n=&both;
    row3=both;
    col3=both;
    row2=both+1;
    row=both;
    col=both;
    
    
    matrixs = (double *) calloc (row*col, sizeof(double)); 
    
    
    for (i=0; i<4; i++)
    {
        for (j=0; j<3; j++)
        {
            fscanf (f1, "%lf", &matrixs[(i*both)+j]);
            s[i][j]=matrixs[j+(both*i)];
        }
    }
    
    
    /*
    for (j=1; j<row; j++)
    {
            fscanf (f1,"%lf", &t[both]);
    }
    */
    }
    
    
    /*
      Prints a two dimensional square matrix s which is n x n
      preceded by the name stored in 'what'.  For example, for the 
      input shown above and the matrix called 's':
      s :
          4.000000  0.300000  0.500000  
          2.000000  4.000000  0.800000  
          1.000000  0.600000  9.000000  
     */ 
    void out2D(char * what, double s[][N], int n)
    {
    int i,j;
    
    
    FILE *in1,*in2;
    char name [10], letter;
    in1 = fopen ("what.txt", "r");
    fgets (name, sizeof(name), in1);
    printf ("%s", name);
    fclose (in1);
    
    
    for (i=0; i<4; i++)
    {
    for (j=0; j<3; j++)       
        {
        printf ("%7.3lf", s[i][j]);
        }
    printf ("\n");
    } 
    }
    void out1D(char * what, double t[], int n)
    {
    
    
    int i;
    FILE *f1;
    f1=fopen("matrix20.txt", "r");
    for (i=3;i<12; i++)
    {
        if (i==3)
        {
            fscanf (f1, "%lf", &t[i-4]);
        }
        else 
            if (i==7)
            {
            fscanf (f1, "%lf", &t[i-7]);
            }
            else
                if (i==11)
                {
                fscanf (f1, "%lf", &t[i-10]);
                }
                else
                {
                fscanf (f1, "%*lf");
                }
     }
     }
    /*
    Runs the jacobi iterative solution to S X = T.
    The matrix S is given by s and is n x n.
    The vector X is given by x, which has n elements.
    The vector T is given by t, which has n elements.
    The iteration stops if all new elements differ by no more than
    EPSILON from the old elements.
    */
    void jacobi(double s[][N], double x[], double t[], int n)
    {
    int i, j, flag = 1, k;
    double new_x[N], sum;
    //                               TO DO
    }
    /*
    Multiplies an n x n matrix A by a vector X, i.e., an n x 1 matrix.
    The resulting vector is called result.
    */ 
    
    
    void mult(double a[][N], double x[], double result[], int n)
    {
    {
     int i, j, k, sum ;
     for (i = 0 ; i < n ; i++) {
     for (j = 0 ; j< n ; j++) {
          sum = 0 ;
              sum = sum + a[i][j] * x[i] ;
          result[i] = sum ;
     }
     }
    }
    }
    /*
    Checks if two vectors are 'close', meaning that each pair of
    corresponding elements differ by no more than EPSILON.
    It returns a string "Jacobi check fails" if the vectors are not close
    otherwise a string "Jacobi check is OK".
    */
    char * check(char * name, double sx[], double t[], int n)
    {
    int i;
    for(i = 0 ; i < n ; i++)
    if(fabs(sx[i] - t[i]) > 10 * EPSILON)
    return (strcat(name, " check fails"));
    return (strcat(name, " check is OK"));
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You can't just steal some code from the net spit it here and expect somebody to do your homework mate, this is not a homework service.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    me and a friend actually posted this about 4 days ago, got multiple people to help, and this is how far we got. We had a basic skeleton to start with and i cant get this final part. Go take a look.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all one says "a friend and I posted ....".

    Secondly, there is no way in hell anyone here will waste time to help you unless you explain what your code does, what you need it to do and what you have tried to make it work.

    If you don't know this by now, this is a good time to read the POSTING RULES.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    yes a friend and i, i dont know her password and she is not with me so i had to make my own account, here is our original thread: converting a one dim. array to a two dim. array

    Everything is explained there, sorry about not following the guidelines

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are really not helping yourself at all, expeditious responses like "Read there and figure it out" are not really helping you at this moment.

    You were offered valuable advice on that thread from quzah who is one of the most knowledgeable people here and refused to do it. You (or your friend) killed the thread not him. Secondly, there is no evidence that you are not stealing that other person's code by claiming to have worked on it with him. If you really had been working on it, you would have no problems explaining what your problem is.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > Have 90% of the code done need QUICK help please!
    Gotta love estimates like this.

    Sure, you've done all the easy I/O, but implementing jacobi() is where the rubber meets the road.

    Have you actually read something like this before hand?
    Jacobi method - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Part of the reason you need to post code is to show that you have made an effort to solve the problem yourself. However, of all the code you posted, most of it is unrelated to the problem at hand. The only bit that is relevant is:
    Code:
    void jacobi(double s[][N], double x[], double t[], int n)
    {
    int i, j, flag = 1, k;
    double new_x[N], sum;
    //                               TO DO
    }
    So what you're unfortunately really showing is that you have made practically no attempt to solve the problem yourself.
    The algorithm as listed in wikipedia looks plenty good enough to get started with.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-16-2012, 11:34 AM
  2. Jacobi Iteration Method
    By elleshlar in forum C Programming
    Replies: 6
    Last Post: 05-10-2011, 05:32 PM
  3. Jacobi algorithm problem... could wikipedia be wrong?
    By somekid413 in forum C Programming
    Replies: 2
    Last Post: 01-19-2010, 12:26 AM
  4. jacobi iterative residue
    By burninfrost296 in forum C Programming
    Replies: 1
    Last Post: 03-05-2009, 08:27 AM
  5. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21