Numerische Integration (Simulationen)
Schreiben Sie ein Programm, dass die Wurzelfunktion (sqrt()) von 1.0 bis 5.0 numerisch integriert. Überprüfen Sie ihr Resultat mit der Suchmaschine wolframalpha.com (int 1.0 to 5.0 srt(x)).
Dazu wird das Integral als die Fläche unter der Funktionskurve betrachtet. Diese Fläche wird in sehr viele kleine Streifen aufgeschnitten, deren Fläche aufsummiert wird. Einfachheitshalber werden die Streifen als Rechtecke oder Trapeze (= Trapezregel) betrachtet. Je mehr Streifen Sie verwenden, umso genauer wird die Lösung.
1 Kommentare
6 Lösung(en)
# Numerische Integraion
from math import *
def integral (f,a,b):
dx=(b-a)/1000000.0 # Stuetzstellen (Streifen)
x=a
summe=0
while x<b:
summe=summe+f(x)*dx
x=x+dx
return summe
print integral (sqrt,1.0,5.0)
Lösung von: Martin Guggisberg (Universität Basel / PH FHNW)
public interface XYFunktion {
double y(double x);
} // end of interface Funktion
//*******************************************************
public class Wurzelfunktion implements XYFunktion {
/* overriden */
@Override
public double y(double x) {
if(x > 0) {
return Math.sqrt(x);
}
return 0;
}
} // end of class Wurzel
//*******************************************************
public class Integrator {
static double trapezregel(XYFunktion f, double a, double b, int anzahlIntervalle) {
double resultat = 0.0;
double d = (b - a) / anzahlIntervalle;
while(a < b - d/2) {
resultat = resultat + trapez(f, a, d);
a = a + d;
}
return resultat;
} // end trapezregel
static double trapez(XYFunktion f, double a, double d) {
return d * (f.y(a) + f.y(a + d)) / 2;
}
} // end of class Integrator
//********************************************************
import static Integrator.*;
public class Main {
public static void main(String[] args) {
new Main().top();
}
void top() {
System.out.println("Integral der Wurzelfunktion von 1 bis 5:");
Wurzelfunktion wrz = new Wurzelfunktion();
double integral = trapezregel(wrz, 1.0, 5.0, 1000);
System.out.println(integral);
}
} // end of class Main
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
/**
* Intergiere die Wurzelfunktion von 1 bis 5 mit 10000 Stützstellen.
* @author Philipp Gressly (phi@gressly.ch) für "www.programmieraufgaben.ch"
*/
public class WurzelIntegration {
public static void main(String[] args) {
new WurzelIntegration().top();
}
double
min = 1,
max = 5;
int
anzahlStuetzstellen = 10000;
void top() {
double delta = (max - min) / (anzahlStuetzstellen - 1);
double akt = min + delta / 2.0; // miss in der Mitte des Intervalls
double integral = 0.0;
while (akt < max) {
double flaeche = Math.sqrt(akt) * delta;
integral = integral + flaeche;
akt = akt + delta;
}
System.out.println("Das Integral der Wurzelfunktion von " + min + " bis " + max + " beträgt " + integral);
}
} // end of class WurzelIntegration
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
/**
* Programmieraufgaben.CH
*
* date : 2011-11-09
* author: philipp gressly freimann (Santis Training AG)
* PL/1: www.iron-spring.com
*/
Integration:
procedure options(main);
/* Parameter */
dcl min float dec(16) init (1.0);
dcl max float dec(16) init (5.0e0);
dcl anzahlStuetzstellen bin fixed(31) init (1000);
/* Initialisierung */
dcl delta float dec(16), /* Streifenbreite */
akt float dec(16), /* Mittelpunkt des aktuellen Streifens auf der X-Achse */
integral float dec(16), /* Flächensumme aller Streifen */
flaeche float dec(16); /* Temporaere Flaeche eines Streifens */
delta = (max - min) / (anzahlStuetzstellen - 1);
akt = min + delta / 2.0;
integral = 0.0;
/* Start Simulation */
do while(akt < max);
flaeche = SQRT(akt) * delta;
integral = integral + flaeche;
akt = akt + delta;
end;
put skip list("Das Integral der Wurzelfunktion von ", min, " bis ", max, " beträgt ", integral);
end Integration;
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
function sqrtIntegration(min, max, iPoints) {
var dx = (max - min) / iPoints,
sum = 0;
while (min <= max) {
sum += Math.sqrt(min) * dx;
min += dx;
}
return sum;
}
console.log(sqrtIntegration(1, 5, 10));
console.log(sqrtIntegration(1, 5, 1000));
console.log(sqrtIntegration(1, 5, 100000));
// recht genau, dauert aber eine weile:
console.log(sqrtIntegration(1, 5, Number.MAX_VALUE));
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
from math import sqrt
start = 1.0
ende = 5.0
intervalle = 10000
intervallBreite = (ende - start) / intervalle
summeFlaeche = 0.
for i in range(0, intervalle):
summeFlaeche += sqrt(start + intervallBreite * i) * intervallBreite
print("Fläche unter der Kurve bei", intervalle, "Intervallen: ", summeFlaeche)
Lösung von: Peter Pan (Home Office)
Verifikation/Checksumme:
exakt: 2/3 * (51.5 - 1)
ca.: 6.78689326
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | |
Schwierigkeit: | k.A. |
Webcode: | ef4p-6ws1 |
Autor: | Martin Guggisberg (Universität Basel / PH FHNW) |
Kommentare (2)