C language solution of 3-yuan 1-time equations using the most basic joint elimination method of _c language in primary and secondary schools

Source: Internet
Author: User
Tags abs

I've never learned linear algebra, but many of these algorithms are related to matrices, so we just have to learn to bite the bullet.
Recently I think I can write a linear equation set of procedures? Then thought of such a method, temporarily can only calculate 3 yuan, arbitrary element of the next continue to think. There are too many hard coded, I hope interested readers can give some suggestions to modify!

Copy Code code as follows:

#include "stdafx.h"//vs2010 need
#include "stdio.h"
#include "Stdlib.h"
#include "math.h"
Double x[3];//storage solution x,y,z, using arrays for easy output
X=1,y=2,z=3
Double a[3][4]=
//{
1,1,1,-6,
1,1,-1,0,
1,-1,1,-2
//};
x=2,y=5,z=10
Double a[3][4]=
{
1,2,3,-42,
2,-1,5,-49,
-1,3,-3,17
};

The first equation of a is x,y with 2 and 3 equations, and then the two equations of the equation are obtained and deposited into B
Double b[2][4];
The two equations of B are combined to eliminate Y and get X
Double c[1][4];


void Combineb (int i)//a[0] and a[1] or a[2] elimination Z
{

Double M0=abs (a[i][2]);
int b=a[0][2]*a[i][2]>0?-1:1;
Double Mi=b*abs (a[0][2]);
printf ("m0=%lf,mi=%lf\n", m0,mi);//Trial
for (int j=0;j<4;j++)
{
B[i-1][j]=a[0][j]*m0+a[i][j]*mi;
}


}

void Combinec ()//b[0] and b[1] elimination y
{

Double M0=abs (b[1][1]);
int b=b[0][1]*b[1][1]>0?-1:1;
Double M1=b*abs (b[0][1]);

for (int j=0;j<4;j++)
{
C[0][J]=B[0][J]*M0+B[1][J]*M1;
}


}
Display 4 parameters
void Show (double n[][4],int D1)
{
Char w[3]={' x ', ' y ', ' z '};

for (int i=0;i<d1;i++)
{
for (int j=0;j<3;j++)
{
printf ("%lf *%c +", n[i][j],w[j]);
}
printf ("%LF = 0\n", n[i][3]);
}
printf ("\ n \ nthe");
}

int _tmain (int argc, _tchar* argv[])
{
Show (a,3);
Combineb (1);
Combineb (2);
Show (b,2);
Combinec ();
Show (c,1);

Get x and then count the other numbers
X[0]= (0-c[0][3])/c[0][0];
X[1]= (0-b[0][3]-b[0][0]*x[0])/b[0][1];
X[2]= (0-a[0][3]-a[0][0]*x[0]-a[0][1]*x[1])/a[0][2];
Output results
for (int i=0;i<3;i++)
printf ("x[%d]=%lf\t", I,x[i]);
printf ("\ n");
System ("pause");
return 0;
}

2. Screenshot  

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.