Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- double a,b,c,eps,c1, e, x, m, M,x1,y;
- int i,n,k;
- bool u;
- double f (double x) {
- return pow(x,4)-2*pow(x,2)-3;
- }
- double f1(double x) {
- return 4*pow(x,3)-4*x;
- }
- void bis_apriori(double a,double b){
- n=trunc(log((b-a)/eps)/log(2))+1; u=true;
- for(int i=1;i<=n;i++){
- c=(a+b)/2;
- if(f(c)==0) {
- cout << " c : " << fixed << setprecision(15) << c << " f(c) : " << f(c) << fixed << setprecision(15) << " n : " << n << endl;
- u=false; break;}
- else{ if(f(a)*f(c)>0){
- a=c;}
- else{
- b=c;
- }
- }
- }
- if(u) cout << " c -> " << fixed << setprecision(15) << c << " f(c) -> " << f(c) << fixed << setprecision(15) << " n -> " << n << endl;
- }
- void bis_aposteriori(double a,double b){
- c=a; u=true;
- do{ c1=c; k++;
- c=(a+b)/2;
- if(f(c)==0) {
- cout << " c -> " << fixed << setprecision(15) << c << " f(c) -> " << f(c) << fixed << setprecision(15) << " n -> " << k << endl;
- u=false; break;}
- else{ if(f(a)*f(c)>0){
- a=c;}
- else{
- b=c;
- }
- }
- }while(2*abs(c-c1)>eps);
- if(u) cout << " c -> " << fixed << setprecision(15) << c << " f(c) -> " << f(c) << fixed << setprecision(15) << " n -> " << k << endl;
- }
- void secanta(double a, double b, double eps, double m1, double m2) {
- c=a-(f(a)/(f(b)-f(a)))*(b-a);
- if (f(c)*f(a)>0){ e=b;x=a;
- }
- else{
- e=a;x=b;
- }
- int k2 = 0;
- do {
- x1=x;
- k2++;
- x = x1 - (f(x1) * (e - x1)) / (f(e) - f(x1));
- } while ((((m1-m2) * abs(x - x1)) / m2) > eps);
- cout << " c -> " << fixed << setprecision(15) << x << " f(c) -> " << f(x) << fixed << setprecision(15) << " n -> " << k2 << endl;
- }
- void coardaapos(double a, double b, double eps, double m1, double m2) {
- c = a;
- u = true;
- int k4 = 0;
- do {
- c1 = c;
- k4++;
- c = (a * f(b) - b * f(a)) / (f(b) - f(a));
- if (f(c) == 0) {
- cout << " c : " << fixed << setprecision(15) << c << " f(c) : " << f(c) << fixed << setprecision(15) << " n : " << k4 << endl;
- u = false;
- break;
- } else {
- if (f(c)*f(a) > 0) {
- a = c;
- } else {
- b = c;
- }
- }
- } while (((m1 * abs(c - c1)) / m2) > eps);
- if (u) {
- cout << " c : " << fixed << setprecision(15) << c << " f(c) : " << f(c) << fixed << setprecision(15) << " n : " << k4 << endl;
- }
- }
- void coardaapriori(double x1, double x2) {
- int k1 = trunc(log(eps/0.5) / (log(f1(x2) - f1(x1)) - log(f1(x2)))) + 1;
- c = a;
- for (int i = 1; i <= k1; i++) {
- c = c - (f(c) * (x2 - c)) / (f(x2) - f(c));
- }
- cout << " c : " << fixed << setprecision(15) << c << " f(c) : " << fixed << setprecision(15) << f(c) << " n : " << k1 << endl;
- }
- void tang(double a, double b, double eps, double m1, double m2) {
- int k5=0;double x,x1,c=0;
- c = (a * f(b) - b * f(a))/(f(b)-f(a));
- if (f(a)*f(c)>0) { x = b;
- } else { x = a;
- }
- do {
- x1=x;
- x = x1 - f(x1)/f1(x1);
- k5++;
- }while(((m1*pow((x-x1),2))/(2*m2))>=eps);
- cout << " c -> " << fixed << setprecision(15) << x << " f(c) -> " << fixed << setprecision(15) << f(x) << " n -> " << k5 << endl;
- }
- void mixt(double a,double b,double eps){
- int k6 = 0;double h=0; double x,y;
- h = (a * f(b) - b * f(a))/(f(b)-f(a));
- if (f(a)*f(h)>0) { x = b;
- y = a; } else { x = a;
- y = b; }
- do {
- y = y - (f(y) * (x-y))/(f(x)-f(y));
- x = x - f(x)/f1(x);
- k6++;
- }
- while (abs(x-y) >= eps);
- cout << " c -> " << fixed << setprecision(15) << x << " f(c) -> " << fixed << setprecision(15) << f(x) << " n -> " << k6 << endl;
- }
- int main(){
- a=1; b=2;
- m=abs(f1(a));
- M=abs(f1(b));
- cout<<"a -> "<<a<<" "<<" b -> "<<b<<endl;
- cout<<"M -> "<<M<<" "<<" m -> "<<m<<endl;
- m=pow(10,-15);
- cout<<"bisectia apriori"<<endl;
- eps=pow(10,-15);
- bisapriori(a,b);
- cout<<"bisectia aposteriori"<<endl;
- bisaposteriori(a,b);
- cout<<"secanta aposteriori"<<endl;
- secanta(a,b,eps,M,m);
- cout<<"coarde aposteriori"<<endl;
- coardaapos(a,b,eps,M,m);
- cout<<"coarde apriori"<<endl;
- eps=pow(10,-15);
- cout<<"nu putem aplica metoda f'(a)=0,ceea ce nu satisface conditiile metodei"<<endl;
- cout<<"dar daca a=1.5,putem aplica metoda(f e convexa,f e strict crescatoare,f'(a)>0,f e derivabila)"<<endl;
- coardaapriori(1.5,b);
- cout<<"tangenta aposteriori"<<endl;
- tang(1.5,b,eps,44,7.5);
- cout<<"mixta"<<endl;
- mixt(1.5,b,eps);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement