Matrices in Keller Box, Error to retrieve value

Question:

I’m getting error in the following code, regarding the matrices, can someone explain what is happening? I’m new to python, can’t really understand the error. This code is to solve boundary layer using keller box or also can be known as matrices in matrix. Here’s the whole code

import numpy as npy

blt = int(raw_input("Input the boundary layer thickness = "))
deleta = float(raw_input("Input the step size of boundary layer thickness = "))
np = int((blt/deleta) + 1)
stop = 1
k=1
l=2
g=2
eselon = 0.00001

def eta(j,k):
    if j == 1 and k == 1:
        return 0
    else:
        return eta(j-1,k) + deleta;
deta = deleta
def etanp():
    return eta(np,k)       
def f(j,k):
    return -eta(np,1)*etab2 + etanp()*etab1 + eta(j,1)
def u(j,k):
    return  -3*etab1 + 2*etab +1
def v(j,k):
    return (-6/etanp()*etab + (2/etanp()))
def fb(j,k):
    return 0.5 * (f(j,k) + f(j-1,k))
def ub(j,k):
    return 0.5 * (u(j,k) + u(j-1,k))
def vb(j,k):
    return 0.5*(v(j,k) + v(j-1,k))
def fvb(j,k):
    return fb(j,k)*vb(j,k)
def uub(j,k):
    return ub(j,k) * ub(j,k)

def a1(j,k):
    return 1 + 0.5*deta *fb(j,k)
def a2(j,k):
    return -1 + 0.5*deta *fb(j,k)
def a3(j,k):
    return 0.5 * deta * vb(j,k)
def a4(j,k):
    return a3(j,k)
def a5(j,k):
    return -1 * deta * ub(j,k)
def a6(j,k):
    return a5(j,k)

def r1(j,k):
    return f(j-1,k) - f(j,k) + deta * ub(j,k)
def r2(j,k):
    return u(j-1,k) - u(j,k) + deta * vb(j,k)
def r3(j,k):
    return v(j-1,k)-v(j,k) - deta *((fvb(j,k)*uub(j,k)))-deta

def AJ(j,k):
    if j == 2:
        return npy.matrix([[0,1,0],[-0.5*deta,0,-0.5*deta],[a2(2,k), a3(2,k), a1(2,k)]])
    else:
        return npy.matrix([[-0.5*deta,0,0],[-1,0,-0.5*deta],[a6(j,k),a3(j,k),a1(j,k)]])
def BJ(j,k):
    return npy.matrix([[0,-1,0],[0,0,-0.5*deta],[0,a4(j,k),a2(j,k)]])
def CJ(j,k):
    return npy.matrix([[-0.5*deta,0,0],[1,0,0],[a5(j,k),0,0]])

def alfa(j,k):
    return AJ(j,k) - (BJ(j,k)*gamma(j,k))
def gamma(j,k):
    return npy.matrix.I((alfa(g,k))*CJ(g,k))
def rr(j,k):
    return npy.matrix([[r1(j,k)],[r2(j,k)],[r3(j,k)]])   
def ww(j,k):
    if j == 2:
        return npy.matrix.I(AJ(2,k)*rr(2,k))
    else:
        return npy.matrix.I((alfa(j,k))*(rr(j,k)-(BJ(j,k)*ww(j-1,k))))

def dell(j,k):
     if j == np:
        return ww(np,k)
     else:   
        return ww(j,k) - (gamma(j,k)*dell(j+1,k))
def delf(j,k):
    if j == 1:
        return 0
    elif j == 2:
        return dell(2,k)[2,1]
    else:
        return dell(j,k)
def delu(j,k):
    if j == 1 or j == np:
        return 0
    elif j == np-1:
        return dell(j,k)[1,1]
def delv(j,k):
    if j == 1:
        return dell(2,k)[1,1]
    elif j == 2:
        return dell(2,k)[3,1]
    else:
        return dell(j,k)[3,1]

def ffinal(j,l):
    return f(j,k) + delf(j,k)
def ufinal(j,l):
    return u(j,k) + delu(j,k)
def vfinal(j,l):
    return v(j,k) + delv(j,k)

# Beginning of calculation for Keller-Box

while stop > eselon:
    eta(1,1)
    for j in range (2,np):
        eta(j,k)  

# Initial condition
    etab = eta(j,k) / eta(np,k)
    etab1 = etab**2
    etab2 = etab**3 
    for j in range (1,np):
        deta
        f(j,1)
        u(j,1)
        v(j,1)

# Current value of Central Differentiation
    for j in range (2,np):
        fb(j,k)
        ub(j,k)
        vb(j,k)
        fvb(j,k)
        uub(j,k)
        a1(j,k)
        a2(j,k)
        a3(j,k)
        r1(j,k)
        r2(j,k)
        r3(j,k)
# Matrices Value for A1, Aj, Bj, and CJ
        CJ(j,k)
        AJ(j,k)
        BJ(j,k)
# Recursion: Forward Sweeping
    for j in range (3,np):
        alfa(j,k)
        gamma(j,k)
    for j in range(2,np):
        rr(j,k)
    for j in range(3,np):
        ww(j,k)

# Recursion: Backward Sweeping
    for j in range (np-1,2,-1):
        dell(j,k)

    for j in range (np,3,-1):
        delu(j-1,k)
        delf(j,k)
        delv(j,k)

# Newton's Method
    for j in range (1,np):
        ffinal(j,l)
        ufinal(j,l)
        vfinal(j,l)

# Check the convergence of iteration
    stop = npy.abs(delv(1,k))
    kmax = k
    k =+ 1

cfrex = vfinal(1,kmax)

print cfrex
Asked By: faridCS227

||

Answers:

You should call numpy.matrix with parentheses:

>>> numpy.matrix([[1,2],[1,2]])
matrix([[1, 2],
        [1, 2]])

Try numpy tutorial.

Answered By: graphite

Update: Your functions alfa and gamma, defined as follows:

def alfa(j,k):
    print 'alfa({},{}) called'.format(j,k)
    return AJ(j,k) - (BJ(j,k)*gamma(j,k))

def gamma(j,k):
    print 'gamma({},{}) called'.format(j,k)
    return npy.matrix.I((alfa(g,k))*CJ(g,k))

will run on forever no matter what they are given as arguments, because each will just call the other and they have no way to terminate. That is, regardless of the inputs, each function will call the other.


There are a few errors in your code that I can see.

1) This is the one @graphite mentioned, where npy.matrix is being called without parentheses around its argument:

def A1(j,k):
    return npy.matrix[[0,1,0],[-0.5*deta(2,k),0,-0.5*deta(2,k)],[a2(2,k), a3(2,k), a1(2,k)]] 
# needs parentheses:
    return npy.matrix([[0,1,0],[-0.5*deta(2,k),0,-0.5*deta(2,k)],[a2(2,k), a3(2,k), a1(2,k)]])

Fix this everywhere you use npy.matrix

2) In your definition of r, you are missing what I assume should be a k:

def r1(j,k):
    return f(j-1,k) - f(j,k) + deta(j,) * ub(j,k)
#should be:
    return f(j-1,k) - f(j,k) + deta(j,k) * ub(j,k)

3) Here np1 is not defined. Perhaps it should be np?

# Recursion: Backward Sweeping
    for j in range (np1,2,-1):
        dell(j,k)

But in general this code could be made much more readable (and therefore easier to find bugs) if you defined fewer functions, especially those functions with no arguments, for example:

def etab():
    return eta(j,k) / eta(np,k)
def etab1():
    return etab()*etab()
def etab2():
    return etab()*etab()*etab()

Could be replaced with:

etab = eta(j, k) / eta(np, k)
etab1 = etab**2
etab2 = etab**3

Another example:

def deta(j,k):
    return deleta

doesn’t even use j or k, so you could change the whole thing to:

deta = deleta

and everywhere below, instead of writing deta(j, k) or deta(2, k) you can just write deta. Of course, if you plan on changing your code at some point in the future so that deta will actually change for different j or k, then you could keep it as is.

I hope this helps!

Answered By: askewchan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.