Numeričke metode i praktikum

Similar documents
Uputa: Zabranjeno je koristiti bilo kakva pomagala. Rje²enja pi²ete desno od zadatka. Predajete samo ovaj list.

Programiranje Programski jezik C. Sadržaj. Datoteke. prof.dr.sc. Ivo Ipšić 2009/2010

Mašinska vizija. Dr Nenad Jovičić tnt.etf.rs/~mv

Numeričke metode i praktikum

Nizovi. Programiranje 1

Programiranje III razred

/*#include <iostream> // Prvi zadatak sa integralnomg ispita

x y = z Zadaci - procedure

Univerzitet u Nišu Građevinsko-arhitektonski fakultet. 4. Ciklična algoritamska struktura 5. Jednodimenzionalno polje.

pojedinačnom elementu niza se pristupa imeniza[indeks] indeks od 0 do n-1

Uvod u programiranje - vežbe. Kontrola toka izvršavanja programa

Fortran 90. Numeričke Metode DECEMBAR ĐURĐEVAC NATAŠA

namespace spojneice { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

CS 5513 Entry Quiz. Systems Programming (CS2213/CS3423))

VB komande. Programiranje 1

Osnove programskog jezika C# Čas 5. Delegati, događaji i interfejsi

b) program deljiv3; uses wincrt; var i:integer; begin i:=3; while i<100 do begin write(i:5); i:=i+3; end; end.

Lesson 7. Reading and Writing a.k.a. Input and Output

I PISMENI ZADATAK III6 I GRUPA IME I PREZIME

M.CS201 Programming language

20 Dynamic allocation of memory: malloc and calloc

File I/O. Arash Rafiey. November 7, 2017

Veliki računski zadaci mogu se razbiti u manje delove i time se omogućava ljudima da iskoriste ono što su neki drugi već uradili, umesto da počinju

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Chapter 13. Functions and Parameter Passing (Part 2)

For. 1) program ispis; {ispisuje brojeve od 1 do 5 jedan ispod drugog} uses wincrt; var s,i:integer; begin for i:=1 to 5do writeln(i); end.

Programming Language B

Zadaci za Tutorijal 2.

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Vežbe - XII nedelja PHP Doc

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Iosif Ignat Laboratory Guide 6. Modular programming MODULAR PROGRAMMING

PROGRAMIRANJE. Objekt-orjentirano programiranje (OOP)

9. RAD S DATOTEKAMA PODATAKA

VHDLPrimeri Poglavlje5.doc

Laboratory: USING FILES I. THEORETICAL ASPECTS

BİL200 TUTORIAL-EXERCISES Objective:

RAČUNARSKI PRAKTIKUM II

C Programming Language

Language comparison. C has pointers. Java has references. C++ has pointers and references

C Programming Language

Iosif Ignat, Marius Joldoș Laboratory Guide 5. Functions. FUNCTIONS in C

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Variation of Pointers

Učitati cio broj n i štampati njegovu recipročnu vrijednost. Ako je učitan broj 0, štampati 1/0.

Izrada VI laboratorijske vježbe

Zadatci Small Basic za 7. Razred ( programa)

Computers Programming Course 10. Iulian Năstac

CIS 2107 Computer Systems and Low-Level Programming Fall 2010 Midterm

Example: Pointer Basics

Personal SE. Functions, Arrays, Strings & Command Line Arguments

A Crash Course in C. Steven Reeves

EM108 Software Development for Engineers

Uporaba numeričkih metoda i praktikum -fortran. Mirko Planinić PMF

Dynamic Memory Allocation

ESC101N: Fundamentals of Computing End-sem st semester


Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Intermediate Programming

PESIT Bangalore South Campus Hosur road, 1km before ElectronicCity, Bengaluru -100 Department of Basic Science and Humanities

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Sadržaj predavanja 02. Cjelobrojni tip podataka(1/3) Cjelobrojni tip podataka(2/3) Cjelobrojni tip podataka(3/3) prec(short) prec(int) prec(long) int

Solution for Data Structure

Functions in C C Programming and Software Tools

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Introduction to Programming (Java) 4/12

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

Type Conversion. and. Statements

Basic and Practice in Programming Lab 10

Lecture 07 Debugging Programs with GDB

Kratke upute za korištenje MATLAB-a

Signals, Instruments, and Systems W2. C Programming (continued)

C Sample Code. Nyoman Bogi Aditya Karna Sisfo IMTelkom

CE Lecture 11

Lecture 3: C Programm

CSS CSS. selector { property: value; } 3/20/2018. CSS: Cascading Style Sheets

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

//Formimi i matrices #include <iostream> #include <math> using namespace std;

C, C++, Fortran: Basics

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Engineering program development 6. Edited by Péter Vass

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

C introduction: part 1

EM108 Software Development for Engineers

Ugradjena promenljiva ans cuva poslednju vrednost koja nije dodeljena ni jednoj promenljivoj >> ans+3

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

PROGRAMMAZIONE I A.A. 2017/2018

UNIVERZITET U BEOGRADU ELEKTROTEHNIČKI FAKULTET

Memory Allocation. General Questions

Primjer ispisuje rečenicu "Dobro dosli na brzi tecaj C-a" na ekranu

for i:=2 to n do if glasovi[i]>max then begin max:=glasovi[i]; k:=i {*promenljiva k ce cuvati indeks takmicara sa najvise glasova *} end;

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

... ; ako je a n parno. ; ako je a n neparno

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Transcription:

Numeričke metode i praktikum Aleksandar Maksimović IRB / 23/03/2006 / Str. 1

vektori Vektor u 3D prostoru. C: int v1[3]; v1[0]=a;v1[1]=b;v1[2]=c; Fortran: INTEGER V1(3) V1(1)=a V1(2)=b V1(3)=c Skalarni produkt dva vektora npr. v1. v2 C: v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]; Fortran: v1(1)*v2(1)+v1(2)*v2(2)+v1(3)*v2(3); / 23/03/2006 / Str. 2

library subroutine readmatrix(a,n,m,unit) integer n,m,unit real a(n,m) Do i=1,n read (unit,*) (a(i,j),j=1,m) end do end subroutine displaymatrix(a,n,m) integer n,m,unit real a(n,m) Do i=1,n write (6,*) (a(i,j),j=1,m) end do 100 Format (5(2x,f10.5)) end lib1.c lib1.f #include <stdio.h> #include <stdlib.h> #include "lib1.h" void readmatrix(float a[][50], int n, int m,file* fp) { int k, l; for (k=0;k<n;k++) for (l=0;l<m;l++) fscanf(fp,"%f ",&a[k][l]); } void displaymatrix(float a[][50], int n, int m) { int k, l; for (k=0;k<n;k++) { for (l=0;l<m;l++) printf("%f ",a[k][l]); printf("\n"); } } / 23/03/2006 / Str. 3

program void readmatrix(float a[][50], int, int,file* ); void displaymatrix(float a[][50], int, int ); lib1.h #include <stdio.h> #include <stdlib.h> #include "lib1.h" mainlib1.c program ReadDisplay mainlib1.f int main() { int j,k,l,m,n; float b[50]; FILE *fp; Implicit real (a h,o z) integer n,m,unt,l Dimension b(50) Deklaracije, polja odnosno vektora b cijelih brojeva, file pointera / 23/03/2006 / Str. 4

program if ((fp = fopen("matrixvector.dat","r")) == NULL) error("data file matrx1.dat not found\n"); Otvori file open(unit=8,file="matrixvector.dat",status="unknown") while (!feof(fp)) { fscanf(fp,"%d %d ",&n,&m); readmatrix(a,n,m,fp); n, m, Matrica fscanf(fp,"%d ",&l); for (k=0;k<l;k++) fscanf(fp,"%f ",&b[k]); } n, vektor read (8,*) n,m unt=8 call readmatrix(a,n,m,unt) c vector read (8,*) l read (8,*) (b(j),j=1,l) matrixvector.dat 2 3 1.0 2.0 3.0 1.0 1.0 1.0 4 1.0 2.0 3.0 4.0 C: fp je FILE POINTER F77: 8 unit n, m cijeli brojevi a dvodimenzionalno polje matrica b jednodimenzionalno polje vektor / 23/03/2006 / Str. 5

program Napravili smo program readanddisplay modularnim, tj. funkcije su u posebnom file u napisane kao podprogrami. C EXE: gcc o mainlibc mainlib1.c lib1.c./mainlibc F77 EXE: f77 o mainlibf mainlib2.f lib1.f./mainlibf Izvršavanje programa ispisuje vektor i matricu definiranu u ulaznom file u. / 23/03/2006 / Str. 6

nove funkcije za vektor Dodajemo 3 nove funkcije. Kompletan header file za C "lib1.h" void readmatrix(float a[][50], int, int,file* ); void displaymatrix(float a[][50], int, int ); void readvektor(float a[], int, FILE* ); void displayvektor(float a[],int); void readline(file*); readmatrix : (1) 2d polje A dimenzija (2,3) NxM, (4) file pointer ili unit (integer) displaymatrix : (1) 2d polje A dimenzija (2,3) NxM readvektor : (1) 1d polje A dimenzije (2) N, (3) file pointer ili unit (integer) displayvektor : (1) 1d polje A dimenzije (2) N readline: (1) file pointer ili unit (integer) read(matrix/vektor): pročitaju matricu vektor iz otvorenog file a readline: pročita liniju (u C 80 max, duljina) iz file a / 23/03/2006 / Str. 7

tok programa Program deklaracija varijabli (polja) za svaku veličinu iz file a 1 varijabla, za rezultat nova varijabla Otvori file preskoči komentar/ pročitaj dimenziju u deklariranu varijablu / pročitaj vektor ili matricu izvrši operacije prikaži rezultat / 23/03/2006 / Str. 8

modularni proram mainlib2.c mainlib2.f if ((fp = fopen("vm.dat","r")) == NULL) error("data file matrx1.dat not found\n"); readline(fp); fscanf(fp,"%d ",&L); readvektor(b,l,fp); displayvektor(b,l); readline(fp); fscanf(fp,"%d %d ",&n,&m); readmatrix(a,n,m,fp); open (unit=8,file="vm.dat",status="unknown") fp=8 c komentar call readline(fp) c vector read (8,*) L call readvektor(b,l,fp) call displayvektor(b,l) call readline(fp) c matrica read (8,*) n,m call readmatrix(a,n,m,fp) displaymatrix(a,n,m); fclose(fp); call displaymatrix(a,n,m) close(8) / 23/03/2006 / Str. 9

Zadatak Pročitati V2M2.dat Format: komentar, 1. vektor, komentar, 2. vektor, komentar, 1. matrica, komentar, 2. matrica Izračunati Skalarni produkt 2 vektora produkt 1. vektora i 1. matrice produkt 2 matrice / 23/03/2006 / Str. 10

deklaracije 1. Trebamo 2 1d polja za vektore V1, V2 i dva skalara za dimenzije L1, L2 2. Trebamo 2 2d polja za matrice MA1, MA2 i 4 cijela broja za dimenzije n1,m1,n2,m2. Matrice ne mogu biti u f77 M1 i M2, zbog dimenzija. 3. Skalarni produkt je skalar, SPROD 4. Matrica puta Vektor (2, 3) x (3) = (2) vektor dimenzije 2, MatVec 5. Matrica puta Matrica je opet Matrica MM dimenzije (2,3)x(3,2)=(2,2) 3. SPROD = V1(i)*V2(i), i=1,2,3,..,l1, L1 mora biti jednak L2 4. MatVec(i)=M1(i,j)*V1(j), j=1,2,...l1, L1 mora biti jednak m1 i=1,2,..n1 5. MM(i,j)= M1(i,k)*M2(k,j), k =1,2,.. m1, m1=n2. dimenzija n1xm2 i=1,..n1. j=1,..m2 / 23/03/2006 / Str. 11

deklaracije Dodatne varijable: File pointer fp Cijeli brojevi i,j,k,l za petlje float tmp dodatna varijabla, ako zatreba itd... / 23/03/2006 / Str. 12

mainlib3.c #include <stdio.h> #include <stdlib.h> #include "lib1.h" int main() { int L1, L2, n1,m1, n2,m2; float V1[10],V2[10], SPROD, MatVec[10]; float MA1[10][50],MA2[10][50],MM[10][50]; FILE *fp; int i,j,k; float tmp; if ((fp = fopen("v2m2.dat","r")) == NULL) error("data file matrx1.dat not found\n"); /* read all data */ readline(fp); fscanf(fp,"%d ",&L1); readvektor(v1,l1,fp); displayvektor(v1,l1); readline(fp); učitavanje 2. vektora readline(fp); fscanf(fp,"%d %d ",&n1,&m1); readmatrix(ma1,n1,m1,fp); displaymatrix(ma1,n1,m1); readline(fp); fscanf(fp,"%d %d ",&n2,&m2); readmatrix(ma2,n2,m2,fp); /* Matrix Vector */ tmp=0; for (i=0; i< n1;i++) { MatVec[i]=0; for (j=0; j< m1;j++) MatVec[i]=MatVec[i]+MA1[i][j]*V1[j]; } printf("matrica x Vektor \n"); displayvektor(matvec,n1); displaymatrix(ma2,n2,m2); /* Matrix Matrix */ /* close file */ for (i=0; i< n1;i++) { fclose(fp); for (j=0; j< m2;j++) /* COMPUTE */ { tmp=0; MM[i][j]=0; for( i=0;i<l1;i++) tmp=tmp+v1[i]; for (k=0; k< m1;k++) SPROD=tmp; MM[i][j]=MM[i][j]+MA1[i][k]*MA2[k][j]; printf("skalarni produkt je %f\n",sprod); } } printf("matrica x Matrica \n"); displaymatrix(mm,n1,m2); return 0; } / 23/03/2006 / Str. 13

mainlib3.f program vektormatmat integer L1, L2, n1,m1, n2,m2 real V1(10),V2(10), SPROD, MatVec(10) real MA1(10,50),MA2(10,50),MM(10,50) integer i,j,k,fp real tmp open (unit=8,file="v2m2.dat",status="unknown") fp=8 c komentar call readline(fp) c vector read (8,*) L1 call readvektor(v1,l1,fp) call displayvektor(v1,l1)... call readline(fp) read (fp,*) n2,m2 call readmatrix(ma2,n2,m2,fp) call displaymatrix(ma2,n2,m2) close(fp) c Calculate SPROD=0 do i=1,l1 SPROD=SPROD+V1(i) end do print *, "Skalarni produkt" print *, SPROD call multmatrixvector(ma1,v1,matvec,n1,m1) print *, "Matrica x Vektor" call displayvektor(matvec,n1) c Pomnozi matrice print *, "Matrica x Matrica" call multmatrixmatrix(ma1,ma2,mm,n1,m1,n2,m2) call displaymatrix(mm,n1,m2); end program / 23/03/2006 / Str. 14

mainlib3.f subroutine multmatrixmatrix(ma1,ma2,mm,n1,m1,n2,m2) INTEGER n1,m1,m2,n2,i,j,k REAL MA1(n1,m1),MA2(n2,m2),MM(n1,m2) Do i=1,n1 Do j=1,m2 MM(i,j) = 0.0 Do k=1,m1 MM(i,j) = MM(i,j) + MA1(i,k)*MA2(k,j) End Do End Do End Do end subroutine multmatrixvector(a,b,c,n,m) integer n,m real b(m),c(m) real a(n,m) do i=1,n c(i)=0.0 do j=1,m c(i)=c(i)+a(i,j)*b(j) end do end do end / 23/03/2006 / Str. 15

EXE programi Fortran: f77 o mainlib3f mainlib3.f lib1.f C: gcc o mainlib3c mainlib3.c lib1.c./mainlib3f 1. 2. 3. 3. 2. 1. 1. 2. 3. 1. 1. 1. 1. 2. 1. 1. 3. 1. Skalarni produkt 6. Matrica x Vektor 14. 6. Matrica x Matrica 12. 7. 5. 4../mainlib3C 1.000000 2.000000 3.000000 3.000000 2.000000 1.000000 1.000000 2.000000 3.000000 1.000000 1.000000 1.000000 1.000000 2.000000 1.000000 1.000000 3.000000 1.000000 Skalarni produkt je 6.000000 Matrica x Vektor 14.000000 6.000000 Matrica x Matrica 12.000000 7.000000 5.000000 4.000000 / 23/03/2006 / Str. 16

Zadaci VMV.dat ima slijedeći format: vektor duljine 3 matricu 3x2 vektor duljine 3 Napiši program koji ispiše skalarni produkt ova dva vektora Napiši program koji množi transponiranu matricu i vektor Pošaljite mail na Aleksandar.Maksimovic@irb.hr sa programima u attachmentu ili iskopirajte u /home/maks/vjezbe/vasdir i obavijestite me mailom / 23/03/2006 / Str. 17