Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

C++ Code

This document provides code examples for numerical integration and solving differential equations in C++. It includes: 1. Examples of using approximating integration formulas and convergence parameters to calculate integrals numerically. 2. Examples of using the Bisection Method and Newton-Raphson Method to find roots of nonlinear equations. 3. Examples of using the Euler Method and 4th-Order Runge-Kutta Method to find approximate solutions to differential equations.

Uploaded by

Gailan Gardi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

C++ Code

This document provides code examples for numerical integration and solving differential equations in C++. It includes: 1. Examples of using approximating integration formulas and convergence parameters to calculate integrals numerically. 2. Examples of using the Bisection Method and Newton-Raphson Method to find roots of nonlinear equations. 3. Examples of using the Euler Method and 4th-Order Runge-Kutta Method to find approximate solutions to differential equations.

Uploaded by

Gailan Gardi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Gailan Fareeq Rafeeq

Codes of C++ &Numerical Methods


For integral:
1. Approximates integration formulas
2. Approximate integration formulas-based convergence parameter
For Nonlinear:
1. The Bisection Method
2. Newton Raphson method
For differential:
1. Euler Method
2. Fourth –Order Runge-Kutta Method
Integral
1. Approximates integration formula
Example [1-1]: Write a program to find the value of area under curve y=x/(1+x2)
Based on the width of the strip
//calculate area under the curve x/(1+x2) //base on strip width.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{float x1,x2;double dx,area(0),y ;int n=0;
cout<<"enter the value of x1 & x2 : " ; cin >> x1>>x2;
cout<<"enter the vlaue of dx : " ; cin >> dx;
while(x1<x2)
{ y=x1/(1+pow(x1,2));
area = area + (y * dx);
n=n+1 ;
x1=x1+dx;
}
cout <<"value of integration =
"<<area<<endl ;
cout<<"no of iterations = "<<n<<endl; return 0;}
enter the value of x1 & x2 : 0 1 enter the vlaue of dx : 0.0001 value of integration = 0.346547
no of iterations = 10000 Press any key to continue
2. Approximate integration formulas-based convergence parameter
Example 1-2: Write a program to find the value of area under curve y=x/(1+x2) based on
the convergence parameter

// Example1-2 find the value of area under curve y=x/(1+x2) based on the convergence
parameter
#include <iostream>
#include <cmath>
using namespace std;
float rectangle (float a, float b,int n) //function
{
float h, x, area(0); h=(b-a)/(n+1);
x=a;
for (int i=1; i<=n;i++)
{area =area +f(x);
x=x+h; }
return (area * h);
}
float f(float x) //function for finding y
{return (x/(1.0+x*x));} int main()
{
int n0, n;
float a,b,eps, rc, rn;
cout <<"a,b,n0,eps=?\n";
cin>> a>>b>>n0>>eps;
cout <<"a ="<<a<<"b = "<<b<<endl;
cout <<"n0= " << n0 << "eps = "<<eps <<endl; rc=rectangle(a,b,n0);
n=2*n0;
rn=rectangle (a,b,n);
while (fabs(rn-rc)>eps)
{n=n+n;
rc=rn;
rn=rectangle (a,b,n);
cout <<"integral= "<<rn<<"for n=" <<n<<endl; }
return 0;}

a,b,n0,eps=?
0 1 2 0.0001 a =0b = 1 n0= 2 eps = 0.0001
integral= 0.262593 for n=8
integral= 0.302221 for n=16
integral= 0.323777 for n=32
integral= 0.335016 for n=64
integral= 0.340755 for n=128
integral= 0.343654 for n=256
integral= 0.345111 for n=512
integral= 0.345842 for n=1024
integral= 0.346208 for n=2048
integral= 0.34639 for n=4096
integral= 0.34649 for n=8192
Press any key to continue
Nonlinear
1. The Bisection Method
Example 2-1 Solve the equation sin x − x + 0.5 = 0 and accurate to 4 decimal places by the
bisection method.
// Example-12-1 to find the value of function sin (x) - x +0.5 using bisect method
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
float f (float x)
{ return sin (x) - x +0.5 ; }//function
int main(){
int kmax;
float a,b,eps;
cout <<"input a,b,eps,kmax\n";
cin>> a>>b>>eps>>kmax; //kmax =max number of iterations cout <<"the result are\n";
cout <<"k a b x f(x)\n";
int k=1;
float x=0.5*(a+b); //next bisection
while((k<=kmax)&&((b-a)>eps))
{
cout<<k<<setprecision(5)<<setw(12)<<x<<setw(15)<<f(x)<<setprecision(5 )
<<setw(12)<<a<<setw(12)<<b<<endl;//print k,x,y,a,b
if (f(x)>0) a=x; //because f(b)<0
else b=x;
x=0.5*(a+b);
k++;}
if(k>kmax)cout <<"no convergence";
return 0; }
2. Newton Raphson method
Find a real root of the equation x3 – x – 1 = 0 using Newton - Raphson method, correct to
four decimal places.
// Example 2-3 to find the value of function cos(x)=x //using Newton raphson method
#include<iostream>
#include<cmath>
using namespace std;
int main()
{double x;
cout<<"Give initial guess"<< endl;
cin>>x;
double err,tol=1e-5,x1;
int k=1,kmax=100;
err=tol+1;
while(err>tol&&k<kmax)
{x1=x-(x-cos(x))/(1+sin(x));
err=fabs(x1-x);
x=x1;
k++;}
if(err<=tol)
cout<<"The root is"<<x<<endl;
else
cout<<"Error, no convergence\n";
return 0; }

Give initial guess


1
The root is 0.739085
Press any key to continue
For differential:
1. Euler Method
Example 3.2: Find the approximate solution to a differential equation with the Euler
method for a function dy/dx=x+y, if you know y(0)=1, h=0.2 and for n=5.
// Example3-2- to find the approximate solution to a differential equation with the Euler
method for a function
#include<iostream>
#include<iomanip>
using namespace std;
float f(float x ,float y) //function
{return x+y;}
int main ()
{float a,h,x[100],y[100];
int i,n;
cout << " enter value of a,n,h,y[0](n must be <100)\n";
cin >>a >>n>>h>>y[0];
x[0] = a;
for (i= 0;i<n ;i++)
{ //this is euler loop
y[i+1] =y[i] + h*f(x[i],y[i]);
x[i+1] =x[i] + h;
}
cout<<setw(14)<<"x"<<setw(14)<<"y"<<"\n";
for(i=0;i<=n;i++)
cout<<setprecision(5)<<setw(14)<<x[i]<<setw(14)<<y[i]<<endl; return 0;}
2. Fourth –Order Runge-Kutta Method
Example 3-4: Apply Runge-Kutta equation to find dy/dx=x+y, y(0)=1 for dx
interval=0.02 ut to x=0.1 Solution : Solve the above equation using C++ source code
n=(0.1-0)/0.02=5
// Example13-4-Program to solve the first order Differential using Runge-Kutta rule for
y'=x+y
#include<iostream>
#include<iomanip>
using namespace std;
float f(float x ,float y) //function
{return x+y;}
int main ()
{float h,x[100],y[100],k1,k2,k3,k4;
int i,n;
cout << "value of x[0],n,h,y[0] are (n must be <100)\n"; cin >>x[0]>>n>>h>>y[0];
for (i= 0;i<n ;i++)
{
k1=h* f(x[i],y[i]);
k2=h*f(x[i]+h/2,y[i]+k1/2);
k3=h*f(x[i]+h/2,y[i]+k2/2);
k4=h*f(x[i]+h,y[i]+k3);
y[i+1] =y[i] +(k1+2*k2+2*k3+k4)/6; // value of y
x[i+1] =x[i] + h;
}
cout<<setw(14)<<"x"<<setw(14)<<"y"<<"\n";
for(i=0;i<=n;i++)
cout<<setprecision(5)<<setw(14)<<x[i]<<setw(14)<<y[i]<<endl; return 0;}

You might also like