Skip to content
Snippets Groups Projects
Commit 0b9ec04b authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Musterlösung 12.11.2018 vervollständigt

parent 0d32b910
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include "dates.h"
int is_leap_year (int year)
{
if (year % 4 == 0)
if (year % 100 == 0)
if (year % 400 == 0)
return 1;
else
return 0;
else
return 1;
else
return 0;
}
int days_in_month (int month, int year)
{
if (month == 2)
if (is_leap_year (year))
return 29;
else
return 28;
else if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
else
return 31;
}
void date_print (date *d)
{
printf ("%02d.%02d.%04d", d->day, d->month, d->year);
}
int date_set (date *d, char day, char month, int year)
{
d->year = year;
if (month > 0 && month <= 12)
d->month = month;
else
return 0;
if (day > 0 && day <= days_in_month (month, year))
d->day = day;
else
return 0;
return 1;
}
void date_next (date *d)
{
d->day++;
if (d->day > days_in_month (d->month, d->year))
{
d->month++;
d->day = 1;
if (d->month > 12)
{
d->year++;
d->month = 1;
}
}
}
typedef struct
{
char day, month;
int year;
}
date;
extern void date_print (date *d);
extern int date_set (date *d, char day, char month, int year);
extern void date_next (date *d);
No preview for this file type
......@@ -25,6 +25,7 @@
\documentclass[a4paper]{article}
\usepackage{pgscript}
\usepackage{gnuplot-lua-tikz}
\begin{document}
......@@ -109,6 +110,8 @@
innerhalb von String-Konstanten:
\lstinline{printf ("Your name is: \"%s\"", name);}
\clearpage
\exercise{Datum-Bibliothek}
Zerlegen Sie die Datum-Bibliothek aus der Übungsaufgabe 3 vom 29.\,10.\,2018
......@@ -116,6 +119,11 @@
Hinweis: Schreiben Sie auch hierfür zusätzlich ein Test-Programm.
\solution
Die Dateien \gitfile{hp}{20181112}{dates.c} und \gitfile{hp}{20181112}{dates.h} enthalten die Bibliothek,
die Datei \gitfile{hp}{20181112}{test-dates.c} ein Programm zum Testen der Bibliothek.
\exercise{Kondensator}
Ein Kondensator der Kapazität $C = 100\,\mu{\rm F}$
......@@ -153,6 +161,59 @@
\solution
Siehe die Datei: \gitfile{hp}{20181112}{loesung-3.c}
In dem Programm \gitfile{hp}{20181112}{loesung-3.c}
arbeiten wir, dem ersten Hinweis folgend,
mit einem Zeitintervall von \lstinline{dt = 0.01}.
Mit dieser Schrittweite lassen wir uns eine Tabelle ausgeben,
die jeweils die Zeit, die durch die Simulation berechnete Spannung
und die Spannung $U_0 \cdot e^{-\frac{t}{RC}}$
gemäß der theoretischen Entladekurve ausgibt.
Wir simulieren, wie die Ladung $Q = C \cdot U$ des Kondensators
im Laufe der Zeit abfließt.
Dazu berechnen wir in jedem Zeitschritt zunächst den Strom $I = U / R$,
der aus dem Kondensator fließt.
Dieser Strom bewirkt, daß innerhalb des Zeitintervalls $dt$
die Ladung $dQ = I \cdot dt$ aus dem Kondensator abfließt.
Am Ende des Zeitintervalls berechnen wir die zur neuen Ladung $Q$
gehörende neue Spannung $U = Q / C$.
Für eine einfache Ausgabe der Tabelle
verwenden wir dreimal die Formatspezifikation \lstinline{%10.3lf}.
Damit schreiben wir jeweils eine \emph{lange Fließkommazahl\/} (\lstinline{%lf})
rechtsbündig in ein Feld der Breite 10 und lassen uns 3 Nachkommastellen ausgeben.
Wir compilieren das Programm mit:
\lstinline[style=cmd]{gcc -Wall -O loesung-3.c -lm -o loesung-3}\\
(Man beachte das \lstinline[style=cmd]{-lm}\hspace{1pt}
für das Einbinden der Mathematik-Bibliothek.)
Der Tabelle entnehmen wir dann, daß die Spannung bei etwa $t = 12.91\,{\rm s}$
den Wert $0.1\,{\rm V}$ unterschreitet.
Ebenfalls der Tabelle können wir entnehmen,
daß die durch die Simulation berechnete Spannung
mit der Spannung $U_0 \cdot e^{-\frac{t}{RC}}$
gemäß der theoretischen Entladekurve
bis auf wenige Prozent übereinstimmt.
Dies ist für viele praktische Anwendungen ausreichend,
wenn auch nicht für Präzisionsmessungen.
\goodbreak
Wenn Sie die Ausgabe des Programms, z.\,B.\ mit
\lstinline{./loesung-3 > loesung-3.dat},
in einer Datei \gitfile{hp}{20181112}{loesung-3.dat} speichern,
können Sie sich die beiden Kurven graphisch darstellen lassen,
z.\,B.\ mit \file{gnuplot} und dem folgenden Befehl:\,
\lstinline[style=cmd]{plot "loesung-3.dat" using 1:2 with lines title "Simulation",}
\lstinline[style=cmd]{"loesung-3.dat" using 1:3 with lines title "Theorie"}
\begin{center}
\input{loesung-3.tikz}
\end{center}
Der Unterschied zwischen der simulierten und der theoretischen Entladungskurve
ist mit bloßem Auge nicht sichtbar.
\end{document}
......@@ -7,10 +7,10 @@ int main (void)
double U0 = 5.0;
double U = U0;
double R = 33000.0;
double t = 0.1;
double t = 0.0;
double dt = 0.01;
double Q = C * U;
while (U > 0.1)
while (U > 0.09)
{
printf ("%10.3lf%10.3lf%10.3lf\n", t, U, U0 * exp (-t / (R * C)));
double I = U / R;
......
This diff is collapsed.
This diff is collapsed.
#include <stdio.h>
#include "dates.h"
void check (char day, char month, int year)
{
date d;
if (date_set (&d, day, month, year))
{
date_print (&d);
printf (" --> ");
date_next (&d);
date_print (&d);
printf ("\n");
}
else
printf ("%02d.%02d.%04d: invalid date\n", day, month, year);
}
int main (void)
{
check (6, 11, 2016);
check (29, 11, 2016);
check (30, 11, 2016);
check (31, 11, 2016);
check (29, 12, 2016);
check (30, 12, 2016);
check (31, 12, 2016);
check (28, 2, 2016);
check (29, 2, 2016);
check (30, 2, 2016);
check (28, 2, 2015);
check (29, 2, 2015);
check (30, 2, 2015);
check (31, 12, 2008);
check (28, 2, 2000);
check (29, 2, 2000);
check (30, 2, 2000);
check (28, 2, 1900);
check (29, 2, 1900);
check (30, 2, 1900);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment