Вы находитесь на странице: 1из 12

GUJARAT TECHNOLOGICAL UNIVERSITY

SARVAJANIK COLLEGE OF ENGINEERING


&TECHNOLOGY, SURAT

Academic year (2018-19)

SUBJECT: CHEMICAL ENGINEERING MATHEMATICS

TOPIC: PROGRAMMING OF NEWTON RAPHSON METHOD

SUBMITTED BY: ADAJANIA BENISHA - 170420105001


BHAVSAR KHUSHBU - 170420105002
MURKUTE SANIKA - 170420105021

SUBMITTED TO: Prof. ANAND UPADHYAY


Prof. ASHISH PARMAR

B.E. II, SEMESTER IV

(CHEMICAL ENGINEERING DEPARTMENT, SCET)


CONTENTS

1. C PROGRAM FOR NEWTON RAPHSON

2. C++ PROGRAM FOR NEWTON RAPHSON

3. SCILAB PROGRAM FOR NEWTON RAPHSON METHOD

4. PROGRAMMING IN EXCEL FOR NEWTON RAPHSON


C PROGRAM NEWTON RAPHSON

/******************************
****NEWTON RAPHSON METHOD*****/
#include<stdio.h>
#include<math.h>

/*Function whose root is to be determined*/


double f(double x){
return x*x*x-27;
}

/*Derivative of the function whose root is to be determined*/


double df(double x){
return 3*x*x;
}

/*Function that returns the root from Newton-Raphson Method*/


double rootNR(double f(double x),double df(double x),double x1,double
eps,double maxSteps){
double x;
int i=1;
do{
x=x1;
if(fabs(df(x))>=0.000000001){
x1=x-f(x)/df(x);
i++;
}
}while(fabs(x-x1)>=eps&&i<=maxSteps);
return x1;
}

/*Newton-Raphson Method Function that tabulates the values at each iteration*/


double printNR(double f(double x),double df(double x),double x1,double
eps,double maxSteps){
double x;
int iter=1;

printf("__________________________________________________________
_________________________________________\n");
printf("iter\tx\t\tf(x)\t\tf'(x)\t\tx1\t\t|x-x1|\t\tf(x1)\n");

printf("__________________________________________________________
_________________________________________\n");
do{
x=x1;
if(fabs(df(x))>=0.000000001){
x1=x-f(x)/df(x);
printf("%d.\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",iter,x,f(x),df(x),x1,fabs(x-
x1),f(x1));
iter++;
}
}while(fabs(x-x1)>=eps&&iter<=maxSteps);
return x1;
}
main(){
double x,eps,x1;
int maxSteps;
printf("Enter the initial guess:\n");
scanf("%lf",&x);
printf("Enter the desired accuracy:\n");
scanf("%lf",&eps);
printf("Enter the max. number of steps:\n");
scanf("%d",&maxSteps);

printf("__________________________________________________________
________________________________________\n\nOne of the roots of the
given equation is:\n\n%lf\n\n\n",printNR(f,df,x,eps,maxSteps));

}
OUTPUT
C++ PROGRAM FOR NEWTON RAPHSON

//Newton-Raphson Method
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double f(double x); //declare the function for the given equation
double f(double x) //define the function here, ie give the equation
{
double a=pow(x,3.0)-x-11.0; //write the equation whose roots are to be
determined
return a;
}
double fprime(double x);
double fprime(double x)
{
double b=3*pow(x,2.0)-1.0; //write the first derivative of the equation
return b;
}
int main()
{
double x,x1,e,fx,fx1;
cout.precision(4); //set the precision
cout.setf(ios::fixed);
cout<<"Enter the initial guess\n"; //take an intial guess
cin>>x1;
cout<<"Enter desired accuracy\n"; //take the desired accuracy
cin>>e;
fx=f(x);
fx1=fprime(x);
cout <<"x{i}"<<" "<<"x{i+1}"<<" "<<"|x{i+1}-x{i}|"<<endl;

do
{
x=x1; /*make x equal to the last calculated value of
x1*/
fx=f(x); //simplifying f(x)to fx
fx1=fprime(x); //simplifying fprime(x) to fx1
x1=x-(fx/fx1); /*calculate x{1} from x, fx and fx1*/
cout<<x<<" "<<x1<<" "<<abs(x1-x)<<endl;
}while (fabs(x1-x)>=e); /*if |x{i+1}-x{i}| remains greater than the
desired accuracy, continue the loop*/
cout<<"The root of the equation is "<<x1<<endl;
return 0;
}
OUTPUT
SCILAB PROGRAM FOR NEWTON RAPHSON

//Newton Raphson Method

deff('y=f(x)','y=x^3-100')
deff('y=z(x)','y=3*x^2')
a=input("Enter value of interval a:")
b=input("Enter value of interval b:")
n=input("Enter the number of iteration n:")
x0=(a+b)/2
for i=1:n
disp([i,x0])
x1=x0-f(x0)/z(x0)
if abs(x1-x0)<0.00001 then
disp("We get required accuracy")
break;
end
x0=x1
end
OUTPUT
PROGRAMMING IN EXCEL FOR NEWTON RAPHSON

Вам также может понравиться