Newton Raphson Method C++ Program

#include<iostream>
#include<cmath>
using namespace std;
double newt(double, double);

int main(){
	double guess1=10, guess2=-10, accuracy=0.1;
	double result1=newt(guess1,accuracy);
	double result2=newt(guess2,accuracy);
		
	cout<<"The roots are"<<'\t'<<result1<<","<<'\t'<<result2<<endl;
	return 0;
}
	
double newt(double xn, double accu){
	double temp;
	double f=(6*xn*xn)-(17*xn)-14;
	double fdash=(12*xn)-17;
	double xnplus1=xn-(f/fdash);
	temp=xn;
	xn=xnplus1;
	
	if(abs(temp-xn)>=accu){
		return newt(xn, accu);
		
	}
	return xn;
		
}

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.



Leave a Reply

Your email address will not be published. Required fields are marked *