Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

MP5.2_Predictor-Corrector Scheme

//Machine Problem No. 5.2 - Adams-Bashforth-Moulton Predictor-Corrector Scheme
//code written by RCQuilala - MSCE (S)

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double f(double y, double t)
{
    double p=-20*y+exp(-t);
    return p;
}

int main()
{
    cout<<"Solve for y(2) from the differential equation"<<endl<<endl;
    cout<<"     y' = -20y + e^(-bt),   y(0) = 0"<<endl<<endl;
    cout<<"using a second-order Adams-Bashforth and third-order"<<endl;
    cout<<"Adams-Moulton Predictor-Corrector Scheme."<<endl<<endl;

    double h;
    cout<<"Enter the step size, h    :   "; cin>>h; cout<<h<<endl<<endl;
    cout<<"t            y(t)"<<endl<<endl;
    int i, n=2/h;
    double t[n+1], y[n+1], y_p[n+1];

    t[0]=0, y[0]=0;
    
    for(i=1;i<n+1;i++)
    {
        t[i]=i*h;
        
        if(i==1)
        {
            y_p[i]=y[i-1]+h*f(y[i-1],t[i-1]);
            //1st-order Adams-Bashforth
            y[i]=y[i-1]+0.5*h*(f(y_p[i],t[i])+f(y[i-1],t[i-1]));
            //2nd-order Adams-Moulton
        }
        else
        {
            y_p[i]=y[i-1]+0.5*h*(3*f(y[i-1],t[i-1])-f(y[i-2],t[i-2]));
            //2nd-order Adams-Bashforth
            y[i]=y[i-1]+(h/12)*(5*f(y_p[i],t[i])+8*f(y[i-1],t[i-1])-f(y[i-2],t[i-2]));
            //3rd-order Adams-Moulton
        }
    }
    
    for(i=0;i<n+1;i++)
    {
        cout<<t[i]<<"      "<<y[i]<<endl;
    }
    
    cout<<endl<<"y(2) = "<<y[n]<<endl;

    return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.