Главная | Обратная связь
МегаЛекции

Программы для решения нелинейных уравнений





 

Текст заголовочного файла methods.h.

/*******************************************************************/

extern double F(double);

/******************************************************************/

/* Ôóíêöèÿ F (X) , çàäàâàåìàÿ ïîëüçîâàòåëåì */

/******************************************************************/

 

#ifdef __NEWTON

extern double F1(double);

/******************************************************************/

/* Ïðîèçâîäíàÿ ôóíêöèè F (X) , çàäàâàåìàÿ ïîëüçîâàòåëåì */

/******************************************************************/

#endif

 

double Round (double X,double Delta);

/******************************************************************/

/* Ôóíêöèÿ Round (X,Delta) , ïðåäíàçíà÷åíà äëÿ îêðóãëåíèÿ */

/* X ñ òî÷íîñòüþ Delta */

/******************************************************************/

 

double BISECT(double Left,double Right,double Eps,int &N);

/******************************************************************/

/* Ôóíêöèÿ BISECT ïðåäíàçíà÷åíà äëÿ ðåøåíèÿ óðàâíåíèÿ F(X)=0 */

/* ìåòîäîì äåëåíèÿ îòðåçêà ïîïîëàì. Èñïîëüçîâàíû îáîçíà÷åíèÿ: */

/* Left - ëåâûé êîíåö ïðîìåæóòêà */

/* Right - ïðàâûé êîíåö ïðîìåæóòêà */

/* Eps - ïîãðåøíîñòü âû÷èñëåíèÿ êîðíÿ óðàâíåíèÿ; */

/* N - ÷èñëî èòåðàöèé */

/******************************************************************/

 

double ITER(double X0,double Eps,int &N);

/******************************************************************/

/* Ôóíêöèÿ ITER ïðåäíàçíà÷åíà äëÿ ðåøåíèÿ óðàâíåíèÿ F(X)=X */



/* ìåòîäîì ïðîñòîé èòåðàöèè. Èñïîëüçîâàíû îáîçíà÷åíèÿ: */

/* X0 - íà÷àëüíîå ïðèáëèæåíèå êîðíÿ */

/* Eps - ïîãðåøíîñòü âû÷èñëåíèÿ êîðíÿ óðàâíåíèÿ; */

/* N - ÷èñëî èòåðàöèé */

/******************************************************************/

 

double HORDA(double Left,double Right,double Eps,int &N);

/******************************************************************/

/* Ôóíêöèÿ HORDA ïðåäíàçíà÷åíà äëÿ ðåøåíèÿ óðàâíåíèÿ F(x)=0 */

/* ìåòîäîì õîðä. Èñïîëüçîâàíû îáîçíà÷åíèÿ: */

/* Left - ëåâûé êîíåö ïðîìåæóòêà */

/* Right - ïðàâûé êîíåö ïðîìåæóòêà */

/* Eps - ïîãðåøíîñòü âû÷èñëåíèÿ êîðíÿ óðàâíåíèÿ; */

/* N - ÷èñëî èòåðàöèé */

/******************************************************************/

 

double NEWTON (double X,double Eps,int &N);

/******************************************************************/

/* Ôóíêöèÿ NEWTON ïðåäíàçíà÷åíà äëÿ ðåøåíèÿ óðàâíåíèÿ F(X)=0 */

/* ìåòîäîì êàñàòåëüíûõ. Èñïîëüçîâàíû îáîçíà÷åíèÿ: */

/* X - íà÷àëüíîå ïðèáëèæåíèå êîðíÿ */

/* Eps - ïîãðåøíîñòü âû÷èñëåíèÿ êîðíÿ óðàâíåíèÿ; */

/* N - ÷èñëî èòåðàöèé */

/******************************************************************/

 

#include "methods.cpp"

/************************************************************************/

 

Текст программного модуля methods.cpp.

 

/*********************************************/

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

extern double F(double);

double BISECT(double Left,double Right,double Eps,int &N)

{

double E = fabs(Eps)*2.0;

double FLeft = F(Left);

double FRight = F(Right);

double X = (Left+Right)/2.0;

double Y;

if (FLeft*FRight>0.0) {puts("Íåâåðíîå çàäàíèå èíòåðâàëà\n");exit(1);}

if (Eps<=0.0) {puts("Íåâåðíîå çàäàíèå òî÷íîñòè\n");exit(1);}

N=0;

if (FLeft==0.0) return Left;

if (FRight==0.0) return Right;

while ((Right-Left)>=E)

{

X = 0.5*(Right + Left); /* âû÷èñëåíèå ñåðåäèíû îòðåçêà */

Y = F(X);

if (Y == 0.0) return (X);

if (Y*FLeft < 0.0)

Right=X;

else

{ Left=X; FLeft=Y; }

N++;

};

return(X);

}

double Round (double X,double Delta)

{

if (Delta<=1E-9) {puts("Íåâåðíîå çàäàíèå òî÷íîñòè îêðóãëåíèÿ\n");exit(1);}

if (X>0.0) return (Delta*(long((X/Delta)+0.5)));

else return (Delta*(long((X/Delta)-0.5)));

}

double ITER(double X0,double Eps,int &N)

{

if (Eps<=0.0) {puts("Íåâåðíîå çàäàíèå òî÷íîñòè\n");exit (1);}

double X1=F(X0);

double X2=F(X1);

N = 2;

while( (X1 - X2)*(X1 - X2) > fabs((2*X1-X0-X2)*Eps) )

{

X0 = X1;

X1 = X2;

X2 = F(X1);

N++;

}

return(X2);

}

#ifdef __NEWTON

double NEWTON (double X,double Eps,int &N)

{

extern double F1 (double);

double Y,Y1,DX;

N=0;

do

{

Y = F(X);

if (Y==0.0) return (X);

Y1 = F1(X);

if (Y1==0.0) {puts("Ïðîèçâîäíàÿ îáðàòèëàñü â íîëü\n");exit(1);}

DX=Y/Y1; X=X-DX; N++;

}

while (fabs(DX)>Eps);

return (X);

}

#endif

double HORDA(double Left,double Right,double Eps,int &N)

{

double FLeft = F(Left);

double FRight = F(Right);

double X,Y;

if (FLeft*FRight>0.0) {puts("Íåâåðíîå çàäàíèå èíòåðâàëà\n");exit(1);}

if (Eps<=0.0) {puts("Íåâåðíîå çàäàíèå òî÷íîñòè\n");exit(1);}

N=0;

if (FLeft==0.0) return Left;

if (FRight==0.0) return Right;

do

{

X = Left-(Right-Left)*FLeft/(FRight-FLeft);

Y = F(X);

if (Y == 0.0) return (X);

if (Y*FLeft < 0.0)

{ Right=X; FRight=Y; }

else

{ Left=X; FLeft=Y; }

N++;

}

while ( fabs(Y) >= Eps );

return(X);

}

/*********************************************************/

Численное интегрирование





Рекомендуемые страницы:

Воспользуйтесь поиском по сайту:
©2015- 2020 megalektsii.ru Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав.