1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple C programming for gaussian

  1. Oct 6, 2014 #1
    1. The problem statement, all variables and given/known data

    make a gaussian elimination in C
    the one we used on matrix to find x1 x2 and x3
    if the matrix is 3x3
    2. Relevant equations


    3. The attempt at a solution

    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
    {
        int i,j,k,n;
        float A[3][4]={{3,0,6,1},{4,1,3,2},{6,3,0,3}};
        float x[3],c,sum=0;
        n=2;
        for(i=0; i<=n; i++);
        {
            for(j=0; j<=n+1; j++);
            {
                if(i>j);
                {
                    c=A[i][j]/A[j][j];
                    for(k=0; k<=n+1; k++);
                    {
                        A[i][k]=A[i][k]-A[j][k]*c;
                    }
                }
            }
        }
        x[n]=A[n][n+1]/A[n][n];
        for(i=n-1; i>=0; i--);
        {
            sum=0;
            for(j=i+1; j<=n; j++);
            {
                sum=sum+A[i][j]*x[j];
            }
            x[i]=(A[i][n+1]-sum)/A[i][i];
        }
        printf("\nThe solution is: \n");
            for(i=0; i<=n; i++);
            {
                printf("\nx%d=%f\t",i,x[i]);
          }
        return(0);
    }

     
    at the end, only x0 and x1 displayed as the solution
    should be x0 x1 and x2 right?
    and the result

    x0=-784.191895

    x1=0.000000

    seems to be incorrect
    anything that i did wrong?
    thx for reply
    [/I][/I][/I][/I][/I][/I][/I][/I][/I]
     
    Last edited: Oct 6, 2014
  2. jcsd
  3. Oct 6, 2014 #2

    jedishrfu

    Staff: Mentor

    Can you edit your post to use the [ code ] tags?

    They will preserve your code indentation without the need for an attachment.
     
  4. Oct 6, 2014 #3
    how do i do that?
    still new in this area
     
  5. Oct 6, 2014 #4

    phinds

    User Avatar
    Gold Member
    2016 Award

    put [C O D E] in front of your code and [/ C O D E] after it (but do NOT include the spaces in the tags. I had to insert them to keep this post from looking like code and hiding the tags.
     
  6. Oct 6, 2014 #5
    ok
    thanks
    just like that right?
     
  7. Oct 6, 2014 #6

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Make a 'gaussian' what in C? Your problem statement is a little vague.
     
  8. Oct 6, 2014 #7
    gaussian elimination in matrix
    sorry its not so clear
     
  9. Oct 7, 2014 #8
    problem solved
    by removing ; after for and if
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Simple C programming for gaussian
Loading...