2026-01-27T15:28:05
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
% Created 2026-01-27 Di 15:08
|
||||
% Created 2026-01-27 Di 15:23
|
||||
% Intended LaTeX compiler: pdflatex
|
||||
\documentclass[11pt]{article}
|
||||
\usepackage[utf8]{inputenc}
|
||||
@@ -61,19 +61,34 @@
|
||||
\tableofcontents
|
||||
|
||||
\section{Declaration of self reliance}
|
||||
\label{sec:orgf1c5ba4}
|
||||
\label{sec:orga095243}
|
||||
I hereby declare that I have written this thesis independently in accordance with § 35 para. 7 RaPO (Framework Examination Regulations for Universities of Applied Sciences in Bavaria, BayRS 2210-4-1-4-1-WFK), have not submitted it elsewhere for examination purposes, have not used any sources or aids other than those indicated, and have marked all direct and indirect quotations as such.
|
||||
\section{Task desciption}
|
||||
\label{sec:org4a38da4}
|
||||
Topic 5: Numerical integration methods
|
||||
The aim of this task is to implement three variants of numerical approaches for calculating integrals
|
||||
in a software module.
|
||||
A function for the rectangle method, the trapezoid method, and the Simpson method should be designed and coded in a reusable module.
|
||||
When defining the interface of the C functions, pay attention to reusability.
|
||||
The task also includes developing a sample application to test the three variants. Select two examples of mathematical functions whose integrals are to be determined, with corresponding integration boundaries, and compare the three methods in terms of their precision. Test the limitations of the methods. Substantiate your results.
|
||||
Explain the mathematical relationships underlying the methods in your paper. A graphical representation will help to illustrate the understanding of the relationships.
|
||||
Optimize the source code as far as possible for performance-optimized use on an embedded controller.
|
||||
The result of the task is a project that can be compiled with MS Studio, which enables the software module to be tested (suitable test cases, etc.) using a command line application, plus the documentation specified below.
|
||||
Scope of relevant task-specific documentation
|
||||
\begin{itemize}
|
||||
\item Graphical representation of the software design and compact description of the key relationships (1 page)
|
||||
\item Theoretical explanation of the relationships (mathematical, algorithmic, etc.) that need to be taken into account here (2 pages)
|
||||
\item Documentation of the reference examples used for testing, with graphical representation and explanation of method limitations (2 pages)
|
||||
\end{itemize}
|
||||
\section{Graphical representation of the software design}
|
||||
\label{sec:orgf50f0e3}
|
||||
|
||||
|
||||
\label{sec:org784d853}
|
||||
\begin{center}
|
||||
\includegraphics[width=.9\linewidth]{./graphical-representation-white.drawio.png}
|
||||
\end{center}
|
||||
\section{Theoretical explanation of the relationships}
|
||||
\label{sec:org15650c5}
|
||||
\subsection{\texttt{"integrate.h"}:}
|
||||
\label{sec:orgc09841a}
|
||||
\label{sec:org8218260}
|
||||
\oldsubsection{\texttt{"integrate.h"}:}
|
||||
\label{sec:orgc92e0ac}
|
||||
Interface for \texttt{integrate.c} Module.
|
||||
Here is declared:
|
||||
\begin{itemize}
|
||||
@@ -112,7 +127,7 @@ integ_status_t simpson(integrand_f f, void *ctx, float a, float b, unsigned n,
|
||||
#endif // INTEGRATE_H_
|
||||
\end{verbatim}
|
||||
\subsection{\texttt{"integrate.c"}:}
|
||||
\label{sec:orgbe94578}
|
||||
\label{sec:org80dbf09}
|
||||
Implements the three numerical integration algorithms (midpoint/rectangle, trapezoid, Simpson 1/3) behind the public interface from \texttt{integrate.h}, including argument validation and method-specific constraints.
|
||||
|
||||
The file includes \texttt{integrate.h} and provides the concrete implementations of \texttt{midpoint()}, \texttt{trapezoid()}, and \texttt{simpson()}, each computing an approximation of $$I = \int_a^bf(x)dx$$ by sampling the integrand at specific points and summing weighted contributions.
|
||||
@@ -207,49 +222,49 @@ integ_status_t simpson(integrand_f f, void *ctx, float a, float b, unsigned n,
|
||||
}
|
||||
\end{verbatim}
|
||||
\subsubsection{Common argument checking (\texttt{bad\_args})}
|
||||
\label{sec:org5576bb4}
|
||||
\label{sec:org7e75604}
|
||||
|
||||
A shared internal helper \texttt{bad\_args()} is implemented as \texttt{static inline} to reduce call overhead and to allow the compiler to inline it for performance on embedded targets.
|
||||
|
||||
It returns an error condition if \texttt{f == NULL, out == NULL, n == 0} or the interval is invalid (the code checks \texttt{b > a}).
|
||||
If \texttt{bad\_args()} reports invalid inputs, each public integration function returns \texttt{INTEG\_ERR\_BAD\_ARGS} immediately.
|
||||
\subsubsection{Midpoint / rectangle method (\texttt{midpoint})}
|
||||
\label{sec:orgb278e27}
|
||||
\label{sec:org9f6d06d}
|
||||
\begin{enumerate}
|
||||
\item Mathematical idea
|
||||
\label{sec:orgd41c3d4}
|
||||
\label{sec:orga069e15}
|
||||
|
||||
The interval [a,b] is split into n subintervals of equal width $$h=(b-a)/n$$, and each subinterval [xi,xi+1] is approximated by a rectangle whose height is the function value at the midpoint $$xi+h/2$$.
|
||||
The Formula is as follows: \cslcitation{1}{[1]}
|
||||
|
||||
$$\approx h \sum_{i=1}^n f({x_{i-1}+x_i}/2)$$
|
||||
\item Implementation details
|
||||
\label{sec:orgebae471}
|
||||
\label{sec:orga095516}
|
||||
The code computes \texttt{h}, initializes the first midpoint \texttt{x = a + 0.5fh}, then loops exactly \texttt{n} times, accumulating \texttt{sum = sum + f(x, ctx)} and stepping \texttt{x = x + h}.
|
||||
|
||||
Finally, the result is written as \texttt{out = sum * h} and the function returns \texttt{INTEG\_OK}.
|
||||
\end{enumerate}
|
||||
\subsubsection{Trapezoid method (\texttt{trapezoid})}
|
||||
\label{sec:orga8108f1}
|
||||
\label{sec:org356a6b8}
|
||||
\begin{enumerate}
|
||||
\item Mathematical idea
|
||||
\label{sec:org31aecc1}
|
||||
\label{sec:orgd155bc9}
|
||||
|
||||
Each subinterval is approximated by a trapezoid formed by the points $$(xi,f(xi))$$ and $$(xi+1,f(xi+1))$$, giving the composite trapezoid rule.
|
||||
|
||||
This corresponds to the standard weighted sum \cslcitation{2}{[2]}
|
||||
$$ = h/2 (f(x_0)+2f(x_1)+\dots+2f(x_{n-1})+f(x_n))$$
|
||||
\item Implementation details
|
||||
\label{sec:org36dc554}
|
||||
\label{sec:org20242c9}
|
||||
|
||||
The code computes \texttt{h}, then initializes \texttt{sum = 0.5f*(f(a,ctx)+f(b,ctx))} to apply the half-weight to the endpoints.
|
||||
It iterates from the first interior node \texttt{x = a + h} for \texttt{i = 1..n-1}, adds each interior sample once (\texttt{sum = sum + f(x,ctx)}), then scales by \texttt{h} via \texttt{*out = sum * h}.
|
||||
\end{enumerate}
|
||||
\subsubsection{Simpson 1/3 method (\texttt{simpson})}
|
||||
\label{sec:org6457a29}
|
||||
\label{sec:orga43d4c9}
|
||||
\begin{enumerate}
|
||||
\item Mathematical idea
|
||||
\label{sec:orgd9bfe20}
|
||||
\label{sec:org4292208}
|
||||
|
||||
Simpson's 1/3 rule approximates the function by piecewise quadratic polynomials over pairs of subintervals, which leads to alternating weights 4 and 2 for interior points.
|
||||
|
||||
@@ -257,7 +272,7 @@ The composite Simpson rule requires an even number of subintervals nn so that th
|
||||
The formula is as such: \cslcitation{3}{[3]}
|
||||
$$ \approx (h/3) [f(x_0)+4f(x_1)+2f(x_2)+ \dots 2f(x_{n-2})+4f(x_{n-1})+f(x_n)]$$
|
||||
\item Implementation details and constraint handling
|
||||
\label{sec:org4ce84b4}
|
||||
\label{sec:orgb038bbe}
|
||||
|
||||
After basic argument validation, the function checks \texttt{if (n \& 1u)} and returns \texttt{INTEG\_ERR\_N\_EVEN\_REQUIRED} if \texttt{n} is odd, enforcing the ``even n'' requirement from the interface contract.
|
||||
|
||||
@@ -266,7 +281,7 @@ The final formula implemented is \\
|
||||
\texttt{out = (h/3)(f(a)+4*sum\_odd+2*sum\_even+f(b))}, which matches the documented Simpson weighting scheme.
|
||||
\end{enumerate}
|
||||
\subsection{Performance-oriented aspects (embedded focus)}
|
||||
\label{sec:org5f114cd}
|
||||
\label{sec:orgf872e63}
|
||||
|
||||
The design avoids dynamic allocation and uses a caller-provided output pointer (\texttt{float *out}), which is predictable and typical for embedded C modules.
|
||||
|
||||
@@ -275,25 +290,25 @@ Using \texttt{static inline} for \texttt{bad\_args} and keeping loop bodies simp
|
||||
|
||||
When checking for evenness of \texttt{n} the simpson function uses \texttt{\& u1} instead of \texttt{\% 2}. It is not possible to substitue a \texttt{/2} for a \texttt{>{}>{}1} for the float variable type. Such tricks can not have been used for any values with the float type.
|
||||
\section{Documentation of the reference examples used for testing}
|
||||
\label{sec:org2d1f7ee}
|
||||
\label{sec:orgd361433}
|
||||
\texttt{"main.c"} is a small command-line test application that demonstrates how to use the reusable integration module by defining example integrand functions, calling all three numerical methods, and printing absolute errors against known exact results.
|
||||
|
||||
The application includes \texttt{integrate.h} and uses standard library functionality (printing and absolute error via \texttt{fabsf}) to compare the numeric results to a known reference (``exact'') value.
|
||||
Its purpose is not to be a generic framework, but a compact test harness that exercises the module API and makes method limitations visible at runtime (e.g., Simpson's even-\texttt{n} requirement).
|
||||
\subsection{Test integrand design}
|
||||
\label{sec:org3cbabb2}
|
||||
\label{sec:org7f48d68}
|
||||
Parameterized quadratic via context pointer
|
||||
|
||||
A small struct \texttt{quad\_t} stores coefficients \texttt{a2}, \texttt{a1}, \texttt{a0} for a quadratic polynomial $$f(x)=a_2x^2+a_1x+a_0$$, showing how the integrator's \texttt{void *ctx} can carry user-defined parameters without globals.
|
||||
The function \texttt{f\_quad(float x, void ctx)} casts \texttt{ctx} to \texttt{const quad\_t} and evaluates the polynomial, matching the generic \texttt{integrand\_f} interface expected by the integration module.
|
||||
\subsection{Test runner (\texttt{run\_one})}
|
||||
\label{sec:org010d856}
|
||||
\label{sec:org700fda6}
|
||||
|
||||
The helper \texttt{run\_one(...)} calls \texttt{midpoint}, \texttt{trapezoid}, and \texttt{simpson} with the same function, bounds, and number of subintervals, storing results into three local floats (\texttt{r}, \texttt{t}, \texttt{s}).
|
||||
It prints the integration problem setup (function name, bounds, \texttt{n}), prints the exact reference value, and prints each method's absolute error using \texttt{fabsf(result - exact)} for a direct precision comparison.
|
||||
Simpson's status return is checked: if \texttt{simpson} returns \texttt{INTEG\_OK}, the Simpson result/error is printed, otherwise a message is printed indicating it was not computed because \texttt{n} must be even.
|
||||
\subsection{Reference examples in \texttt{main()}}
|
||||
\label{sec:orgd2d9b7a}
|
||||
\label{sec:org8334b01}
|
||||
|
||||
Four concrete test cases are instantiated using \texttt{quad\_t} coefficients and passed to \texttt{run\_one}.
|
||||
They have been chosen in such a way that many edge cases are accounted for.
|
||||
@@ -487,9 +502,9 @@ int main(void)
|
||||
}
|
||||
\end{verbatim}
|
||||
\subsubsection{Results}
|
||||
\label{sec:org8a0d097}
|
||||
\label{sec:orga992d73}
|
||||
\section{Bibliography}
|
||||
\label{sec:org8cd8f85}
|
||||
\label{sec:orgab8c92c}
|
||||
\begin{cslbibliography}{0}{0}
|
||||
\cslbibitem{1}{\cslleftmargin{[1]}\cslrightinline{“Engineering at Alberta Courses Rectangle Method,” Jan. 2026. Available: \url{https://engcourses-uofa.ca/books/numericalanalysis/numerical-integration/rectangle-method}}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user