#include "stdafx.h"
#include <math.h>
#include <iostream>
using namespace std;
double f (double x){
return sin(5*x)*exp(-x)+pow(x,3);
}
double df (double x){
return 3*pow(x,2) + 5*exp(-x)*cos(5*x)-exp(-x)*sin(5*x);
}
double d2f (double x){
return 6*x-10*exp(-x)*cos(5*x)-24*exp(-x)*sin(5*x);
}
double min(double a,double b){
double m1,r;
int n1=64;
m1=fabs(df(a));
double h1=(b-a)/n1;
for(int e=1;e<=n1;e++){
r=fabs(df(a-e*h1));
if(r<m1) m1=r;
}
return m1;
}
double max(double a,double b){
double M1,r;
int n1=64;
M1=fabs(df(a));
double h1=(b-a)/n1;
for(int e=1;e<=n1;e++){
r=fabs(df(a-e*h1));
if(r>M1) M1=r;
}
return M1;
}
void hordi(double x0, double c, double m1, double M1){
int k=1;
double fc=f(c);
double fx0=f(x0);
double x;
double epsilon=pow(10,-6);
x=x0-((c-x0)/(fc-fx0))*fx0;
while((((M1-m1)/m1)*fabs(x-x0))>=epsilon){
k++;
x0=x;
fx0=f(x0);
x=x0-((c-x0)/(fc-fx0))*fx0;
}
cout<<" x="<<x<<" f(x)="<<f(x)<<" k="<<k<<endl;
}
void main(){
double a=-5;
double b=1;
int l=1;
int j=1;
int i;
double A, B, c, x0;
double n=64;
double h=(b-a)/n;
for(i=1;i<=n;i++){
A=a+(i-1)*h;
B=a+i*h;
if(f(A)*d2f(A)<0){
x0=A;
c=B;
}else{
x0=B;
c=A;
}
cout<<"Root hordi "<<j;
hordi(x0,c,min(A,B),max(A,B));
j++;
}
}