10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔

 

 

6. 牛顿-科特斯求积公式,求定积分

C/C++ code
    
    
    
    
#include < stdio.h > #include < math.h > int NC(a,h,n,r,f) float ( * a)[]; float h; int n,f; float * r;{ int nn,i; float ds; if (n > 1000 || n < 2 ) { if (f) printf( " /n Faild! Check if 1<n<1000!/n " ,n); return ( - 1 );} if (n == 2 ){ * r = 0.5 * (( * a)[ 0 ] + ( * a)[ 1 ]) * (h); return ( 0 );} if (n - 4 == 0 ) { * r = 0 ; * r =* r + 0.375 * (h) * (( * a)[n - 4 ] + 3 * ( * a)[n - 3 ] + 3 * ( * a)[n - 2 ] + ( * a)[n - 1 ]); return ( 0 );} if (n / 2 - (n - 1 ) / 2 <= 0 )nn = n; else nn = n - 3 ;ds = ( * a)[ 0 ] - ( * a)[nn - 1 ]; for (i = 2 ;i <= nn;i = i + 2 )ds = ds + 4 * ( * a)[i - 1 ] + 2 * ( * a)[i]; * r = ds * (h) / 3 ; if (n > nn) * r =* r + 0.375 * (h) * (( * a)[n - 4 ] + 3 * ( * a)[n - 3 ] + 3 * ( * a)[n - 2 ] + ( * a)[n - 1 ]); return ( 0 );}main(){ float h,r; int n,ntf,f; int i; float a[ 16 ];printf( " Input the x[i](16):/n " ); for (i = 0 ;i <= 15 ;i ++ ) scanf( " %d " , & a[i]);h = 0.2 ;f = 0 ;ntf = NC(a,h,n, & r,f); if (ntf == 0 ) printf( " /nR=%f/n " ,r); else printf( " /n Wrong!Return code=%d/n " ,ntf); getch();}



7.雅克比迭代,求解方程近似解

C/C++ code
    
    
    
    
#include < stdio.h > #include < math.h > #define N 20 #define MAX 100 #define e 0.00001 int main(){ int n; int i,j,k; float t; float a[N][N],b[N][N],c[N],g[N],x[N],h[N]; printf( " /nInput dim of n: " ); scanf( " %d " , & n); if (n > N) { printf( " Faild! Check if 0<n<N!/n " ); getch(); return 1 ; } if (n <= 0 ) {printf( " Faild! Check if 0<n<N!/n " ); getch(); return 1 ;} printf( " Input a[i,j],i,j=0…%d:/n " ,n - 1 ); for (i = 0 ;i < n;i ++ ) for (j = 0 ;j < n;j ++ ) scanf( " %f " , & a[i][j]); printf( " Input c[i],i=0…%d:/n " ,n - 1 ); for (i = 0 ;i < n;i ++ )scanf( " %f " , & c[i]); for (i = 0 ;i < n;i ++ ) for (j = 0 ;j < n;j ++ ) { b[i][j] =- a[i][j] / a[i][i]; g[i] = c[i] / a[i][i]; } for (i = 0 ;i < MAX;i ++ ) { for (j = 0 ;j < n;j ++ ) h[j] = g[j]; { for (k = 0 ;k < n;k ++ ) { if (j == k) continue ; h[j] += b[j][k] * x[k]; } } t = 0 ; for (j = 0 ;j < n;j ++ ) if (t < fabs(h[j] - x[j])) t = fabs(h[j] - x[j]); for (j = 0 ;j < n;j ++ ) x[j] = h[j]; if (t < e) { printf( " x_i=/n " ); for (i = 0 ;i < n;i ++ ) printf( " x[%d]=%f/n " ,i,x[i]); getch(); return 0 ; } printf( " after %d repeat , return/n " ,MAX); getch(); return 1 ; } getch();}



8.秦九昭算法

