首页 > 其他分享 >实例92 高斯消去法

实例92 高斯消去法

时间:2022-09-23 10:12:23浏览次数:52  
标签:include 高斯 int double printf 92 ep 消去法

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
 
int GS(int,double**,double *,double);
double **TwoArrayAlloc(int,int);
void TwoArrayFree(double **);
 
void main()
{
    int i,n;
    double ep,**a,*b;
    n = 3;
    ep = 1e-4;
    a = TwoArrayAlloc(n,n);
    b = (double *)calloc(n,sizeof(double));
    if(b == NULL)
    {
        printf("内存分配失败\n");
        exit(1);
    }
    a[0][0]= 2; a[0][1]= 6; a[0][2]=-1;
    a[1][0]= 5; a[1][1]=-1; a[1][2]= 2;
    a[2][0]=-3; a[2][1]=-4; a[2][2]= 1;
    b[0] = -12; b[1] = 29;  b[2] = 5;
    if(!GS(n,a,b,ep))
    {
        printf("不可以用高斯消去法求解\n");
        exit(0);
    }
    printf("该方程组的解为:\n");
    for(i=0;i<3;i++)
        printf("x%d = %.2f\n",i,b[i]);
    TwoArrayFree(a);
    free(b);
}
 
int GS(n,a,b,ep)
int n;
double **a;
double *b;
double ep;
{
    int i,j,k,l;
    double t;
    for(k=1;k<=n;k++)
    {
        for(l=k;l<=n;l++)
            if(fabs(a[l-1][k-1])>ep)
                break;
            else if(l==n)
                return(0);
        if(l!=k)
        {
            for(j=k;j<=n;j++)
            {
                t = a[k-1][j-1];
                a[k-1][j-1]=a[l-1][j-1];
                a[l-1][j-1]=t;
            }
            t=b[k-1];
            b[k-1]=b[l-1];
            b[l-1]=t;
        }
        t=1/a[k-1][k-1];
        for(j=k+1;j<=n;j++)
            a[k-1][j-1]=t*a[k-1][j-1];
        b[k-1]*=t;
        for(i=k+1;i<=n;i++)
        {
            for(j=k+1;j<=n;j++)
                a[i-1][j-1]-=a[i-1][k-1]*a[k-1][j-1];
            b[i-1]-=a[i-1][k-1]*b[k-1];
        }
    }
    for(i=n-1;i>=1;i--)
        for(j=i+1;j<=n;j++)
            b[i-1]-=a[i-1][j-1]*b[j-1];
return(1);
}
 
double **TwoArrayAlloc(int r,int c)
{
    double *x,**y;
    int n;
    x=(double *)calloc(r*c,sizeof(double));
    y=(double **)calloc(r,sizeof(double*));
    for(n=0;n<=r-1;++n)
        y[n]=&x[c*n];
    return (y);
}
 
void TwoArrayFree(double **x)
{
    free(x[0]);
    free(x);
}
trust100@ubuntu:~/test/clanguage$ ./a.out 
该方程组的解为:
x0 = 3.00
x1 = -2.00
x2 = 6.00

 

标签:include,高斯,int,double,printf,92,ep,消去法
From: https://www.cnblogs.com/mapstar/p/16721725.html

相关文章

  • Jenkins 20220922笔记本2
                                  ......
  • lc_top_0922
    lc3无重复字符的最长子串滑动窗口维护不重复的集合,指针向右平移classSolution{publicintlengthOfLongestSubstring(Strings){Set<Character>set......
  • 20220922测试总结
    多做,视野才开阔,不要老是想着水题!P7800[COCI2015-2016#6]PAROVI原题链接题目分析一来可以直接暴力求解,硬性枚举是否选择这些线段,显然必须优化。我们先预处理每个二元......
  • 20220922缉
    20220922(种苹)t1[COCI2015-2016#6]PAROVI最初思路若选择二元组中不包含1,那Slavko只需选择2作为x即可对所有二元组满足a,b≥x;同样,若不包含n,则Slavko只需选择n作为x即可满......
  • acwing892. 台阶-Nim游戏
    acwing892.台阶-Nim游戏原题链接:https://www.acwing.com/problem/content/894/思路奇数台阶异或和不等于0先手必胜奇数台阶异或和等于0先手必败代码#include<iost......
  • Jenkins 20220921笔记本1
                            ......
  • luogu6927
    [ICPC2016WF]SwapSpace题面翻译你有\(n\)个硬盘\((n\leqslant10^{6})\),现在需要对所有硬盘进行格式化。格式化后,第\(i\)个硬盘的容量会由原来的\(a_{i}\)变为\(......
  • lc_模拟_回文串_0921
    lc5最长回文子串1动态规划classSolution{publicStringlongestPalindrome(Strings){intlen=s.length();if(len<2){r......
  • 0921v-for
    <html> <head> <metacharset="utf-8"/> <title></title> <scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script> </head><body> <divi......
  • 0921v-show
    <html> <head> <metacharset="utf-8"/> <title></title> <scriptsrc="js/vue.js"type="text/javascript"charset="utf-8"></script> </head><body> <divi......