def newton(f, x0, eps):
    steps = 0
    alph = 1e-2
    x = x0.copy()
    n = len(x)
    while True:
        steps = steps + 1
        (fx, H, dfdx) = f(x)
        (Q, R) = qr_gs_decomp(H)
        Dx_sol = qr_gs_solve(Q, R, -dfdx)
        lamb = 1.0
        while True:
            a = Dx_sol * lamb
            y = x + a
            (fy, Hy, dfy) = f(y)
            #if (np.linalg.norm(fy)<(1-lamb/2.0)*np.linalg.norm(fx) or lamb<0.02):
            if (fy < fx + alph * np.dot(a.T, dfdx) or lamb < 0.02):
                break
            lamb = lamb / 2.0
        x = y.copy()
        dfdx = dfy.copy()
        if (np.linalg.norm(dfdx) < eps):  # steps>stepsmax): #
            print('Process finish after %i iterations.' % (steps))
            break
    return x
Beispiel #2
0
def newton(f, x0, dx, eps):
    stepsmax = 100000
    steps = 0
    x = x0.copy()
    n = len(x)
    J1 = np.empty((n, n))
    R1 = np.empty((n, n))
    Dx = np.empty(n)

    while True:
        fx = f(x)
        for j in range(n):
            x[j] = x[j] + dx
            # xp + dx
            fxp = f(x)
            fxp = fxp - fx
            # df
            for i in range(n):
                J1[i, j] = fxp[i] / dx
            x[j] = x[j] - dx

        fxm = -1.0 * fx
        (J, R) = qr_gs_decomp(J1)
        Dx_sol = qr_gs_solve(J, R, fxm)

        lamb = 2.0
        while True:
            lamb = lamb / 2.0
            y = x + Dx_sol * lamb
            fy = f(y)
            if (np.linalg.norm(fy) < (1 - lamb / 2.0) * np.linalg.norm(fx)
                    or lamb < 0.02):
                break
        x = y.copy()
        fx = fy.copy()
        steps = steps + 1
        if (np.linalg.norm(fx) < eps
                or np.linalg.norm(Dx_sol) < dx):  # steps>stepsmax): #
            print('Process finish after %i iterations.' % (steps))
            break
    return x
m = int(3)
# Columns
A = np.empty((n, m))
b = np.empty(n)

for i in range(n):
    print(x[i], y[i], dy[i])
print('\n')

for i in range(n):
    b[i] = y[i] / dy[i]
    for j in range(m):
        A[i, j] = funs(j, x[i]) / dy[i]

(Q, R) = qr_gs_decomp(A)
c = qr_gs_solve(Q, R, b)

R_inv = qr_gs_inverse(R)
S = np.zeros((m, m))

alph = 1.0
beta = 0.0
S = alph * np.matmul(R_inv, R_inv.T) + beta * S
print("\nThe fit calculated is:")
print("(%g +/- %g)*log(x) + (%g +/- %g)*1.0 + (%g +/- %g)*x\n" %
      (c[0], S[0, 0]**0.5, c[1], S[1, 1]**0.5, c[2], S[2, 2]**0.5))

N = int(1000)
for i in range(1, N):
    X = i / 100.0
    Y = c[0] * funs(0, X) + c[1] * funs(1, X) + c[2] * funs(2, X)
Beispiel #4
0
print(QR);

# Linear equation solve
print('\nSOLVING LINEAL EQUATION SYSTEM\n');
ns = int(4);
x = np.empty(ns);
b = np.random.rand(ns);
AA = np.random.rand(ns, ns);
RR = np.empty((ns, ns));
print('Random matrix A:\n');
print(AA);
# Decomposition QR
(Qb, Rb) = qr_gs_decomp(AA);

# System solving
x_sol = qr_gs_solve(Qb, Rb, b);

print('\nRandom vector b:\n');
print(b);

print('\nSolution (x) of the system of linear equations A*x = Q*Rx = b:\n');
print(x_sol);

# Check that we get b back after A*x
print('\nCheck that A*x = b:\n');
b2 = np.empty(ns);
A2 = np.matmul(Qb, Rb);
b2 = np.matmul(A2, x_sol);
print(b2);

#print(np.linalg.solve(A2, b));