C/C++ code
    
    
    
    
#include < math.h > float qin( float a[], int n, float x){ float r = 0 ; int i; for (i = n;i >= 0 ;i -- ) r = r * x + a[i]; return r;}main(){ float a[ 50 ],x,r = 0 ; int n,i; do { printf( " Input frequency: " ); scanf( " %d " , & n); } while (n < 1 ); printf( " Input value: " ); for (i = 0 ;i <= n;i ++ ) scanf( " %f " , & a[i]); printf( " Input frequency: " ); scanf( " %f " , & x); r = qin(a,n,x); printf( " Answer:%f " ,r); getch();}



9.幂法

C/C++ code
    
    
    
    
#include < stdio.h > #include < math.h > #define N 100 #define e 0.00001 #define n 3 float x[n] = { 0 , 0 , 1 }; float a[n][n] = {{ 2 , 3 , 2 },{ 10 , 3 , 4 },{ 3 , 6 , 1 }}; float y[n];main(){ int i,j,k; float xm,oxm; oxm = 0 ; for (k = 0 ;k < N;k ++ ) { for (j = 0 ;j < n;j ++ ) { y[j] = 0 ; for (i = 0 ;i < n;i ++ ) y[j] += a[j][i] * x[i]; } xm = 0 ; for (j = 0 ;j < n;j ++ ) if (fabs(y[j]) > xm) xm = fabs(y[j]); for (j = 0 ;j < n;j ++ ) y[j] /= xm; for (j = 0 ;j < n;j ++ ) x[j] = y[j]; if (fabs(xm - oxm) < e) { printf( " max:%f/n/n " ,xm); printf( " v[i]:/n " ); for (k = 0 ;k < n;k ++ ) printf( " %f/n " ,y[k]); break ; } oxm = xm; } getch();}



10.高斯塞德尔

C/C++ code
    
    
    
    
#include < math.h > #include < stdio.h > #define N 20 #define M 99 float a[N][N]; float b[N]; int main(){ int i,j,k,n; float sum,no,d,s,x[N]; printf( " /nInput dim of n: " ); scanf( " %d " , & n); if (n > N) { printf( " Faild! Check if 0<n<N!/n " ); getch(); return 1 ; } if (n <= 0 ) { printf( " Faild! Check if 0<n<N!/n " );getch(); return 1 ;} printf( " Input a[i,j],i,j=0…%d:/n " ,n - 1 ); for (i = 0 ;i < n;i ++ ) for (j = 0 ;j < n;j ++ ) scanf( " %f " , & a[i][j]); printf( " Input b[i],i=0…%d:/n " ,n - 1 ); for (i = 0 ;i < n;i ++ ) scanf( " %f " , & b[i]); for (i = 0 ;i < n;i ++ ) x[i] = 0 ; k = 0 ; printf( " /nk=%dx= " ,k); for (i = 0 ;i < n;i ++ ) printf( " %12.8f " ,x[i]); do { k ++ ; if (k > M){printf( " /nError!/n”);getch();} break ; } no = 0.0 ; for (i = 0 ;i < n;i ++ ) { s = x[i]; sum = 0.0 ; for (j = 0 ;j < n;j ++ ) if (j != i) sum = sum + a[i][j] * x[j]; x[i] = (b[i] - sum) / a[i][i]; d = fabs(x[i] - s); if (no < d) no = d; } printf( " /nk=%2dx= " ,k); for (i = 0 ;i < n;i ++ ) printf( " %f " ,x[i]);} while (no >= 0.1e-6 ); if (no < 0.1e-6 ){ printf( " /n/n answer=/n " ); printf( " /nk=%d " ,k); for (i = 0 ;i < n;i ++ ) printf( " /n x[%d]=%12.8f " ,i,x[i]);}getch();}

注:本文转自:http://topic.csdn.net/u/20080509/13/d4500a19-10aa-4fdf-b876-8e30f4daa331.html?seed=880334510

你可能感兴趣的:(c,算法,ini,input,语言,float)