Jacobi迭代法算法程序(C语言)

用C语言实现Jacobi迭代法求解方程组,
程序代码如下:
#include<stdio.h>  
#include<math.h>  
void main()
{
    float a[10][11],x1[10],x2[10],temp=0,fnum=0;
    int i,j,n,bk=0;
    printf("使用Jacobi迭代法求解方程组:\n");
    printf("输入方程组的元(n<10):\nn=");
    scanf("%d",&n);
    for(i=1;i<n+1;i++)
        x1[i]=0;
    printf("\n输入方程组的系数矩阵:\n");
    for(i=1;i<n+1;i++)
    {
        j=1;
        while(j<n+1)
        {
            printf("a%d%d=",i,j);
            scanf("%f",&a[i][j]);
            j++;
        }
    }
    printf("\n输入方程组的常数项:\n");
    for(i=1;i<n+1;i++)
    {
        printf("b%d=",i);
        scanf("%f",&a[i][n+1]);
    }
    printf("\n");
    while(bk!=1)
    {
        for(i=1;i<n+1;i++)
        {
            for(j=1;j<n+1;j++)
            {
                if (j!=i)
                temp=a[i][j]*x1[j]+temp;
            }
            x2[i]=(a[i][n+1]-temp)/a[i][i];
            temp=0;
        }
        for(i=1;i<n+1;i++)
        {
            fnum=float(fabs(x1[i]-x2[i]));
            if(fnum>temp) temp=fnum;
        }
        if(temp<=pow(10,-4)) bk=1;
        for(i=1;i<n+1;i++)
            x1[i]=x2[i];
    }
    printf("原方程组的解为:\n");
    for(i=1;i<n+1;i++)
    {
        printf("x%d=%7.4f     ",i,x1[i]);
        printf("\n");
    }
}



文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: C语言 数值分析
相关日志:
评论: 5 | 引用: 0 | 查看次数: -
回复回复Marti[2016-05-29 09:20 AM | del]
Thanks! This is an outstanding internet site!
回复回复Lloyd[2016-05-11 06:54 AM | del]
You're a really helpful internet site; could not make it without ya!
回复回复Randi[2016-04-22 05:03 PM | del]
I love reading through your websites. Appreciate it!
回复回复Raleigh[2016-04-15 02:31 PM | del]
You're a very practical web site; couldn't make it without ya!
回复回复Candelaria[2016-04-07 03:16 PM | del]
You've got one of the better internet websites.
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.