numerical methods with c codes
TRANSCRIPT
-
8/2/2019 Numerical Methods With c Codes
1/60
Numerical Methods Algorithm in C++
The scope of these lectures is to study the numerical methods frequently used in
Physics and to write (where possible) their C++ codes. The main topics include.
The Solution of Nonlinear Equations f(x) = 0o Bisection Methodo Newton Raphson Methodo Secant Method
The Solution of Linear Systems AX = Bo Gauss Jordan Methodo Cramers Ruleo Jacobi Methodo Gauss Seidel iterative Methodo Exercise
Numerical Integrationo Riemanns Sumo Trapezoidal Ruleo Simpsons 1/3 Ruleo Simpsons 3/8 Ruleo Monte Carlo -1 Methodo Monte Carlo -2 Method
Ordinary Differential Equationso Euler Methodo Euler Improved Methodo Runge Kutta 4th order Methodo Runge Kutta Fehlberg Method
-
8/2/2019 Numerical Methods With c Codes
2/60
The Solution of Nonlinear Equations f(x) = 0
Bisection Method or interval halving method
The bisection method is one of the bracketing methods for finding roots of
equations.
Given a function f(x)=0 and an interval which might contain a root, perform a
predetermined number of iterations using the bisection method. The interval must
be large enough to avoid discontinuity.
The method works like this if [a, b] is the interval in which the root of f(x) lies then
we compute f(a) and f(b). Now we compute f(x) at M=a+b/2 and then calculate
f(M).
Now multiply f(a) with f(M) if the answer of this is negative than the root lies in
[a, M] if not (i.e. positive) then root lies in [M, b].
Example: Consider the non-linear equation
f(x) = x3+x
2-3x-3=0
find the root in the interval [1, 2]
Sol:- Now f(1) = -4
And f(2) = 3
Sign change between [1, 2] guarantees a root in this interval.
Now consider mid point i.e. M = 1.5 than
f(1.5) = -1.475
And f(1)f(1.5) = +ive value
Hence we take the interval [1.5, 2] and repeat the process again and again till we
find that x = 1.731925 is a root to the above equation up to 6 decimal places.
-
8/2/2019 Numerical Methods With c Codes
3/60
C++ program for the above example
#include
#include
#include
#include
double f(double x)
{
double value;
value=pow(x,3)+pow(x,2)-3*x-3;
return value;
}
void main()
{
clrscr();
double a,b,M,ans,comp;
int i;
ofstream res("bisectionresults.txt");
couta>>b;
for(i=0;i
-
8/2/2019 Numerical Methods With c Codes
4/60
cout
-
8/2/2019 Numerical Methods With c Codes
5/60
Newton Raphson Method
The method states that if xn is an approximate to an exact root of the equation f(x)
= 0 then a better approximation is in general given by xn+1 where
xn+1 = xn - f(xn)/f(xn)
Example illustrates the method
Example:
Using Newton Raphson method find the solution of e-x
- sinx = 0 correct up to four
decimal places near x0 = 0.5.
Sol:
f(x) = e-x
sinx
hence
f(x) = -(e-x
+ cosx)
Now
xn+1 = xn - e-x
sinx /-(e-x
+ cosx)
for n = 0 x1 = 0.5885
for n = 1 x2 = 0.586
for n = 2 x3 = 0.5885
hence x = 0.5885 is the root of the equation.
-
8/2/2019 Numerical Methods With c Codes
6/60
C++ program for the above example
#include
#include
#include
#include
double f(double x)
{
double value;
value=1.0/exp(x)-sin(x);
return value;
}
double df(double x)
{
double value;
value=-(1.0/exp(x)+cos(x));
return value;
}
void main()
{
clrscr();
double init,Xn1,Xn,ans;
int i;
ofstream res("New_Raph.txt");
cout
-
8/2/2019 Numerical Methods With c Codes
7/60
cin>>init;
Xn=init;
for(i=1;i
-
8/2/2019 Numerical Methods With c Codes
8/60
Secant Method
The basic disadvantage with Newton Raphson method is that it needs to evaluate
the derivative of f(x). This can be avoided by using the definition of derivatives.
That is we can replace f(x) with the gradient of the secant of f(x) defined as
f'(xn) ~= f(xn) - f(xn-1) / xn xn-1
putting this in
xn+1 = xn - f(xn)/f(xn)
we get
xn+1 = xn f(xn) (xn xn-1) / f(xn) - f(xn-1)
on solving we get
xn+1 = xn-1 f(xn) xn f(xn-1) / f(xn) f(xn-1)
this is known as secant formula. Note that this formula requires two values namely
x0 and x1, between whom the root lays, for the evaluation of the formula.
Example:
Use the secant method to estimate the root of the equation f(x) = sinx 5x + 2 = 0,
given that x0 = 0.4 and x1 = 0.6.
Sol:
For n = 1 the formula becomes
x2 = x0 f(x1) x1 f(x0) / f(x1) f(x0)
putting values and solving we find x2 = 0.494, x3 = 0.4950 and x4 = 0.4950.
hence 0.4950 is the root of the equation.
-
8/2/2019 Numerical Methods With c Codes
9/60
C++ program for the above example
#include
#include
#include
#include
double f(double x)
{
double value;
value=sin(x)-5*x+2;
return value;
}
void main()
{
clrscr();
double init,final,Xn1,Xn,Xn_1,ans;
int i;
ofstream res("Secant.txt");
coutinit>>final;
Xn=init;
Xn_1=final;
for(i=1;i
-
8/2/2019 Numerical Methods With c Codes
10/60
res
-
8/2/2019 Numerical Methods With c Codes
11/60
The Solution of Linear Systems AX = B
Simultaneous linear equations arise in many practical situations. These equations
can be solved either by direct method or by iterative methods. We discuss bothdirect and indirect (iterative) methods.
Consider the system of algebraic equations
a11x1+ a12x2+ a13x3+. a1nxn = c1
a21x1+ a22x2+ a2nxn = c2
..
..
an1x1+ an2x1+ an3x1+ .. annx1 = cn
we can write the above system as
AX = C
Where A is
a11 a12 a13 a1n
a21 a22 a23 a1n
an1 an2 an3 ann
And X is And B is
x1 b1x2 b2
. .
. .
xn bn
-
8/2/2019 Numerical Methods With c Codes
12/60
Gauss Jordan Method
In this method we eliminate coefficient of x1 from each equation except from first,
then eliminate x2 from second equation and so on for the next equations. i.e.
making the diagonal elements of the matrix of the coefficients equal to 1 and all
others equal to zero.
Example:
Solve the following system of equation using Gauss Jordan method
2x1+ 2x2+4x3 = 18
1x1+ 3x2+2x3 = 13
3x1+ 1x2+3x3 = 14Sol:
x1 x2 x3 c
2 2 4 181 3 2 13
3 1 3 14
1 1 2 9
1 3 2 13
3 1 3 14
1 1 2 90 2 0 4
0 -2 -3 -13
1 1 2 90 1 0 2
0 -2 -3 -13
1 0 2 7
0 1 0 2
0 0 -3 -9
1 0 2 7
0 1 0 20 0 1 3
1 0 0 1
0 1 0 2
0 0 1 3
Hence x1=1, x2 = 2 and x3 = 3
-
8/2/2019 Numerical Methods With c Codes
13/60
C++ program for the above example
#include
#include
#include
#include#include
void main()
{
clrscr();
cout
-
8/2/2019 Numerical Methods With c Codes
14/60
//muliplying first element of row 3 with 1 of 1st row
//to make 1st element of row 3 zero
arr[2][0] = -mt2*arr[0][0]+arr[2][0];
arr[2][1] = -mt2*arr[0][1]+arr[2][1];
arr[2][2] = -mt2*arr[0][2]+arr[2][2];
arr[2][3] = -mt2*arr[0][3]+arr[2][3];
//now making 2nd element of 2nd row equal to 1
t=1;
if (arr[1][1] != 1) // start second pivot
{
t = (1/(arr[1][1]));
arr[1][1] = t * arr[1][1];
}
//no need to multi arr[1][0] with t since it is zero
arr[1][2] = t * arr[1][2];arr[1][3] = t * arr[1][3];
//making 2nd element of 1st row zero
float mt3=arr[0][1];
arr[0][1] = -mt3*arr[1][1]+arr[0][1];
arr[0][2] = -mt3*arr[1][2]+arr[0][2];
arr[0][3] = -mt3*arr[1][3]+arr[0][3];
//making second element of 3rd row zero
float mt4 = arr[2][1];
arr[2][1] = -mt4*arr[1][1]+arr[2][1];
arr[2][2] = -mt4*arr[1][2]+arr[2][2];
arr[2][3] = -mt4*arr[1][3]+arr[2][3];
t=1;
if (arr[2][2] != 1) // start third pivot
{
t = (1/(arr[2][2]));
arr[2][2] = t * arr[2][2];
}
//making 3rd element of row three equal to 1
arr[2][3] = t * arr[2][3];//making remaining elements of row 2 and 3 zero
float mt5 = arr[1][2];
arr[1][2] = -mt5*arr[2][2]+arr[1][2];
arr[1][3] = -mt5*arr[2][3]+arr[1][3];
float mt6 = arr[0][2];
-
8/2/2019 Numerical Methods With c Codes
15/60
arr[0][2] = -mt6*arr[2][2]+arr[0][2];
arr[0][3] = -mt6*arr[2][3]+arr[0][3];
//prining the solved matrix
for(i=0; i
-
8/2/2019 Numerical Methods With c Codes
16/60
Cramers Rule
It stated that if AX = C is the system of equations and D is the determinant of A,
D1 is the determinant of A after replacing the 1st
column of A be C, and D2 is the
determinant of A after replacing the 2nd column of A by C and D3 is the
determinant of A after replacing the 3rd
column of A by C, than
x1 = D1/D
x2 = D2/D
x3 = D3/D
Example:
Find the solution of3x1+x2+x3=3x1-3x2+x3=5
x1+x2+4x3=4
Using Cramers Rule
Sol:
D = -38
D1= -38
D2= 38
D3= 38
Hence
x1 = 1
x2 = -1
and
x3 = 1
-
8/2/2019 Numerical Methods With c Codes
17/60
C++ program for the above example
#include
#include
#include
#include#include
void main()
{
clrscr();
cout
-
8/2/2019 Numerical Methods With c Codes
18/60
float x3 = D3/D;
delay(700);
cout
-
8/2/2019 Numerical Methods With c Codes
19/60
Jacobi Method
Given a system of linear equations
AX = C
Where the diagonal elements of A are none zero. The system can be written as
ak+1
= Bxk+b
where the elements of B are
bij = -aij / aii
and bii=0
initial guess is x0
= (0,0,0,0,.)T
and continue the process of iteration for different
values of k, unless we get the required accuracy.For the system
a11x1+a12x2+a13x3 = a14, a21x1+a22x2+a23x3 = a24, a31x1+a32x2+a33x3 = a34
The iterations equations can be written as
x1k+1
= 1/a11(a14-a12x2k-a13x3
k), x2
k+1= 1/a22(a24-a21x1
k-a23x3
k), x1
k+1= 1/a33(a34-a31x1
k-
a33x3k)
Example:
Consider the system, 8x+y-z = 8, x-7y+2z = -4, 2x+y+9z =12 Find sol with x,y and
z starting at zero.
Sol:
Setting the equations as
x = 1/8(8-y+z) -1, y = 1/7(4+x+2z) -2, z = 1/9(12-2x-y) -3
putting x,y and z = 0, we get
x = 1.095, y = 1.095 and z = 1.048
putting these values in 1,2 and 3
and repeating the process enough time we see that the solution
converges to the values.
x = 1.000 , y = 1.000 and z = 1.000
-
8/2/2019 Numerical Methods With c Codes
20/60
C++ program for the above example
#include
#include
#include
#include#include
void main()
{
clrscr();
cout
-
8/2/2019 Numerical Methods With c Codes
21/60
X2=X2_it;
X3=X3_it;
}
}
delay(700);
cout
-
8/2/2019 Numerical Methods With c Codes
22/60
Gauss Seidel iterative Method
The Jacoby method is difficult to implement because we need the equations to be
in such an order that the system becomes diagonally dominant also the
convergence is slow. Another method that is also diagonally dominant (i.e. largest
values must come in the diagonal elements of matrix A) but converges faster that
Jacoby method is Gauss Seidel method. The iterations equations of this method are
slightly different from the Jacoby method as given below.
Consider the system
a11x1+a12x2+a13x3 = a14,
a21x1+a22x2+a23x3 = a24,
a31x1+a32x2+a33x3 = a34
The iterations equations for Gauss Seidel method are
x1k+1
= 1/a11(a14-a12x2k-a13x3
k),
x2k+1
= 1/a22(a24-a21x1k+1
-a23x3k),
x1k+1
= 1/a33(a34-a31x1k+1
-a33x3k+1
)
That is in second and third equations the values already calculated are used.
-
8/2/2019 Numerical Methods With c Codes
23/60
C++ program for the above example
#include
#include
#include
#include#include
void main()
{
clrscr();
cout
-
8/2/2019 Numerical Methods With c Codes
24/60
X2=X2_it;
X3=X3_it;
}
}
delay(700);
cout
-
8/2/2019 Numerical Methods With c Codes
25/60
Excrcise
Consider the network as shown below
Applying Kirchoff s loop rule we obtain
Solve the network #2 for the currents given the following value for the resistors and
battery:
Consider the network
Applying Kirchoff s loop rule we obtain
Solve the network #3 for the currents given the following value for the resistors and
batteries:
-
8/2/2019 Numerical Methods With c Codes
26/60
Numerical integration
Riemanns Sum
Let be continuous over the interval , and let
be a partition, then the definite integral is given by
b n
f(x) dx = lim f(xn-1) hna 1
Where xn-1 [x0, xn] and the mesh size of the partition goes to zero in the
"limit," i.e. h 0 as n.Example:
Compute the integral of the function f(x) = x2
over the interval [1, 2] with n=20.
Sol:
Here x0 = 1, x1 = 1+0.05, x2 = 1.2(0.05),x20 = 2, h = 2-1/20 = 0.05
2
Hence x2 dx = f[x0] h+ f[x1] h+ f[x2] h+ f[xn-1] h+ f[xn] h1
= 2.25875
Exact answer is = 2.3333333333
-
8/2/2019 Numerical Methods With c Codes
27/60
C++ program for the above example
#include
#include
#include
#include
#include
float f(float x)
{
float value;
value=pow(x,2);
return value;
}
void main()
{
clrscr();
int n;
float a,b,h,Fx,sum=0.0;
ofstream res("Riemann.txt");
couta>>b>>n;
h=(b-a)/n;
clrscr();
//performing itterations
-
8/2/2019 Numerical Methods With c Codes
28/60
cout
-
8/2/2019 Numerical Methods With c Codes
29/60
Trapezoidal Rule
Consider y = f(x) continuous over [a=x0, b=xn], where b = a + nh. n = 1,2,3 than
by definition trapezoidal rule states that.b
f(x) dx = h/2 [f(x0)+2{f(x1)+ f(x2)+ f(xn-1)}+f(xn)]a
where
h = b-a/n
and
x0 = a, x1 = a+h, x2 = a+2h, x3 = a+3h xn-1 = a+(n-1)h+ xn = b
-
8/2/2019 Numerical Methods With c Codes
30/60
C++ program for Trapezoidal Rule
#include
#include
#include
#include
#include
float f(float x)
{
float value;
value=2+2*cos(2*sqrt(x));
return value;
}
void main()
{
clrscr();
int n;
float a,b,h,sum=0.0;
ofstream res("Trapezoidal.txt");
couta>>b>>n;
h=(b-a)/n;
sum=f(a)+f(b);
clrscr();
//performing itterations
-
8/2/2019 Numerical Methods With c Codes
31/60
cout
-
8/2/2019 Numerical Methods With c Codes
32/60
Simpsons 1/3 Rule
Consider y = f(x) continuous over the interval [a, b]. Suppose that the interval is
divided into n subintervals (where n must be an even number) with
h = b-a/n
such that x0 = a, x1 = a+h, x2 = a+2h, x3 = a+3h xn-1 = a+(n-1)h+ xn = b. Than
Simpsons Rule states that
b
f(x) dx = h/3 [f(x0)+2{f(x2)+ f(x4)+ f(xn-2)}+ 4{f(x1)+ f(x3)+ f(xn-1)}+f(xn)]a
is an approximation of the integral of f(x) over the interval [a, b]
-
8/2/2019 Numerical Methods With c Codes
33/60
C++ program for Simpsons 1/3 Rule
#include
#include
#include
#include
#include
float f(float x)
{
float value;
value=2+2*cos(2*sqrt(x));
return value;
}
void main()
{
clrscr();
int n;
float a,b,h,sum=0.0;
ofstream res("Simpsons3.txt");
couta>>b>>n;
h=(b-a)/n;
sum=f(a)+f(b);
clrscr();
//performing itterations
-
8/2/2019 Numerical Methods With c Codes
34/60
for(int k=1;k
-
8/2/2019 Numerical Methods With c Codes
35/60
Simpsons 3/8 Rule
Consider y = f(x) continuous over the interval [a, b]. Suppose that the interval is
divided into n subintervals (where n must divisible by 3) with
h = b-a/n
such that x0 = a, x1 = a+h, x2 = a+2h, x3 = a+3h xn-1 = a+(n-1)h+ xn = b. Than
Simpsons 3/8 Rule states that
b
f(x) dx = 3h/8 [f(x0)+3{f(x1)+ f(x2)+ f(xn-1)}+f(xn)]a
Is an approximation of the integral of f(x) over the interval [a, b]
-
8/2/2019 Numerical Methods With c Codes
36/60
C++ program for Simpsons 3/8 Rule
#include
#include
#include
#include
#include
float f(float x)
{
float value;
value=3*(1/exp(x))*sin(pow(x,2))+1;
return value;
}
void main()
{
clrscr();
int n;
float a,b,h,sum=0.0;
ofstream res("Simpsons3-8.txt");
couta>>b>>n;
h=(b-a)/n;
sum=(f(a)+f(b));
clrscr();
//performing itterations
-
8/2/2019 Numerical Methods With c Codes
37/60
for(int k=1;k
-
8/2/2019 Numerical Methods With c Codes
38/60
Monte Carlo -1 Method
Monte Carlo methods can be thought of as statistical simulation methods that
utilize sequences of random numbers to perform the simulation. The name "Monte
Carlo'' was coined by Nicholas Constantine Metropolis (1915-1999), because of
the similarity of statistical simulation to games of chance, and because Monte
Carlo is a center for gambling and games of chance.
From previous discussion of trapezoidal rule and Simpsons rule we found out that
the integral of a function f(x) over the interval [a, b] can be written as according to
mid point rule. (Where c is the mid point between a and b)
.
Or equivalently
.
The above equations are for n = 1, i.e we are using the whole interval from a to b
and have not made any subintervals. If we do divide the interval into sub interval
than f(c) will be the average of all the values of f(ck) in the sub interval. In that
case the above relation can be written as
.
using h = (b-a)/n
, where .
With error
, where .
-
8/2/2019 Numerical Methods With c Codes
39/60
The Algorithm for the Monte Carlo method goes like this.
Pickn randomly distributed points in the interval .
Determine the average value of the function
.
Compute the approximation to the integral
.
An estimate for the error is
, where
Every time a Monte Carlo simulation is made using the same sample size itwill come up with a slightly different value. Larger values of will
produce more accurate approximations. The values converge very slowly of
the order .
-
8/2/2019 Numerical Methods With c Codes
40/60
Following Excel Sheet demonstrate the procedure for Monte Carlo Method
In column A we have used =RAND()*PI()/2 to generate 100random numbers between 0 and Pi.
In column B we have calculated the values if function f(x) = Cosxfrom the values of the random numbers generated in column ausing the function =COS(A#) where # represent the number of
cell just next to the cell of column B.
In cell C2 we calculate the average of the values of column Busing the function =AVERAGE(B2:B101).
In cell D2 we have calculated the numerical answer using(PI()/2)*C2. Which is the required numerical solution.
Next four columns contain the calculations of the error in theroutine. According to the formula given above.
-
8/2/2019 Numerical Methods With c Codes
41/60
C++ program for Monte Carlo -1 Method
#include
#include
#include
#include
#include
#include
double f(double x)
{
double value;
value=cos(x);
return value;
}
void main()
{
clrscr();
double a,b;
couta>>b;
int n=1;
coutn;
double random[1000];
-
8/2/2019 Numerical Methods With c Codes
42/60
//generating random numbers
for(int j=1;j
-
8/2/2019 Numerical Methods With c Codes
43/60
getch();
}Use Monte Carlo Method to find
the numerical solution of
1. f(x) = cos x, with a=0 and b = Pi/2 = 1.570796.Ans : 1.002148
2. Let . Use the Monte Carlomethod to calculate approximations to the
integral . Use 500 random
number.
Ans : 5.2659091
3. Let . Use the MonteCarlo method to calculate approximations to
the integral . Use 500
Random Number
Ans: 1.035687
-
8/2/2019 Numerical Methods With c Codes
44/60
Monte Carlo -2 Method
The Monte Carlo method can be used to numerically approximate the value of a
double integral.
The algorithm for Monte Carlo method with two variable or double integrals goes
like this
Pick n randomly distributed points inthe rectangle
Determine the average value of the function
.
Compute the approximation to the integral
.
An estimate for the error is
, where .
-
8/2/2019 Numerical Methods With c Codes
45/60
C++ program for Monte Carlo -2 Method
#include
#include
#include
#include
#include
#include
double f(double x,double y)
{
double value;
value=4-pow(x,2)-pow(y,2);
return value;
}
void main()
{
clrscr();
double a,b;
couta>>b;
double c,d;
coutc>>d;
int n=1;
coutn;
-
8/2/2019 Numerical Methods With c Codes
46/60
double random1[1000];//For the first variable say x
//generating random numbers
for(int j=1;j
-
8/2/2019 Numerical Methods With c Codes
47/60
double ans=avg*(b-a)*(d-c);//computes answer of integral
delay(700);
cout
-
8/2/2019 Numerical Methods With c Codes
48/60
Ordinary Differential Equations
Euler Method
To approximate a solution to the equation
y(x) = f(x, y(x))
Where x is independent variable and y is dependent on x.
With initial condition
y(x0) = y0
On an interval Ix = [x0, x], choose a small value for h > 0 and an integer n such that
x0 + nh = x. n =1,2,3,
Compute y1, y2, . . . , ym+1 using the difference equation
ym+1 = ym + hf(xm, ym). m = 0,1,2,n-1
Then ym+1= yx is an approximation for y(x0 + nh) = y(x).
-
8/2/2019 Numerical Methods With c Codes
49/60
C++ program for Eulers Method for y(x) = f(x, y(x)) = 0.04x, y(0) =50, Ix = [0, 50]
#include
#include
#include
#include
#include
double f(double a,double b){
double value;
value=(0.04)*b;
return value;
}
void main()
{
again:
clrscr();
int n;
double X0,Y0,X,Y,h;
coutX0;
coutY0;
cout
-
8/2/2019 Numerical Methods With c Codes
50/60
cin>>X;
coutn;
if(n
-
8/2/2019 Numerical Methods With c Codes
51/60
Euler Improved Method
To approximate a solution to the equation
y(x) = f(x, y(x))
Where x is independent variable and y is dependent on x.
With initial condition
y(x0) = y0
On an interval Ix = [x0, x], choose a small value for h > 0 and an integer n such that
x0 + nh = x. n =1,2,3,
Compute y1, y2, . . . , ym+1 using the difference equationym+1 = ym + h/2(f(xm+h, z)+f(xm,ym)). m = 0,1,2,n-1
where z = ym+1 = ym + h f(xm,ym) m = 0,1,2,n-1
Then ym+1= yx is an approximation for y(x0 + nh) = y(x).
-
8/2/2019 Numerical Methods With c Codes
52/60
C++ program for Eulers Improved Method for y(x) = f(x, y(x)) = 0.04x, y(0) =50,
Ix = [0, 50]
#include
#include#include
#include
#include
double f(double a,double b)
{
double value;
value=(0.04)*b;
return value;
}
void main()
{
again:
clrscr();
int n;
double X0,Y0,X,Y,h;
coutX0;coutY0;
coutX;
-
8/2/2019 Numerical Methods With c Codes
53/60
coutn;
if(n
-
8/2/2019 Numerical Methods With c Codes
54/60
Runge Kutta 4th order Method
To approximate a solution to the equation
y(x) = f(x, y(x))
Where x is independent variable and y is dependent on x.
With initial condition
y(x0) = y0
On an interval Ix = [x0, x], choose a small value for h > 0 and an integer n such that
x0 + nh = x. n =1,2,3,
Compute y1, y2, . . . , ym+1 using the difference equationk1= h* f(xm,ym)
k2 = h* f(xm+h/2,ym+k1/2)
k3 = h* f(xm+h/2,ym+k2/2)
k4 = h* f(xm+h/2,ym+k3)
ym+1 = ym + 1/6(k1+2k2+2k3+k4). m = 0,1,2,n-1
Then ym+1= yx is an approximation for y(x0 + nh) = y(x).
-
8/2/2019 Numerical Methods With c Codes
55/60
C++ program for R-K 4th
order Method for y(x) = f(x, y(x)) = 0.04x, y(0) =50, Ix =
[0, 50]
#include
#include#include
#include
#include
double f(double a,double b)
{
double value;
value=(0.04)*b;
return value;
}
void main()
{
again:
clrscr();
int n;
double X0,Y0,X,Y,h;
coutX0;
coutY0;
coutX;
cout
-
8/2/2019 Numerical Methods With c Codes
56/60
cin>>n;
if(n
-
8/2/2019 Numerical Methods With c Codes
57/60
Runge Kutta Fehlberg (RKF45) Method
To approximate a solution to the equation
y(x) = f(x, y(x))
Where x is independent variable and y is dependent on x.
With initial condition
y(x0) = y0
On an interval Ix = [x0, x], choose a small value for h > 0 and an integer n such that
x0 + nh = x. n =1,2,3,
Compute y1, y2, . . . , ym+1 using the difference equationk1= h* f(xm,ym)
k2 = h* f(xm+h/4,ym+k1/4)
k3 = h* f(xm+3*h/8,ym+3*k2/32+9*k2/32)
k4 = h* f(xm+12*h/13,ym+1932*k1/2197-7200*k2/2197+7296*k3/2197)
k5 = h* f(xm+12*h/13,ym+439*k1/216-8*k2+3680*k3/513-845*k4/4104)
k6 = h* f(xm+h/2,ym-8*k1/27+2*k2-3544*k3/2565+1859*k4/4104-11*k5/40)
ym+1 = ym + 16*k1/135+6656*k3/12825+28561*k4/56430-9*k5/50+2*k6/55).
m = 0,1,2,n-1
Then ym+1= yx is an approximation for y(x0 + nh) = y(x).
-
8/2/2019 Numerical Methods With c Codes
58/60
C++ program for RKF45 Method for y(x) = f(x, y(x)) = 0.04x, y(0) =50, Ix = [0, 50]
#include
#include
#include
#include
#include
double f(double a,double b)
{double value;
value=(0.04)*b;
return value;
}
void main()
{
again:
clrscr();
int n;
double X0,Y0,X,Y,h;
coutX0;
coutY0;
coutX;
-
8/2/2019 Numerical Methods With c Codes
59/60
coutn;
if(n
-
8/2/2019 Numerical Methods With c Codes
60/60
delay(700);
cout