Скачиваний:
18
Добавлен:
01.05.2014
Размер:
2.9 Кб
Скачать
/*
  ” ©«: Lr_5.cpp


  Ђўв®а: “ᥭЄ® Ђ.‚.
  ђҐ¤. 1.0  07.10.06
*/

#include <iostream.h>
#include <math.h>
#include <conio.h>
#include <fstream.h>
#include "methods.h"

// Global variables
double	F(double);	// Function
double	F1(double);	// Derivative
double	F2(double x);	// Second derivative
double	delta=0.1;	// function calculating error

ofstream	f_out_1("out1.txt"),	// Output data files
		f_out_2("out2.txt"),
		f_out_3("out3.txt");


double	eps=0.1,	// output data error
	eps_theor=0;	// Theoretical eps value



void main()
{
 clrscr();

 double	func_val,	// value of function
	x,		// value of root
	_F,_F1;
 int	n;		// number of iterations

 // file opening

 if (f_out_1.fail() || f_out_2.fail() || f_out_3.fail())
 {
	cout<<"Impossible to open output file!"<<endl;
	exit(1);
 }



 f_out_1.setf(ios::fixed);
 f_out_1.setf(ios::showpoint);

 f_out_2.setf(ios::fixed);
 f_out_2.setf(ios::showpoint);
 f_out_2.precision(9);

 /* Calculating & table printing */
 delta=0.1;

 for (int j=1; j<=6; j++)
 {
	f_out_1.precision(j);
	f_out_1<<"Delta = "<<delta<<endl;

	eps=0.1;
	f_out_1<<"Eps\t\t"<<"X\t\t"<<"F(x)\t\t"<<"F'(x)\t\t"<<"N\t"<<"Eps_Theor\t"<<"Condit\n";
	for (int i=1; i<=6; i++)
	{
		x=0.9;
		f_out_1.precision(i);
		x=NEWTON(x,eps,n,f_out_2);
		_F=F(x); _F1=F1(x);
		f_out_1<<eps<<"\t"; if (i<6) {f_out_1<<"\t";};
		f_out_1<<x<<"\t";   if (i<6) {f_out_1<<"\t";};
		f_out_1.precision(j);
		f_out_1<<_F<<"\t"; if ( (j<6) && !((j==5)&&(_F<0)) ) {f_out_1<<"\t";};
		f_out_1<<_F1<<"\t"; if ( (j<6) && !((j==5)&&(_F1<0)) ) {f_out_1<<"\t";};
		f_out_1<<n<<"\t";
		eps_theor=delta/fabs(_F1);
		f_out_1.precision(6);
		f_out_1<<eps_theor<<"\t";
		if (eps>=eps_theor) {f_out_1<<"good\n";}
		else	{f_out_1<<"bad\n";}

		eps = eps/10;
	}

	f_out_1<<endl<<endl;
	delta = delta/10;
 }


 f_out_3.setf(ios::fixed);
 f_out_3.setf(ios::showpoint);
 f_out_3<<"X\t"<<"F(x)\t\tF'(x)\t\tF''(x)\n";
 for (x=0;x<1;x=x+0.01)
 {
  f_out_3.precision(2);
  f_out_3<<x<<"\t";
  f_out_3.precision(9);
  f_out_3<<F(x)<<"\t"<<F1(x)<<"\t"<<F2(x)<<endl;
 }


 f_out_1.close();
 f_out_2.close();
 f_out_3.close();

 cout<<"Program finished. Press <Enter>...";
 getch();
 return;
}

// Function definition
double	F(double x)
{
	extern double delta;
	double s;

	s = asin(2*x/(1+x*x))-exp(-x*x);
	return Round(s,delta);
}

// Derivative definition
double	F1(double x)
{
	double	s;

	s=(2-4*x*x/(1+x*x))/((1+x*x)*sqrt(1-4*x*x/pow((1+x*x),2))) + 2*x*exp(-x*x);
	return Round(s,delta);
}

// Secondderivative definition
double	F2(double x)
{
	double	s;

	s=2*exp(-x*x)*(1-2*x*x)
	  +( (4*x)/pow((1+x*x),2) )*( 4*x*x/(1+x*x)-3)/sqrt(1-4*x*x/pow((1+x*x),2) )
	  -( 1/pow((1+x*x),3) )*( 16*x*x*x/(1+x*x)-8*x )*( 1-2*x*x/(1+x*x) )/( pow((1-4*x*x/pow((1+x*x),2)),1.5) );

	return s;
}
Соседние файлы в папке lr_5