Численные методы и оптимизация, страница 3

RO = SQR(((1 / (I + 1)) * R))

PRINT "Погрешность апроксимации"

PRINT RO

PRINT "Значения F(X)       F'(X)"

FOR N = -10 TO 10 STEP 2

FF = A(2) * N * N + A(1) * N + A(0)

FFF = A(2) * 2 * N + A(1)

PRINT N; "   "; FF; "   "; FFF

NEXT

X = 4

DE = .015

CALL NJUTON(DE, X, A(), N)

PRINT "КорниметодомНьютона"

PRINT "N="; N; " X="; X;

X1 = X

X = 14

DE = .02

CALL NJUTON(DE, X, A(), N)

PRINT "  N="; N; " X="; X

X2 = X

PRINT "Корни методом итераций"

X = 4

L = .5

DE = .0001

CALL ITER(DE, L, X, A(), N)

PRINT "N="; N; " X="; X;

X = 14

L = -.5

CALL ITER(DE, L, X, A(), N)

PRINT "  N="; N; " X="; X

PRINT "Интеграл методом прямоугольников"

N = 10

CALL PRJAM(N, X1, X2, A(), I)

PRINT "N="; N; " I="; I;

I1 = I

N = 20

CALL PRJAM(N, X1, X2, A(), I)

R = ABS(I1 - I) / 3

PRINT "  N="; N; " I="; I; "  R="; R

PRINT "Интеграл методом трапеций"

N = 10

CALL TRAP(N, X1, X2, A(), I)

I1 = I

PRINT "N="; N; " I="; I;

N = 20

CALL TRAP(N, X1, X2, A(), I)

R = ABS(I1 - I) / 3

PRINT "  N="; N; " I="; I; "  R="; R

PRINT "Интеграл методом симпсона"

N = 10

CALL SIMP(N, X1, X2, A(), I)

I1 = I

PRINT "N="; N; " I="; I;

N = 20

CALL SIMP(N, X1, X2, A(), I)

R = ABS(I1 - I) / 15

PRINT "  N="; N; " I="; I; "  R="; R

PRINT "Экстремум методом дитохомии"

AI = -10

BI = 10

CALL DIHOTOM(N, AI, BI, DE, A(), XM, FXM)

PRINT "N="; N; "XM="; XM; "FXM="; FXM

PRINT "Экстремум методом золотого сечения"

AI = -10

BI = 10

CALL GOLD(N, AI, BI, DE, A(), XM, FXM)

PRINT "N="; N; "XM="; XM; "FXM="; FXM

Тексты подпрограмм:

SUB DIHOTOM (N, AI, BI, DE, A(), XM, FXM)

H = DE / 2

N = 0

DO

X1 = (AI + BI - H) / 2

X2 = (AI + BI + H) / 2

FX1 = A(2) * X1 * X1 + A(1) * X1 + A(0)

FX2 = A(2) * X2 * X2 + A(1) * X2 + A(0)

IF FX1 > FX2 THEN AI = X1

IF FX1 <= FX2 THEN BI = X2

N = N + 1

D = ABS(BI - AI)

LOOP UNTIL D < DE

XM = (AI + BI) / 2

FXM = A(2) * XM * XM + A(1) * XM + A(0)

END SUB

SUB GAUSS (K, G(), B(), A())

REM Вспомогательная матрица

DIM S(K, (K + 1))

FOR L = 0 TO K

FOR M = 0 TO K

S(L, M) = G(L, M)

NEXT M

S(L, (K + 1)) = B(L)

NEXT L

REM Прямойход

FOR L = 0 TO K - 1

FOR M = L + 1 TO K

FOR J = L + 1 TO K + 1

S(M, J) = S(M, J) - S(L, J) * (S(M, L) / S(L, L))

NEXT J

NEXT M

NEXT L

A(K) = S(K, K + 1) / S(K, K)

REM Обратныйход

FOR M = K - 1 TO 0 STEP -1

A(M) = S(M, K + 1)

FOR J = K TO M + 1 STEP -1

A(M) = A(M) - S(M, J) * A(J)

NEXT J

A(M) = A(M) / S(M, M)

NEXT M

END SUB

SUB GOLD (N, AI, BI, DE, A(), XM, FXM)

N = 0

DO

X1 = (BI - AI) * .382 + AI

X2 = (BI - AI) * .618 + AI

FX1 = A(2) * X1 * X1 + A(1) * X1 + A(0)

FX2 = A(2) * X2 * X2 + A(1) * X2 + A(0)

IF FX1 > FX2 THEN AI = X1

IF FX1 <= FX2 THEN BI = X2

N = N + 1

D = ABS(BI - AI)

LOOP UNTIL D < DE

XM = (AI + BI) / 2

FXM = A(2) * XM * XM + A(1) * XM + A(0)

END SUB

SUB GRAM (I, K, G(), X(), Y(), B())

REM MATRIX

FOR L = 0 TO K

FOR M = 0 TO K

FOR N = 0 TO I

G(L, M) = G(L, M) + (X(N) ^ (L + M))

NEXT N

G(0, 0) = I + 1

NEXT M

FOR N = 0 TO I

B(L) = B(L) + Y(N) * X(N) ^ L

NEXT N

NEXT L

END SUB

SUB ITER (DE, L, X, A(), N)

N = 0

DO

XN = X + L * (A(2) * X * X + A(1) * X + A(0))

D = ABS(X - XN)

FX0 = A(2) * X * X + A(1) * X + A(0)

FXN = A(2) * XN * XN + A(1) * XN + A(0)

DF = ABS(FX0 - FXN)

IF DF > D THEN D = DF

X = XN

N = N + 1

LOOP UNTIL D < DE

END SUB

SUB NJUTON (DE, X, A(), N)

N = 0

DO

XN = X - (A(2) * X * X + A(1) * X + A(0)) / (A(2) * 2 * X + A(1))

REM PRINT XN

D = ABS(X - XN)

X = XN

N = N + 1

LOOP UNTIL D < DE

END SUB

SUB PRJAM (N, X1, X2, A(), I)

H = ABS(X2 - X1) / N

I = 0

FOR K = 0 TO (N - 1)

X = X1 + K * H + H / 2

I = I + (A(2) * (X) * (X) + A(1) * (X) + A(0)) * H

NEXT

END SUB

SUB SIMP (N, X1, X2, A(), I)

I = 0

H = (X2 - X1) / (2 * N)

I = A(2) * X1 * X1 + A(1) * X1 + A(0)

I = I + A(2) * X2 * X2 + A(1) * X2 + A(0)

C = 4

X = X1

FOR K = 1 TO (2 * N - 1)

X = X + H

I = I + (A(2) * X * X + A(1) * X + A(0)) * C

C = 6 - C

NEXT

I = I * H / 3

END SUB

SUB TRAP (N, X1, X2, A(), I)

I = 0

H = ABS(X2 - X1) / N

FOR K = 1 TO (N - 1)

X = X1 + K * H

I = I + A(2) * X * X + A(1) * X + A(0)

NEXT

FA = A(2) * X1 * X1 + A(1) * X1 + A(0)

FB = A(2) * X2 * X2 + A(1) * X2 + A(0)

I = (I * 2 + FA + FB) / 2 * H

END SUB