• 10-amaliy mashg’ulot: Tasodifiy sonlar massivlari bilan ishlash.
  • Gauss usulida Pythonda qo’llashga yana bitta dasturni ko’rib chiqaylik




    Download 15,08 Mb.
    bet97/135
    Sana22.05.2024
    Hajmi15,08 Mb.
    #250347
    1   ...   93   94   95   96   97   98   99   100   ...   135
    Bog'liq
    Fizik jarayonlarni kompyuterda modellashtirish

    Gauss usulida Pythonda qo’llashga yana bitta dasturni ko’rib chiqaylik:
    Importing NumPy Library
    import numpy as np
    import sys

    # Reading number of unknowns
    n = int(input('Enter number of unknowns: '))

    # Making numpy array of n x n+1 size and initializing
    # to zero for storing augmented matrix
    a = np.zeros((n,n+1))

    # Making numpy array of n size and initializing
    # to zero for storing solution vector
    x = np.zeros(n)

    # Reading augmented matrix coefficients
    print('Enter Augmented Matrix Coefficients:')
    for i in range(n):
    for j in range(n+1):
    a[i][j] = float(input( 'a['+str(i)+']['+ str(j)+']='))

    # Applying Gauss Elimination
    for i in range(n):
    if a[i][i] == 0.0:
    sys.exit('Divide by zero detected!')

    for j in range(i+1, n):
    ratio = a[j][i]/a[i][i]

    for k in range(n+1):
    a[j][k] = a[j][k] - ratio * a[i][k]

    # Back Substitution
    x[n-1] = a[n-1][n]/a[n-1][n-1]

    for i in range(n-2,-1,-1):
    x[i] = a[i][n]

    for j in range(i+1,n):
    x[i] = x[i] - a[i][j]*x[j]

    x[i] = x[i]/a[i][i]

    # Displaying solution
    print('\nRequired solution is: ')
    for i in range(n):
    print('X%d = %0.2f' %(i,x[i]), end = '\t')
    Natijani hisoblaymiz:
    Enter number of unknowns: 3
    Enter Augmented Matrix Coefficients:
    a[0][0]=1
    a[0][1]=1ееееее66
    a[0][2]=1
    a[0][3]=9
    a[1][0]=2
    a[1][1]=-3
    a[1][2]=4
    a[1][3]=13
    a[2][0]=3
    a[2][1]=4
    a[2][2]=5
    a[2][3]=40

    Required solution is:


    X0 = 1.00 X1 = 3.00 X2 = 5.00

    10-amaliy mashg’ulot: Tasodifiy sonlar massivlari bilan ishlash.

    Download 15,08 Mb.
    1   ...   93   94   95   96   97   98   99   100   ...   135




    Download 15,08 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Gauss usulida Pythonda qo’llashga yana bitta dasturni ko’rib chiqaylik

    Download 15,08 Mb.