W
sebi707 schrieb:
Die allgemeine Formel für die Koeffizienten a, b und c findest du Beispielsweise hier: http://www.arndt-bruenner.de/mathe/10/parabeldurchdreipunkte.htm
Die Gleichungen am Ende stimmen nicht ganz. man muss b und c noch negieren. So geht's:
#include <iostream>
struct parabel
{
parabel( double a, double b, double c )
: m_a(a), m_b(b), m_c(c)
{}
double operator()( double x ) const
{
return (m_a*x + m_b)*x + m_c;
}
friend std::ostream& operator<<( std::ostream& out, const parabel& p )
{
return out << p.m_a << "x^2 + " << p.m_b << "x + " << p.m_c;
}
private:
double m_a, m_b, m_c;
};
int main()
{
using namespace std;
double x1, y1;
double x2, y2;
double x3, y3;
cout << "Eingabe drei Punkte x1,y1 x2,y2 x3,y3\n";
if( cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 )
{
auto den = (x1-x2)*(x2-x3)*(x3-x1);
auto p = parabel(
(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/den,
(x1*x1*(y3-y2) + x2*x2*(y1-y3) + x3*x3*(y2-y1))/den,
(x1*x1*(x3*y2-x2*y3)+x1*(x2*x2*y3-x3*x3*y2)+x2*x3*y1*(x3-x2))/den );
cout << "Ergebnis: " << p << endl;
// Kontrolle:
cout << "f(x1)=" << p(x1) << endl;
cout << "f(x2)=" << p(x2) << endl;
cout << "f(x3)=" << p(x3) << endl;
}
return 0;
}
Ausgabe:
Eingabe drei Punkte x1,y1 x2,y2 x3,y3
-1 12 2 15 5 -18
Ergebnis: -2x^2 + 3x + 17
f(x1)=12
f(x2)=15
f(x3)=-18
Gruß
Werner