Anda di halaman 1dari 1

CORNELIUS CSAR JUDE H.

SALINAS 083342 BS APs-ACS CE 30 Section A Newtons Method


#include<iostream> #include<math.h> #include<iomanip> using namespace std; double f(double x){ return (sin(x) - x*x +3*x -2); } double df(double x){ return (cos(x) - 2*x +3); } double newton(double intGuess, double (*f)(double), double (*deriv)(double), int sigFigs){ double x_prev = intGuess; double x_next; do{ x_next = x_prev - f(x_prev)/deriv(x_prev); if(fabs(x_next-x_prev)<exp(-sigFigs)) {break;} x_prev=x_next; }while(f(x_next)!=0.0); return x_next; } int main(){ double guess; while(1){ system("cls"); cout<< "Solve for the roots of f(x) = sin(x) - x*x +3*x -2."<< endl; cout<< "Enter initial guess: "<< endl; cin>> guess; system("cls"); double answer = newton(guess, f, df, 8); cout<< "Solve for the roots of f(x) = sin(x) - x*x +3*x -2."<< endl; cout<< endl; cout<< setprecision(8); cout<< "The computed root given the initial guess, "<< guess<< ", is "<< answer<< "."<< endl; system("pause"); } system("pause"); return 0; }

Anda mungkin juga menyukai