numerical methods and program in c++
Transcript of numerical methods and program in c++
Newton-Raphson Root Finding Method
Algorithm:
xi+1 = xi - f(xi)/f'(xi)
Example: find the roots of f(x) = 2x2 -x -21
% cat newt3.cpp// Newton-Raphson method for finding the // roots of f(x) = 0 for a polynomial function#include <iostream>using namespace std;#include <cmath>
double newton(double a, double b, double c, double x0, double crit);
int main(){ double a, b, c, root, guess, conv_crit; cout << "Enter values for a, b, and c :"; cin >> a >> b >> c;
cout << "Enter a guess for the root :"; cin >> guess; cout << "Enter the convergence criterion: "; cin >> conv_crit; // call function newton
root = newton(a, b, c, guess, conv_crit);
// display result cout.setf(ios::fixed | ios::showpoint); cout.precision(4);
cout << "One root is " << root << endl;
return 0;
}
double newton(double a, double b, double c, double x0, double crit){ double x1, delta; int iters = 0;
// use a do-while loop do
{ x1 = x0 - (a*x0*x0 + b*x0 + c) / ( 2*a*x0 + b);
delta = fabs ( x1 - x0 );
x0 = x1; // new approximation becomes the old // approximation for the next iteration
iters++; // count the number of iterations
} while (delta > crit && iters <= 30); cout << iters << " iterations" << endl;
return x1;}
% a.outEnter values for a, b, and c :2 -1 -21Enter a guess for the root :10Enter the convergence criterion: .00015 iterationsOne root is 3.5000% a.outEnter values for a, b, and c :2 -1 -21Enter a guess for the root :-9Enter the convergence criterion: .00015 iterationsOne root is -3.0000
Code for GAUSS SEIDEL METHOD in C Programming
#include<stdio.h>#include<conio.h>#include<math.h>#define ESP 0.0001#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)
void main(){ double x1=0,x2=0,x3=0,y1,y2,y3; int i=0; clrscr(); printf("\n__________________________________________\n"); printf("\n x1\t\t x2\t\t x3\n"); printf("\n__________________________________________\n"); printf("\n%f\t%f\t%f",x1,x2,x3); do {
y1=X1(x2,x3); y2=X2(y1,x3); y3=X3(y1,y2); if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP ) { printf("\n__________________________________________\n"); printf("\n\nx1 = %.3lf",y1); printf("\n\nx2 = %.3lf",y2); printf("\n\nx3 = %.3lf",y3); i = 1; } else { x1 = y1; x2 = y2; x3 = y3; printf("\n%f\t%f\t%f",x1,x2,x3); } }while(i != 1);getch();}
/*
OUT PUT ___________
__________________________________________
x1 x2 x3
__________________________________________
0.000000 0.000000 0.0000000.850000 -1.027500 1.0108751.978588 -1.146244 0.8802052.084265 -1.168629 0.8662792.105257 -1.172475 0.8636032.108835 -1.173145 0.8631452.109460 -1.173262 0.8630652.109568 -1.173282 0.863051__________________________________________
x1 = 2.110
x2 = -1.173
x3 = 0.863
*/
Code for SECANT METHOD in C Programming
#include<stdio.h>#include<conio.h>#include<math.h>#define ESP 0.0001
#define F(x) (x)*(x) - 4*(x) - 10void main(){ float x1,x2,x3,f1,f2,t; clrscr(); printf("\nEnter the value of x1: "); scanf("%f",&x1); printf("\nEnter the value of x2: "); scanf("%f",&x2); printf("\n______________________________________________\n"); printf("\n x1\t x2\t x3\t f(x1)\t f(x2)"); printf("\n______________________________________________\n"); do { f1=F(x1); f2=F(x2); x3=x2-((f2*(x2-x1))/(f2-f1)); printf("\n%f %f %f %f %f",x1,x2,x3,f1,f2); x1=x2; x2=x3; if(f2<0) t=fabs(f2); else t=f2; }while(t>ESP);printf("\n______________________________________________\n");printf("\n\nApp.root = %f",x3);getch();}
/* OUT PUT---------
Enter the value of x1: 4
Enter the value of x2: 2
___________________________________________________________
x1 x2 x3 f(x1) f(x2)___________________________________________________________
4.000000 2.000000 9.000000 -10.000000 -14.0000002.000000 9.000000 4.000000 -14.000000 35.0000009.000000 4.000000 5.111111 35.000000 -10.0000004.000000 5.111111 5.956522 -10.000000 -4.3209875.111111 5.956522 5.722488 -4.320987 1.6540635.956522 5.722488 5.741121 1.654063 -0.1430845.722488 5.741121 5.741659 -0.143084 -0.0040155.741121 5.741659 5.741657 -0.004015 0.000010___________________________________________________________
App.root = 5.741657
*/
Code for TRAPEZOIDAL RULE in C Programming
#include<stdio.h>#include<conio.h>#include<math.h>void main(){ float x[10],y[10],sum=0,h,temp; int i,n,j,k=0; float fact(int); clrscr(); printf("\nhow many record you will be enter: "); scanf("%d",&n); for(i=0; i<n; i++) { printf("\n\nenter the value of x%d: ",i); scanf("%f",&x[i]); printf("\n\nenter the value of f(x%d): ",i); scanf("%f",&y[i]); } h=x[1]-x[0]; n=n-1; for(i=0;i<n;i++) { if(k==0) { sum = sum + y[i]; k=1; } else sum = sum + 2 * y[i]; } sum = sum + y[i]; sum = sum * (h/2); printf("\n\n I = %f ",sum);getch();}
/*______________________________________
OUT PUT______________________________________
how many record you will be enter: 6
enter the value of x0: 7.47
enter the value of f(x0): 1.93
enter the value of x1: 7.48
enter the value of f(x1): 1.95
enter the value of x2: 7.49
enter the value of f(x2): 1.98
enter the value of x3: 7.50
enter the value of f(x3): 2.01
enter the value of x4: 7.51
enter the value of f(x4): 2.03
enter the value of x5: 7.52
enter the value of f(x5): 2.06
I = 0.099652
*/
Code for SIMPSON'S 1/3 RULE in C Programming
#include<stdio.h>#include<conio.h>#include<math.h>void main(){ float x[10],y[10],sum=0,h,temp; int i,n,j,k=0; float fact(int); clrscr(); printf("\nhow many record you will be enter: "); scanf("%d",&n); for(i=0; i<n; i++) { printf("\n\nenter the value of x%d: ",i); scanf("%f",&x[i]); printf("\n\nenter the value of f(x%d): ",i); scanf("%f",&y[i]); } h=x[1]-x[0]; n=n-1; sum = sum + y[0]; for(i=1;i<n;i++) { if(k==0) { sum = sum + 4 * y[i]; k=1; }
else { sum = sum + 2 * y[i]; k=0; } } sum = sum + y[i]; sum = sum * (h/3); printf("\n\n I = %f ",sum);getch();}
/*______________________________________
OUT PUT______________________________________
how many record you will be enter: 5
enter the value of x0: 0
enter the value of f(x0): 1
enter the value of x1: 0.25
enter the value of f(x1): 0.8
enter the value of x2: 0.5
enter the value of f(x2): 0.6667
enter the value of x3: 0.75
enter the value of f(x3): 0.5714
enter the value of x4: 1
enter the value of f(x4): 0.5
I = 0.693250
*/
Sin x SERies#include<iostream.h>#include<conio.h>
int main(){
int i,j,n,fact,sign=-1;float x, p=1,sum=0;cout<<"Enter the value of x : ";cin>>x;cout<<"Enter the value of n : ";cin>>n;
for(i=1;i<=n;i+=2){
fact=1;for(j=1;j<=i;j++){
p=p*x;fact=fact*j;
}sign=-1*sign;sum+=sign*p/fact;
}cout<<"sin "<<x<<"="<<sum;
getch();return 0;
}
EVALUATION OF COSX SERIES - C PROGRAMProgram:
#include<stdio.h>
//#include<conio.h>
void cosx(float x,int n)
{
int i;
float term=1,sum=1;
for(i=1;i<=n;i++)
{
term=((-term)*(x*x))/((2*i)*(2*i-1));
sum=sum+term;
}
printf("Sum of the cos series=%f\n",sum);
}
void main()
{
int n,y;
float x;//clrscr();
printf("Enter the no of terms:");
scanf("%d",&n);
printf("enter the value in degree:");
scanf("%f",&x);
y=x;
x=x*(3.14/180);
cosx(x,n);
// getch();
}
Output:nn@linuxmint ~ $ gcc c12.c
nn@linuxmint ~ $ ./a.out
Enter the no of terms:10
enter the value in degree:0
Sum of the cos series=1.000000
nn@linuxmint ~ $ ./a.out
Enter the no of terms:0
enter the value in degree:0
Sum of the cos series=1.000000
nn@linuxmint ~ $ ./a.out
Enter the no of terms:10
enter the value in degree:60
Sum of the cos series=0.500460
nn@linuxmint ~ $
EVALUATION OF SINX SERIES - C PROGRAMProgram:
#include<stdio.h>
//#include<conio.h>
void sinx(float x,int n)
{
int i;
float term=x,sum=x;
for(i=1;i<=n;i++)
{
term=((-term)*(x*x))/((2*i)*(2*i+1));
sum=sum+term;
}
printf("Sum of the sin series=%f\n",sum);
}
void main()
{
int n,y;
float x;//clrscr();
printf("enter the limit:");
scanf("%d",&n);
printf("enter the value in degree:");
scanf("%f",&x);
y=x;
x=x*(3.14/180);
sinx(x,n);
//getch();
}
Output:nn@linuxmint ~ $ gcc c11.c
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:90
Sum of the sin series=1.000000
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:0
Sum of the sin series=0.000000
nn@linuxmint ~ $ ./a.out
enter the limit:10
enter the value in degree:30
Sum of the sin series=0.499770
nn@linuxmint ~ $
Write a C program to find the exponential series of 1+x+x2/2!+x3/3!+.......+xn/n!
#include< math.h>void main( ){int x, n, fact, i, j;float sum=1;clrscr( );printf("Enter the 'x' value:");scanf("%d",&x);printf("\nEnter the 'n' value:");scanf("%d",&n);for(i=1; i< =n ; i++){fact=1;for( j=i ; j >=1; j--)fact=fact*j;
sum=sum+(pow(x,i )/ fact);}printf("\nSum of the series : %f ",sum);getch( );}