LU Factorization
lab code for homeunknown
plain_text
9 days ago
1.2 kB
1
Indexable
Never
#include<bits/stdc++.h> using namespace std; int main(){ //int n; //cin>>n; double a[10][10],x[10]; for(int i=1;i<5;i++){ for(int j=1;j<6;j++){ cin>>a[i][j]; } } //for(int i=1;i<5;i++){ // for(int j=1;j<6 ;j++){ // cout<<a[i][j]<<" "; // } // cout<<endl; //} float u11=a[1][1]; float u12=a[1][2]; float u13=a[1][3]; float u14=a[1][4]; float l21=a[2][1]/u11; float l31=a[3][1]/u11; float l41=a[4][1]/u11; float u22=a[2][2]-(a[2][1]/u11)*a[1][2]; float u23=a[2][3]-l21*a[1][3]; float u24=a[2][4]-l21*a[1][4]; float l32=(a[3][2]-l31*u12)/u22; float u33=a[3][3]-l31*u13-l32*u23; float u34=a[3][4]-l31*u14-l32*u24; float l42=(a[4][2]-l41*u12)/u22; float l43=(a[4][3]-l41*u13-l42*u23)/u33; float u44=a[4][4]-l41*u14-l42*u24-l43*u34; float y1=a[1][5]; float y2=a[2][5]-l21*y1; float y3=a[3][5]-l31*y1-l32*y2; float y4=a[4][5]-l41*y1-l42*y2-l42*y3; float x4=(y4-l41*y1-l42*y2-l43*y3)/u44; float x3=(y3-l31*y1-l32*y2-u34*x4)/u33; float x2=(y2-l21*y1-u23*x3-u24*x4)/u22; float x1=(y1-u12*x2-u13*x3-u14*x4)/u11; cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<endl; return 0; } /* 1 2 -1 1 6 -1 1 2 1 3 2 -1 2 2 14 1 1 -1 2 8 */
Leave a Comment