Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <set>
- #include <string>
- #include <sstream>
- #include <queue>
- #include <iterator>
- #include <iostream>
- using namespace std;
- int n=1;
- // x^3 + 3x- 5;
- float functionValue(float x) {
- float res;
- float exp;
- exp = pow(x, 3);
- res = (exp + (3*x*1.0)) - 5;
- return res;
- }
- int main() {
- // freopen("in.txt", "r", stdin);
- // freopen("out.txt", "w", stdout);
- float x1, x2, x0, f0, f1, f2, Estop = 0.01;
- x1 = 1, x2 = 2;
- f1 = functionValue(x1);
- f2 = functionValue(x2);
- if(f1*f2 > 0) {
- cout << "X1 and X2 do not bracket any root!" << endl;
- goto stop;
- } else {
- step4:
- x0 = (x1+x2) /2;
- f0 =functionValue(x0);
- }
- if(f1*f0 < 0) {
- x2 = x0;
- } else {
- x1 = x0;
- f1 = f0;
- }
- if((x2-x1)/x2 < Estop) {
- x0 = (x1 + x2) / 2;
- cout << "Root: " << x0 << endl;
- goto stop;
- } else {
- cout << "Iteration " << n++ <<": " << x0 << endl;
- goto step4;
- }
- stop:
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement