Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- double f(double x)
- {
- double zwracana;
- zwracana = x*x*x - 2*x*x - 4*x - 7;
- return zwracana;
- }
- double fprim(double x)
- {
- double zwracana;
- zwracana = 3*x*x - 4*x - 4;
- return zwracana;
- }
- double mod(double x)
- {
- if(x<=0)return -1*x;
- else return x;
- }
- double Bisekcja(double a, double b, double eps)
- {
- double xi;
- if(mod(f(a))<=eps) return a;
- if (mod(f(b))<=eps) return b;
- //cout<<f(a)<<endl;
- //cout<<f(b)<<endl;
- if (f(a)*f(b)>0) cout<<"blad";
- else
- {
- while (1){
- xi= (a+b)/2;
- if(mod(f(xi))<=eps)
- {
- return xi;
- }
- else
- {
- if(f(a)*f(xi)<=eps)
- {
- b=xi;
- }
- else a=xi;
- if (b-a<=eps)return xi;
- }
- }
- }
- }
- double newton (double x, double eps)
- {
- //ograniczenie na liczbe iteracji;
- for (int i=0 ; i<100 ; i++)
- {
- if(mod(f(x))<eps)
- {
- return x;
- }else
- x -= f(x)/fprim(x);
- }
- cout<<"blad"<<endl;
- }
- int main()
- {
- //cout << "Hello world!" << endl;
- cout<<Bisekcja(3,4,0.001)<<endl;
- cout<<newton(4,0.001);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement