home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
modula2
/
library
/
fst
/
modula3
/
funktion.mod
< prev
next >
Wrap
Text File
|
1993-07-28
|
7KB
|
232 lines
MODULE funktion;
(*******************************************************************
Berechnung der Maximal- und Minimalwerte folgender Functionen:
a) f(x,y) := sin(2.0*x)+sin(3.0*y)+cos(4.0*x)+cos(5.0*y)
b) f(x,y) := exp(x+y/2.0)*cos(x*y)
c) f(x,y) := x*y
d) f(x,y) := sin(x+y)*cos(x*y)
e) f(x,y) := exp(x)*cos(x*y)-sin(2.0*x)+cos(6.0*y).
Die Grenzen ([a,b] x [c,d]) müßen explizit eingegeben werden.
*******************************************************************)
FROM InOut IMPORT WriteLn,
WriteString,
ReadCard,
WriteCard,
Read,
Write;
FROM MathLib0 IMPORT sin,
cos,
exp;
FROM RealInOut IMPORT WriteReal,
ReadReal,
Done;
FROM Strings IMPORT Assign;
TYPE
Function = PROCEDURE(REAL, REAL) : REAL;
String70 = ARRAY[1..70] OF CHAR;
CONST
Anzfunk = 5;
VAR
funkname : ARRAY[1..Anzfunk] OF String70;
f : Function;
a, b, delta1, (* Intervall [a,b] und die zugehoerige Schrittweite *)
c, d, delta2 (* Intervall [c,d] und die zugehoerige Schrittweite *)
: REAL;
wahl : CARDINAL; (* Laufvariable *)
(*******************************************************************)
(* Procedure INIT *)
(*******************************************************************)
(* Setzt alle Variablen auf 0.0 und weist dem Feld funkname die *)
(* Bezeichnungen der einzelnen Funktionen zu. *)
(* Abschliessend wird eine Nachricht ausgegeben. *)
(*******************************************************************)
PROCEDURE init;
VAR
j : CARDINAL;
BEGIN
a:=0.0; b:=0.0; c:=0.0; d:=0.0; delta1:=0.0; delta2:=0.0;
Assign('f(x,y) := sin(2.0*x)+sin(3.0*y)+cos(4.0*x)+cos(5.0*y)',
funkname[1]);
Assign('f(x,y) := exp(x+y/2.0)*cos(x*y)', funkname[2]);
Assign('f(x,y) := x*y', funkname[3]);
Assign('f(x,y) := sin(x+y)*cos(x*y)', funkname[4]);
Assign('f(x,y) := exp(x)*cos(x*y)-sin(2.0*x)+cos(6.0*y)',
funkname[5]);
WriteLn; WriteLn;
WriteString('Dieses Programm berechnet die Minimal- bzw.');
WriteString(' Maximalwerte der folgenden'); WriteLn;
WriteString('Funktionen im Intervall [a,b] x [c,d]'); WriteLn;
WriteString('Zur Auswahl stehen: '); WriteLn;
WriteLn;
FOR j:=1 TO Anzfunk DO
WriteCard(j, 0); WriteString(': ');
WriteString(funkname[j]); WriteLn;
END;
END init;
(*******************************************************************)
(* Procedure f1, f2, f3, f4 und f5 *)
(*******************************************************************)
(* Diese Prozeduren enthalten die zur Asuwahl stehenden Funktionen.*)
(*******************************************************************)
PROCEDURE f1(x, y : REAL) : REAL;
BEGIN
RETURN sin(2.0*x)+sin(3.0*y)+cos(4.0*x)+cos(5.0*y);
END f1;
PROCEDURE f2(x, y : REAL) : REAL;
BEGIN
RETURN exp(x+y/2.0)*cos(x*y);
END f2;
PROCEDURE f3(x, y : REAL) : REAL;
BEGIN
RETURN x*y;
END f3;
PROCEDURE f4(x, y : REAL) : REAL;
BEGIN
RETURN sin(x+y)*cos(x*y);
END f4;
PROCEDURE f5(x, y : REAL) : REAL;
BEGIN
RETURN exp(x)*cos(x*y)-sin(2.0*x)+cos(6.0*y);
END f5;
(*******************************************************************)
(* Procedure MINMAX *)
(*******************************************************************)
(* Berechnet den Minimal- bzw. Maximalwert der Funktion f im *)
(* Intervall [a,b] x [c,d] und gibt das Ergebnis aus. *)
(*******************************************************************)
PROCEDURE minmax(f : Function;
a, b, delta1, c, d, delta2 : REAL);
VAR
min, max, (* Minimum bzw. Maximum *)
x, y, z : REAL; (* Laufvariablen *)
BEGIN
min:=f(a, c); (* Startwert min *)
max:=min; (* Startwert max *)
x:=a;
y:=c;
WHILE (x<=b) DO (* berechnen *)
WHILE (y<=d) DO
z:=f(x, y);
IF (z<min) THEN min:=z; END;
IF (z>max) THEN max:=z; END;
y:=y+delta2;
END;
x:=x+delta1;
END;
WriteLn; (* und ausgeben *)
WriteString('Das Minimum betraegt: ');
WriteReal(min, 10); WriteLn;
WriteString('Das Maximum betraegt: ');
WriteReal(max, 10); WriteLn;
END minmax;
(*******************************************************************)
(* Procedure LESEREAL *)
(*******************************************************************)
(* Gibt die Nachricht m aus und liest eine REAL-Zahl ein. *)
(*******************************************************************)
PROCEDURE lesereal(m : ARRAY OF CHAR) : REAL;
VAR
z : REAL;
BEGIN
REPEAT
WriteString(m);
ReadReal(z); WriteLn;
UNTIL Done;
RETURN z;
END lesereal;
(*******************************************************************)
(* Procedure LESEPOSREAL *)
(*******************************************************************)
(* gibt die Nachricht m aus und liest eine positive REAL-Zahl ein. *)
(*******************************************************************)
PROCEDURE leseposreal(m : ARRAY OF CHAR) : REAL;
VAR
z : REAL;
BEGIN
REPEAT
WriteString(m);
ReadReal(z); WriteLn;
UNTIL (z>0.0);
RETURN z;
END leseposreal;
(*******************************************************************)
(* Hauptprogramm *)
(*******************************************************************)
BEGIN
init;
(* Funktion auswaehlen *)
WriteLn;
REPEAT
WriteString('Bitte waehlen Sie ein Funktion aus: ');
ReadCard(wahl); WriteLn;
UNTIL (wahl>=1) AND (wahl<=Anzfunk);
WriteLn;
WriteCard(wahl, 0); WriteString(': ');
WriteString(funkname[wahl]); WriteLn;
CASE wahl OF
1 : f:=f1; |
2 : f:=f2; |
3 : f:=f3; |
4 : f:=f4; |
5 : f:=f5; |
END;
WriteLn;
(* Intervall [a,b] einlesen *)
WriteString('Bitte Intervall [a;b] eingeben (a<b):');
WriteLn;
REPEAT
a:=lesereal('Bitte a eingeben (Realzahl): ');
b:=lesereal('Bitte b eingeben (Realzahl): ');
UNTIL (a<b);
delta1:=leseposreal('Bitte Schrittweite (Realzahl und >0) eingeben: ');
WriteLn;
(* Intervall [c,d] einlesen *)
WriteString('Bitte Intervall [c;d] eingeben (c<d):');
WriteLn;
REPEAT
c:=lesereal('Bitte c eingeben (Realzahl): ');
d:=lesereal('Bitte d eingeben (Realzahl): ');
UNTIL (c<d);
delta2:=leseposreal('Bitte Schrittweite (Realzahl und >0) eingeben: ');
minmax(f, a, b, delta1, c, d, delta2); (* Berechnung *)
WriteLn;
WriteString('Ende der Berechnung.');
WriteLn; WriteLn;
END funktion.