Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <cmath>
- #include <string>
- using namespace std;
- double funcval(double, double[], int );
- double derival(double, double[], int);
- int main (){
- //vector<double> coef;
- int coefNum;
- ifstream file("polynomial.txt");
- if (!file){
- return 0;
- }
- string useless;
- getline(file, useless);
- file >> coefNum;
- double poly[coefNum + 1];
- while (file.good()){
- int d;
- file >> d;
- file >> poly[d];
- cout << poly[d] << " ";
- }
- cout << endl;
- for (int i = 0; i<=coefNum; i++){
- cout << poly[i] << " ";
- }
- file.close();
- double xval;
- cout<<"Enter an initial estimate for the root. ";
- cin>>xval;
- double h=.002;
- int dir = -1;
- while (h > .000002){
- if (derival(xval + h * dir, poly, coefNum) * dir < 0){
- xval += h;
- cout << xval << endl;
- } else {
- dir *= -1;
- h /= 10;
- }
- //cout << h << endl;
- }
- cout << funcval(xval, poly, coefNum);
- return 0;
- }
- double funcval(double xval, double coeff[], int deg){
- double y = 0;
- for(int i = 0; i <= deg; i++){
- y = y + coeff[i] * pow(xval, i);
- }
- return y;
- }
- double derival(double xval, double coeff[], int deg){
- double dy = 0;
- for (int i = 1; i <= deg; i++){
- dy = dy + coeff[i] * pow(xval, i - 1);
- }
- return dy;
- }
Add Comment
Please, Sign In to add comment