1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double func(double t)
{
double a=4*(exp(0.8*t)-exp(-0.5*t))/1.3 + 2*exp(-0.5*t);
return a;
}
double f(double t, double y)
{
y=4*(exp(0.8*t)-exp(-0.5*t))/1.3 + 2*exp(-0.5*t);
double a=4*exp(0.8*t)-(0.5*y);
return a;
}
int main()
{
double t0=0;
double y0=2;
double y;
double sum11=0;
double sum12=0;
double sum13=0;
double sum21=0;
double sum22=0;
double sum23=0;
double sum31=0;
double sum32=0;
double sum33=0;
double K1, K2, K3, K4;
double h;
//The Euler method for h=0.1
while(t0<=9.9)
{
h=0.1;
y= y0 + (h*f(t0,y0));
y0=y;
t0=t0+h;
sum11=sum11+abs(func(t0)-y);
}
//The Euler method for h=0.05
while(t0<=9.9)
{
h=0.05;
y= y0 + (h*f(t0,y0));
y0=y;
t0=t0+h;
sum12=sum12+abs(func(t0)-y);
}
//The Euler method for h=0.025
while(t0<=9.9)
{
h=0.025;
y= y0 + (h*f(t0,y0));
y0=y;
t0=t0+h;
sum13=sum13+abs(func(t0)-y);
}
//Heun's method for h=0.1
while(t0<=9.9)
{
h=0.1;
K1=f(t0,y0);
K2=f(t0+h, y0+h*K1);
y = y0+ (h*(K1+K2))/2;
y0=y;
t0=t0+h;
sum21=sum21+abs(func(t0)-y);
}
//Heun's method for h=0.05
while(t0<=9.9)
{
h=0.05;
K1=f(t0,y0);
K2=f(t0+h, y0+h*K1);
y = y0+ (h*(K1+K2))/2;
y0=y;
t0=t0+h;
sum22=sum22+abs(func(t0)-y);
}
//Heun's method for h=0.025
while(t0<=9.9)
{
h=0.025;
K1=f(t0,y0);
K2=f(t0+h, y0+h*K1);
y = y0+ (h*(K1+K2))/2;
y0=y;
t0=t0+h;
sum23=sum23+abs(func(t0)-y);
}
//4-stage Runge-Kutta method for h=0.1
while(t0<=9.9)
{
h=0.1;
K1= h*f(t0,y0);
K2=h*f(t0+ h/2, y0+ K1/2);
K3=h*f(t0+ h/2, y0+ K2/2);
K4=h*f(t0+h, y0+ K3);
y = y0 + (K1+ 2*K2+ 2*K3+ K4)/6;
y0=y;
t0=t0+h;
cout << "Values of t are: " << t0 << endl;
cout << "The approximate values are:" << y << endl;
cout << "The actual values are: " << func(t0) << endl;
cout << "The respective error is: " << abs(func(t0)-y)<< endl;
sum31=sum31+abs(y-func(t0));
}
cout << "The cumulative error is:" << sum31 << endl;
//4-stage Runge-Kutta method for h=0.05
while(t0<=9.9)
{
h=0.05;
K1=h*f(t0,y0);
K2=h*f(t0+ h/2, y0+ K1/2);
K3=h*f(t0+ h/2, y0+ K2/2);
K4=h*f(t0+h, y0+ K3);
y = y0 + (K1+ 2*K2+ 2*K3+ K4)/6;
y0=y;
t0=t0+h;
sum32=sum32+abs(func(t0)-y);
}
//4-stage Runge-Kutta method for h=0.025
while(t0<=9.9)
{
h=0.025;
K1=h*f(t0,y0);
K2=h*f(t0+ h/2, y0+ K1/2);
K3=h*f(t0+ h/2, y0+ K2/2);
K4=h*f(t0+h, y0+ K3);
y = y0 + (K1+ 2*K2+ 2*K3+ K4)/6;
y0=y;
sum33=sum33+abs(func(t0)-y);
}
setprecision(20);
cout << fixed;
cout << " " << "h" << " " << endl;
cout << " 0.1" << " " << " 0.05" << " " << " 0.025" << endl;
cout <<" Euler " << sum11 <<" " << sum12 << " " << sum13 << endl;
cout <<" Heun " << sum21 << " " << sum22 << " " << sum23 << endl;
cout <<"Runge Kutta " << sum31 << " " << sum32 << " " << sum33 << endl;
return 0;
}
|