kramann.info
© Guido Kramann

Login: Passwort:










2.3 C++ Programm "rffunktionen2.cpp"

Auch das folgende Programm arbeitet genauso wie "regulafalsi.cpp". Hier wurde ausserdem die Verwendung der Regula falsi selbst auch als Funktion ausgeführt. regulafalsi(..) ruft dabei polynom(..) auf. polynom(..) wurde etwas kürzer umgeschrieben.

#include <iostream>
#include <math.h>
using namespace std;

double einlesen(char text[])
{
    double x;
    cout<<endl<<text;
    cin>>x;
    return x;
}

double polynom(double x,double ai,double bi,double ci, double di)
{
    return ai+bi*x+ci*x*x+di*x*x*x;
}

double regulafalsi(double ug,double og,double grenze,double ai,double bi,double ci, double di)
{
    double x,y;
    //Regula falsi
    do
    {
        x = (og+ug)*0.5;
        y = polynom(x,ai,bi,ci,di);
        if(y>0.0)
            og=y;
        else
            ug=y;            
    } while(fabs(y)>grenze);

    return x;
}

int main(void)
{
    //Hilfsvariablen deklarieren
    double a,b,c,d,og,ug,grenze;
    double y1,y2,y,x;

    //Parameter-Eingabe
    cout<<"Nullstellen von y=dx3+cx2+bx+a:"<<endl;
    a = einlesen("a=");
    b = einlesen("b=");
    c = einlesen("c=");
    d = einlesen("d=");
    grenze = einlesen("Fehlergrenze grenze=");

    do
    {
        ug = einlesen("untere Grenze ug=");
        y1 = polynom(ug,a,b,c,d);
        og = einlesen("obere Grenze og=");
        y2 = polynom(og,a,b,c,d);

        if(ug>og || y1*y2>=0)
        {
            cout<<endl<<"og muss größer als ug sein";
            cout<<endl<<"y(og)*y(ug) muss kleiner Null sein";
        }
    } while(ug>og || y1*y2>=0);

    //Regula falsi
    y = regulafalsi(ug,og,grenze,a,b,c,d);
    cout<<endl<<"Die Nullstelle liegt bei "<<y;

    return 0;
}

Code 2.3-1: C++ Programm "rffunktionen2.cpp"