Guass elimination method and guass column principal Element Elimination Method for Solving Linear Equations

Source: Internet
Author: User
# Include <stdio. h>
# Include <math. h>
# Include <conio. h>
# Define N 3
Void guass (double A [] [n + 1], double X [])
{
Double L [N];
Int I, J, K;
L [0] = 1.0;
For (I = 0; I <N-1; I ++)
{
If (FABS (A [I] [I]) <0.000001)
{
Printf ("cannot solve! /N ");
Return;
}
For (j = I + 1; j <n; j ++)
L [J] = A [J] [I]/A [I] [I];
For (j = I + 1; j <n; j ++)
For (k = I + 1; k <n + 1; k ++)
A [J] [k] = A [J] [k]-l [J] * A [I] [k];
} For (I = N-1; I> = 0; I --)
{
X [I] = A [I] [N];
For (j = I + 1; j <n; j ++) x [I] = x [I]-A [I] [J] * X [J];
X [I] = x [I]/A [I] [I];
}
}
Void main ()
{
Int I, J;
Double X [N];
Double S [N] [n + 1] ={{ 0.101, 2.304, 3.555, 1.183 },
{-1.347, 3.712, 4.623, 2.137 },
{-2.835, 1.072, 5.643, 3.035}
};
For (I = 0; I <n; I ++)
{
For (j = 0; j <= N; j ++) printf ("% 5.3f", s [I] [J]);
Printf ("/N ");
}
Guass (S, X );
For (I = 0; I <n; I ++) printf ("X [% d] = % 5.6f/N", I + 1, x [I]);
Getch ();
}/////////////////////////////////////// /// // # Include <stdio. h>
# Include <math. h>
# Include <conio. h>
# Define N 3
// Guass column principal element elimination method
Int guassl (double ** A, double * X, int N)
{
Int I, J, K, numl, * h, T;
Double * l, Max;
L = new double [N];
H = new int [N];
For (I = 0; I <n; I ++) H [I] = I; // row mark
For (I = 1; I <n; I ++)
{
Max = FABS (A [H [I-1] [I-1]);
Numl = I-1;
// Maximum value of column element
For (j = I; j <n; j ++)
{
If (FABS (A [H [J] [I-1])> MAX)
{
Numl = H [J];
Max = FABS (A [H [J] [I-1]);
}
}
If (max <0.00000000001) return 0;
// Exchange the row number
If (numl> i-1)
{
T = H [I];
H [I] = H [numl];
H [numl] = T;
}
For (j = I; j <n; j ++) L [J] = A [H [J] [I-1]/A [H [I-1] [I-1];
For (j = I; j <n; j ++)
For (k = I; k <n + 1; k ++) A [H [J] [k] = A [H [J] [k]-l [J] * A [H [I-1] [k];
}

For (I = n-1; I> = 0; I --)
{
X [H [I] = A [H [I] [N];
For (j = I + 1; j <n; j ++) X [H [I] = x [H [I]-A [H [I] [J] * X [H [J];
X [H [I] = x [H [I]/A [H [I] [I];
}
// Restore the value of X [I]
For (I = 0; I <n; I ++) L [I] = x [H [I];
For (I = 0; I <n; I ++) x [I] = L [I];
// Delete temporary array Variables
Delete [] L;
Delete [] h;
Return 1;
}
Void main ()
{
Int I, J;
Double X [N];
Double S [N] [n + 1] = {2, 8, 2, 14 },
{1, 6,-1, 13 },
{2,-1, 2, 5}
};
Double ** U;
U = new double * [N];
For (I = 0; I <n; I ++) U [I] = s [I];
For (I = 0; I <n; I ++)
{
For (j = 0; j <= N; j ++) printf ("% 5.3f", s [I] [J]);
Printf ("/N ");
}
Guassl (u, X, N );
For (I = 0; I <n; I ++) printf ("X [% d] = % 5.6f/N", I + 1, x [I]);
Delete [] U;
Getch ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.