Wertetabelle (Schleifen)
Geben Sie zu einer quadratischen Funktion y = ax2 + bx + c eine Wertetabelle aus. Die Anwenderin kann a, b und c eingeben. Ebenso kann sie den Startwert für x, den Endwert für x (Maximum) und die Schrittweite festlegen.
Beispiel: Die Anwenderin will eine Wertetabelle der quadratischen Funktion y = 5x2 -3x + 2 im Intervall -5 bis 5 mit Schrittweite 0.5.
a := 5
b := -3
c := 2
xstart := -5
xend := 5
schrittweite := 0.5
Es wird eine Wertetabelle mit 21 Werten ausgegeben.
Zusatzaufgabe: Erweitern Sie Ihre Lösung für kubische Funktionen
y = ax3 + bx2 + cx + d.
0 Kommentare
5 Lösung(en)
import java.util.Scanner;
/**
* @author Philipp Gressly (phi AT gressly DOT ch)
*/
public class Wertetabelle {
public static void main(String[] args) {
new Wertetabelle().top();
}
void top() {
double a = einlesen("a");
double b = einlesen("b");
double c = einlesen("c");
double start = einlesen("start");
double end = einlesen("end");
double intervall = einlesen("intervall");
tabelle(a, b, c, start, end, intervall);
}
void tabelle(double a, double b, double c, double start, double end,
double intervall) {
double aktuell = start;
while (aktuell <= end) {
double wert = quadratischeFunktion(aktuell, a, b, c);
System.out.println(aktuell + " -> " + wert);
aktuell = aktuell + intervall;
}
}
double quadratischeFunktion(double x, double a, double b, double c) {
return a * x * x + b * x + c;
}
Scanner sc = new Scanner(System.in);
double einlesen(String groesse) {
System.out.println("Geben Sie " + groesse + " ein: ");
return sc.nextDouble();
}
} // end of class Wertetabelle
def werteQuadFkt(a,b,c,start,stop,intervall):
x = start
xlist = []
while (x <= stop):
xlist.append(x)
x += intervall
for x in xlist:
y = a*x*x+b*x+c
print('x: '+str(x)+'\t'+'y: '+str(y))
a = 5.0
b = -3.0
c = 2.0
start = -5.0
end = 5.0
intervall = 0.5
werteQuadFkt(a,b,c,start,end,intervall)
function quadraticFunc(a, b, c, x, xEnd, inc) {
let arr = [];
for (x; x <= xEnd; x += inc)
arr.push([x, a * (x ** 2) + b * x + c]);
return arr;
}
function cubicFunc(a, b, c, d, x, xEnd, inc) {
let arr = [];
for (x; x <= xEnd; x += inc)
arr.push([x, a * (x ** 3) + b * (x ** 2) + c * x + d]);
return arr;
}
console.table(quadraticFunc(5, -3, 2, -5, 5, 0.5));
console.table(cubicFunc(5, 4, 3, 2, -5, 5, 0.5));
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 6.x | C# 10.x | VS-2022
static IEnumerable<(double x, double y)> Equations(double start, double end, double step, params double[] param) {
for (var x = start; x <= end; x += step) {
var y = 0.0;
for (var k = param.Length - 1; k >= 0; k--)
y += Math.Pow(x, k) * param[^(k + 1)];
yield return (x, y);
}
}
static void PrintEquations(IEnumerable<(double x, double y)> lst) => lst.ToList().ForEach(i => Console.WriteLine($"{i.x,10:f}\t{i.y,10:f}"));
var quadratic = Equations(-5, 5, 0.5, 5, -3, 2);
PrintEquations(quadratic);
var cubic = Equations(-5, 5, 0.5, 5, -3, 3, 2);
PrintEquations(cubic);
Lösung von: Jens Kelm (@JKooP)
// C++ 20 | VS-2022
#include <iostream>
#include <vector>
#include <tuple>
#include <ranges>
#include <iomanip>
std::vector<std::tuple<double, double>> equations(double start, double end, double step, std::vector<double> param) {
const auto ps{ (int)param.size() };
std::vector<std::tuple<double, double>> out{};
for (auto x{ start }; x <= end; x += step) {
auto y{ 0.0 };
for (auto k{ ps - 1}; k >= 0; k--)
y += pow(x, k) * param[static_cast<long long>(ps)-k-1];
out.push_back({ x, y });
}
return out;
}
void print(std::vector<std::tuple<double, double>> v) {
for (const auto& [x, y] : v) { // C++ 20
std::cout << std::fixed << std::setprecision(2) << "x: " << x << std::setw(15) << "y: " << y << "\n";
}
}
int main() {
const auto quadratic{ equations(-5, 5, 0.5, {5, -3, 2 }) };
const auto cubic{ equations(-5, 5, 0.5, { 5, -3, 3, 2 }) };
print(quadratic);
print(cubic);
}
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
a | 5 |
b | -3 |
c | 2 |
x | y |
-5 | 142 |
-4.5 | 116.75 |
-4 | 94 |
-3.5 | 73.75 |
-3 | 56 |
-2.5 | 40.75 |
-2 | 28 |
-1.5 | 17.75 |
-1 | 10 |
-0.5 | 4.75 |
0 | 2 |
0.5 | 1.75 |
1 | 4 |
1.5 | 8.75 |
2 | 16 |
2.5 | 25.75 |
3 | 38 |
3.5 | 52.75 |
4 | 70 |
4.5 | 89.75 |
5 | 112 |
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 1 |
Schwierigkeit: | Mittel |
Webcode: | smw6-u9sy |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |