SYMULACJE KOMPUTEROWE


 

PROGRAM Z WYKORZYSTANIEM FUNKCJI STATYSTYCZNYCH I INSTRUKCJI COMPUTE. TWORZENIE RAPORTOW.

 Materiał do przygotowania:

-        Urządzenia we/wy w języku SIMSCRIPT

-        Operacje na plikach

-        Generowanie raportów

Program ćwiczenia:

Zadanie 1:

Dany jest plik wejściowy zawierający n-elementowy wektor X. Napisać program w języku SIMSCRIPT, który dla zadanego wektora X obliczy następujące funkcje:

  1. y1=ln(|x|)
  2. y2=log(|x|)
  3. y3=sqrt(|x|)
  4. y4=sin(x)
  5. y5=x3

Ponadto, dla wszystkich wektorów y1,…,y5 oraz X obliczy następujące statystyki

  1. Sumę wszystkich elementów
  2. Wartość średnią
  3. Sumę kwadratów wszystkich elementów
  4. Wariancję
  5. Odchylenie standardowe
  6. Maksymalny i minimalny element

Uzyskane wyniki należy zapisać do pliku w postaci raportu.

Zadanie 2:

Zapisanie macierzy do pliku o podanej nazwie.

Zadanie 3:

Wygenerowanie macierzy z wartościami z rozkładem: Poissona, wykładniczym, Gamma o podanych parametrach, rozkładzie Gaussa. Zapis do pliku, odczyt z pliku.

Zadanie 4:

Obliczenie podstawowych charakterystyk macierzy zapisanych w Zadaniu 3.

TEORIA (Rozkład Gamma)

 

 

 


 

ZADANIE

1)     Przeanalizować kod procedur operacji na macierzach.

2)     Napisać procedury wykonujące operacje na tablicy macierzy (1-dimensional pointer array).

 

preamble

    normally, mode is real

    define matrix1, matrix2 as real 2-dimensional arrays

    define o as integer variable

    define d as 1-dimensional pointer array

end

 

main

    reserve d(*) as 10

   

print 4 line thus

                                   ________________________________

                                   PROGRAM DO DZIALAN NA MACIERZACH

                                   ================================

 

while o <> 10

  do

print 11 line like this

           

            1: wypelnij macierze A i B

            2: wygeneruj losowe macierze A i B

            3: wyswietl macierze A i B

            4: oblicz iloczyn macierzy A i B

            5: oblicz maximum macierzy A i B

            6: oblicz minimum macierzy A i B

            7: oblicz slad macierzy A i B

            8: oblicz sume macierzy A i B

            9: koniec        

            10: wprowadz do wektora

print 1 line thus

 

read o

 

select case o

 

    case 0

            d(1)=matrix1(*,*)

            matrix2(*,*)=d(1)

           

           

   

     case 1

            print 1 line like this

            Macierz A

   

            call enter_matrix yielding matrix1(*,*)

     

            print 1 line like this

            Macierz B

   

            call enter_matrix yielding matrix2(*,*)

 

     case 2

        print 1 line like this

            Macierz A

           

            call random_matrix yielding matrix1(*,*)

           

            print 1 line like this

            Macierz B

           

            call random_matrix yielding matrix2(*,*)

    

     case 3

            print 1 line like this

            Macierz A

     

            call print_matrix given matrix1(*,*)

   

            print 1 line like this

            Macierz B

   

            call print_matrix given matrix2(*,*)

 

            read as /

 

     case 4

            print 1 line like this

            Iloczyn macierzy A i B

   

            call multiply given matrix1(*,*) and matrix2(*,*)

            yielding matrix_result(*,*)

            call print_matrix given matrix_result(*,*)

 

            read as /

 

     case 5

            print 1 line like this

            max macierzy A wynosi

    

            call max given matrix1(*,*)

            skip 1 line

 

            print 1 line like this

            max macierzy B wynosi

    

            call max given matrix2(*,*)

            skip 1 line

 

            read as /

 

     case 6

            print 1 line like this

            min macierzy A wynosi

    

            call min given matrix1(*,*)

            skip 1 line

 

            print 1 line like this

            min macierzy B wynosi

     

            call min given matrix2(*,*)

            skip 1 line

 

            read as /

 

     case 7

            print 1 line like this

            slad macierzy A wynosi

   

            call slad given matrix1(*,*)

            skip 1 line

 

            print 1 line like this

            slad macierzy B wynosi

   

            call slad given matrix2(*,*)

            skip 1 line

 

            read as /

 

     case 8

            print 1 line like this

            suma macierzy wynosi A+B

 

            call suma given matrix1(*,*) and matrix2(*,*)

            yielding matrix_result(*,*)

            call print_matrix given matrix_result(*,*)

                       

            read as /

 

     case 9

            leave

 

    endselect

  loop   

end

 

 

routine enter_matrix yielding matrix

    define row, col, i, j as integer variables

    define matrix as real 2-dimensional array

 

    print 1 line like this

    Podaj wymiary macierzy [wiersz, kolumna]

   

    read row, col

   

    reserve matrix(*,*) as row by col

 

    for i = 1 to row do

        for j = 1 to col do

            print 1 line with i, j like this

            Wprowadz element matrix(**,**)

       

            read matrix(i,j)

        loop

    loop

 

    return

end

 

routine random_matrix yielding matrix

    define row, col, i, j as integer variables

    define matrix as real 2-dimensional array

   

    print 1 line like this

    Podaj wymiary macierzy

   

    read row, col

   

    reserve matrix(*,*) as row by col

   

    for i = 1 to row do

            for j = 1 to col do

                let matrix(i,j) = random.f(1)*100

            loop

    loop

 

    return

end

 

routine print_matrix given matrix

    define i, j as integer variables

    define matrix as real 2-dimensional array

 

    for i = 1 to dim.f(matrix(*,*)) do

        for j = 1 to dim.f(matrix(1,*)) do

            write matrix(i,j) as D(8,2)     

        loop

    write as /

    loop

 

    skip 1 line

    return 

end

 

routine multiply given matrix1 and matrix2 yielding matrix_result

  define i, j, k as integer variables

  define matrix1, matrix2, matrix_result as real 2-dimensional arrays

  reserve matrix_result(*,*) as dim.f(matrix1(*,*)) by dim.f(matrix2(1,*))

 

  for i = 1 to dim.f(matrix1(*,*)) do

    for j = 1 to dim.f(matrix2(1,*)) do

      for k = 1 to dim.f(matrix1(1,*)) do

        let matrix_result(i,j) = matrix_result(i,j) + matrix1(i,k) *

            matrix2(k,j)

      loop

    loop

  loop

  return

end

 

routine max given matrix

    define i, j as integer variables

    define x as variables

    define matrix as real 2-dimensional array

 

    let x = matrix(1,1)

 

    for i = 1 to dim.f(matrix(*,*)) do

        for j = 1 to dim.f(matrix(1,*)) do

            if x < matrix(i,j)

                                   let x = matrix(i,j)

                        endif         

        loop

    loop

 

    write x as D(8,2)

 

    skip 1 line

    return 

end

 

routine min given matrix

    define i, j as integer variables

    define x as variables

 

    define matrix as real 2-dimensional array

 

    let x = matrix(1,1)

 

    for i = 1 to dim.f(matrix(*,*)) do

        for j = 1 to dim.f(matrix(1,*)) do

            if x > matrix(i,j)

                                   let x = matrix(i,j)

                        endif         

        loop

    loop

 

    write x as D(8,2)

 

    skip 1 line

    return  

end

 

routine slad given matrix

    define i, j as integer variables

    define suma as variables

    define matrix as real 2-dimensional array

 

    for i = 1 to dim.f(matrix(*,*)) do

        for j = 1 to dim.f(matrix(1,*)) do

            if i eq j

                                   let suma = suma + matrix(i,j)

                        endif         

        loop

    loop

 

    write suma as D(8,2)

 

    skip 1 line

    return 

end

 

routine suma given matrix1 and matrix2 yielding matrix_result

  define i, j as integer variables

  define matrix1, matrix2, matrix_result as real 2-dimensional arrays

  reserve matrix_result(*,*) as dim.f(matrix1(*,*)) by dim.f(matrix1(1,*))

 

  for i = 1 to dim.f(matrix1(*,*)) do

    for j = 1 to dim.f(matrix1(1,*)) do

        let matrix_result(i,j) = matrix_result(i,j) + matrix1(i,j) +

            matrix2(i,j)

      loop

    loop

  return

end

 


 

ZADANIE - STATYSTYKI

1)Przeanalizować sposób obliczenia statystyk danych.

2)Zapis macierzy do pliku.

3)Odczyt macierzy z pliku.

4) Statystyki danych odczytanych z pliku z punktu 3)

 

preamble

    normally,mode is real

    define X,Y1,Y2,Y3,Y4,Y5 AS 1-dimensional arrays

    define X1,X2,X3,X4,X5,X6 as text variables

end

main

    define N,M,I,J as integer variables

    open unit 2 for input,file name is "DANE-S5"

    use 2 for input

    read N,M

    reserve X(*),Y1(*),Y2(*),Y3(*),Y4(*),Y5(*) as N

''    for I=1 to N, read X(I)

    for I=1 to N

    do

      read X(I)

    loop

    call FUNKCJE given X(*) yielding Y1(*),Y2(*),Y3(*),Y4(*),Y5(*)

    call STATYSTYKA given X(*) yielding XS,XM,XSQ,XV,XST,XMA,XMI

    call STATYSTYKA given Y1(*) yielding Y1S,Y1M,Y1SQ,Y1V,Y1ST,Y1MA,Y1MI

    call STATYSTYKA given Y2(*) yielding Y2S,Y2M,Y2SQ,Y2V,Y2ST,Y2MA,Y2MI    

    call STATYSTYKA given Y3(*) yielding Y3S,Y3M,Y3SQ,Y3V,Y3ST,Y3MA,Y3MI

    call STATYSTYKA given Y4(*) yielding Y4S,Y4M,Y4SQ,Y4V,Y4ST,Y4MA,Y4MI    

    call STATYSTYKA given Y5(*) yielding Y5S,Y5M,Y5SQ,Y5V,Y5ST,Y5MA,Y5MI

    open unit 3 for output,file name is "WYN-S5"

    use 3 for output

    let page.v=1

    let lines.v=M

    begin report

        begin heading

        skip 2 output lines

            if page is first

                print 1 line as follows

                     FUNKCJE MATEMATYCZNE I STATYSTYCZNE

                skip 3 output lines

            always

            print 1 line with page.v as follows

                                                         STRONA NR. **

            skip 2 output lines

            print 1 line as follows

    I       X(I)      Y1(I)      Y2(I)      Y3(I)      Y4(I)     Y5(I)

            skip 1 output line

        end

        for J=1 to N

            write J,X(J),Y1(J),Y2(J),Y3(J),Y4(J),Y5(J) as I 5 ,6 D(11,2),/

        let X1 = "X"

        let X2 = "Y1"

        let X3 = "Y2"

        let X4 = "Y3"

        let X5 = "Y4"

        let X6 = "Y5"

        call WYDRUK given X1,XS,XM,XSQ,XV,XST,XMA,XMI

        call WYDRUK given X2,Y1S,Y1M,Y1SQ,Y1V,Y1ST,Y1MA,Y1MI

        call WYDRUK given X3,Y2S,Y2M,Y2SQ,Y2V,Y2ST,Y2MA,Y2MI

        call WYDRUK given X4,Y3S,Y3M,Y3SQ,Y3V,Y3ST,Y3MA,Y3MI

        call WYDRUK given X5,Y4S,Y4M,Y4SQ,Y4V,Y4ST,Y4MA,Y4MI

        call WYDRUK given X6,Y5S,Y5M,Y5SQ,Y5V,Y5ST,Y5MA,Y5MI

    end

    close unit 2

    close unit 3

end

routine FUNKCJE given X yielding Y1,Y2,Y3,Y4,Y5

    define K,L as integer variables

    define X,Y1,Y2,Y3,Y4,Y5 as 1-dimensional arrays

    let K=dim.f(X(*))

    reserve Y1(*),Y2(*),Y3(*),Y4(*),Y5(*) as K

    for L=1 to K

        do

        Y1(L) = log.e.f(abs.f(X(L)))

        Y2(L) = log.10.f(abs.f(X(L)))

        Y3(L) = sqrt.f(abs.f(X(L)))

        Y4(L) = tan.f(X(L))

        Y5(L) = X(L)*X(L)

        loop

    return

end

routine STATYSTYKA given Y yielding S,M,SQ,V,ST,MA,MI

    define S,M,SQ,V,ST,MA,MI as real variables

    define K1,L as integer variables

    define Y as 1-dimensional array

 ''''’ KOD OBLICZENIA STATYSTK Z WYKORZYSTANIEM POLECENIA COMPUTE

    let K1 = dim.f(Y(*))

    for L=1 to K1,

        compute

            S  as the sum of Y(L)

            '' podobnie dla pozostałych statystyk

    return

end

routine WYDRUK given X,S,M,SQ,V,ST,MA,MI

    define X as text variable

    define S,M,SQ,V,ST,MA,MI as real variables

    use 3 for output

    skip 3 output lines

    print 1 line with X thus

         STATYSTYKA DLA WEKTORA: ***

    skip 1 output line         

    print 7 line with S,M,SQ,V,ST,MA,MI thus

    SUMA WARTOSCI ELEMENTOW = *******.***

    WARTOSC SREDNIA         = *******.***

    SUMA KWADRATOW          = *******.***

    WARIANCJA               = *******.***

    ODCHYLENIE STANDARDOWE  = *******.***

    ELEMENT MAKSYMALNY      = *******.***

    ELEMENT MINIMALNY       = *******.***

    return

end