How to iterate through excel rows in python

Viewed 44

I have a function which consists of few internal signals running continuously in a loop. I am trying to create an xls and capture on of the internal signal values (which are not the return values of for loop) in every iteration, but I am only getting first return value. Here is my code:

import numpy as np
import math
import xlsxwriter
def conjgrad(A, b, x):
      
    r = b - np.dot(A, x)
    p = r
    rsold = np.dot(np.transpose(r), r)
    
    for i in range(2*len(b)):
        Ap = np.dot(A, p)
        alpha = rsold / np.dot(np.transpose(p), Ap)
        x = x + np.multiply(alpha, p)
        r = r - np.multiply(alpha, Ap)
        rsnew = np.dot(np.transpose(r), r)
        if (np.sqrt(rsnew) < 1e-8).all():
            print('Itr:', i)
            break
        print('Final Itr:', i)
        beta = rsnew/rsold
        p = r + np.multiply(beta,p)
        rsold = rsnew
        inter = b - np.dot(A,x)
        sumCol = 0;
        for j in range(0, len(b)):  
            sumCol = sumCol + inter[j][0];
        print('1-norm',sumCol)
        norm_2d = np.sqrt(math.fabs(sumCol))
        print('2d-norm',norm_2d)

        workbook = xlsxwriter.Workbook('cg_norm_sample.xlsx')
        worksheet = workbook.add_worksheet()
        col = 0
        row = 0

        for iteration, cost in (norm_2d):
            worksheet.write(row, col, iteration)
            worksheet.write(row, col +1, norm)
            row = row+1
    return x
if __name__ == '__main__':
    A = np.array([[0, 1, 0, 0], [0, 0, 1, 0], [0,2,0,0],[1,0,0,1]])
    print(A)
    b = np.full((4, 1), 1)
    print(b)
    x = np.full((4, 1), 1)
    print(x)
    print ('start')
    x = conjgrad(A, b, x)
    print(x)

It is showing me this error near for iteration, cost in (norm_2d) TypeError: 'numpy.float64' object is not iterable*

Can someone please help me here. Thanks in advance.

0 Answers
Related