write a program to find a root of non linear equation using Newton's-Raphson method in c++.
write a program to find a root of non linear equation using Newton's-Raphson method in c++.
Source Code:
/********************************************
program: solution of non-linear equation
bisection method
language: C
Author : Tanveer Khan
Rajasthan University, Jaipur
************************************************/
Source Code:
///solution of non-linear equation using Newton Raphson Method
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
float f(float x)
{
return(pow(x,4)-x-10);
}
float df(float x)
{
return(4*(pow(x,3))-1);
}
void main()
{
clrscr();
float x0,x01,x1=0,x,y;
int i=0,n;
cout<<"equation : f(x)=(x^4)-x-10=0 \n";
cout<<"if your given interval is wrong than program is terminated.";
cout<<"\nenter 2 initial approximation value = ";
cin>>x0>>x01;
cout<<"enter no. of iteration=";
cin>>n;
if(f(x0)*f(x01)>=0)
{
exit(1);
}
if(fabs(f(x0))>fabs(f(x01)))
{
y=x01;
}
else
{
y=x0;
}
do
{
if(i==n)
{
break;
}
x1=y-(f(y)/df(y));
cout<<"\n"<<i+1<<"\tx0="<<y<<"\tx"<<i<<"="<<x1;
cout<<"\tf(x"<<i<<")="<<f(x1);
cout<<"\n approximation root is "<<x1<<endl;
if(y==x1)
{
cout<<"\n the root is "<<y<<"\n";
break;
}
i++;
y=x1;
}
while(f(y)*f(x1)!=0);
getch();
}
OUTPUT:
write a program to find a root of non linear equation using Newton's-Raphson method in c++.
Source Code:
/********************************************
program: solution of non-linear equation
bisection methodlanguage: CAuthor : Tanveer KhanRajasthan University, Jaipur************************************************/Source Code:///solution of non-linear equation using Newton Raphson Method
#include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> float f(float x) { return(pow(x,4)-x-10); } float df(float x) { return(4*(pow(x,3))-1); } void main() { clrscr(); float x0,x01,x1=0,x,y; int i=0,n; cout<<"equation : f(x)=(x^4)-x-10=0 \n"; cout<<"if your given interval is wrong than program is terminated."; cout<<"\nenter 2 initial approximation value = "; cin>>x0>>x01; cout<<"enter no. of iteration="; cin>>n; if(f(x0)*f(x01)>=0) { exit(1); } if(fabs(f(x0))>fabs(f(x01))) { y=x01; } else { y=x0; } do { if(i==n) { break; } x1=y-(f(y)/df(y)); cout<<"\n"<<i+1<<"\tx0="<<y<<"\tx"<<i<<"="<<x1; cout<<"\tf(x"<<i<<")="<<f(x1); cout<<"\n approximation root is "<<x1<<endl; if(y==x1) { cout<<"\n the root is "<<y<<"\n"; break; } i++; y=x1; } while(f(y)*f(x1)!=0); getch(); }
OUTPUT:
Comments
Post a Comment