Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Error while assigning iteration on variable S[i,m-1]

Hi. I have the following code: I did some minor changes in the code by adding the variable S[i,m-1], apart from the S[*,*] mentioned in the coding, the values of S's are zero. The actual formula to calculate the first iteration is : u_{i,1}(x)=h*[g_i(x)-S_{i,0}(x)-\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*g_j(t)dt], while for second iteration onwards uses: u_{i,m}(x)=(1+h)*u_{i,m-1}(x)-h*S_{i,m-1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,m-1}(t)dt]. Here is the full code (which was helped by @dsejas).

from __future__ import print_function

NUMBER_OF_ITERATIONS = 10

h, t, x = var('h t x')

g = vector(SR, 5)
print('g(t): ***************************************************')
g[1] = -t^3-t; print(g[1])
g[2] = (1/4)*t^5-(1/4)*t^4-(1/2)*t^3-3*t^2-1; print(g[2])
g[3] = (1/2)*t^6-(31/6)*t^3+2*t^2+3; print(g[3])
g[4] = t^3-5; print(g[4])
print()
gg = vector(SR, 5)
print('gg(t): ***************************************************')
gg[1] = -x^3-x; print(gg[1])
gg[2] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1; print(gg[2])
gg[3] = (1/2)*x^6-(31/6)*x^3+2*x^2+3; print(gg[3])
gg[4] = x^3-5; print(gg[4])
print()

S = matrix(SR, 5, 5)
print('S(x): ***************************************************')
S[1,0] = -x^3; print(S[1,0])
S[1,1] = -x; print(S[1,1])
S[2,0] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3; print(S[2,0])
S[2,1] = -3*x^2-1; print(S[2,1])
S[3,0] = (1/2)*x^6-(31/6)*x^3; print(S[3,0])
S[3,1] = 2*x^2+3; print(S[3,1])
S[4,0] = x^3; print(S[1,0])
S[4,1] = -5; print(S[1,1])

K2 = matrix(SR, 5, 5)
print('K2(x,t): ***************************************************')
K2[1,1] = 0; print(K2[1,1])
K2[1,2] = 1; print(K2[1,2])
K2[1,3] = 1; print(K2[1,3])
K2[1,4] = 0; print(K2[1,4])
K2[2,1] = x-1; print(K2[2,1])
K2[2,2] = t; print(K2[2,2])
K2[2,3] = 0; print(K2[2,3])
K2[2,4] = -x; print(K2[2,4])
K2[3,1] = x-t; print(K2[3,1])
K2[3,2] = 0; print(K2[3,2])
K2[3,3] = 0; print(K2[3,3])
K2[3,4] = -3*t^2; print(K2[3,4])
K2[4,1] = 2*x-3*t; print(K2[4,1])
K2[4,2] = 0; print(K2[4,2])
K2[4,3] = 0; print(K2[4,3])
K2[4,4] = 0; print(K2[4,4])
print()

u = matrix(SR, 5, NUMBER_OF_ITERATIONS+1)
print('u(x): ***************************************************')
print('-------------------- For m = 1: --------------------')
for i in range(1, 5):
    u[i,1] = h*(gg[i]-S[i,0]-h*integrate(K2.row(i)*g.subs(x==t),t, 0, x)); print(u[i,1].full_simplify())
print('')

for m in range(2, NUMBER_OF_ITERATIONS+1):
    print('-------------------- For m = ' + str(m) + ': --------------------')
    for i in range(1, 5):
        u[i,m] = (1+h)*u[i,m-1]-h*S[i,m-1]-h*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, 0, x); print(u[i,m].full_simplify())
    print()

There seems to be something wrong with the code when I added the S[*,*] variable because hand calculation gives u[1,1]=-1/3x^3+3x+1/14x^7+1/24x^6-65/48x^4 which is slightly different from the one i obtain from the coding.

Error while assigning iteration on variable S[i,m-1]

Hi. I have the following code: I did some minor changes in the code by adding the variable S[i,m-1], apart from the S[*,*] mentioned in the coding, the values of S's are zero. The actual formula to calculate the first iteration is : u_{i,1}(x)=h*[g_i(x)-S_{i,0}(x)-\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*g_j(t)dt], while for second iteration onwards uses: u_{i,m}(x)=(1+h)*u_{i,m-1}(x)-h*S_{i,m-1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,m-1}(t)dt]. Here is the full code (which was helped by @dsejas).

from __future__ import print_function

NUMBER_OF_ITERATIONS = 10

h, t, x = var('h t x')

g = vector(SR, 5)
print('g(t): ***************************************************')
g[1] = -t^3-t; print(g[1])
g[2] = (1/4)*t^5-(1/4)*t^4-(1/2)*t^3-3*t^2-1; print(g[2])
g[3] = (1/2)*t^6-(31/6)*t^3+2*t^2+3; print(g[3])
g[4] = t^3-5; print(g[4])
print()
gg = vector(SR, 5)
print('gg(t): ***************************************************')
gg[1] = -x^3-x; print(gg[1])
gg[2] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1; print(gg[2])
gg[3] = (1/2)*x^6-(31/6)*x^3+2*x^2+3; print(gg[3])
gg[4] = x^3-5; print(gg[4])
print()

S = matrix(SR, 5, 5)
print('S(x): ***************************************************')
S[1,0] = -x^3; print(S[1,0])
S[1,1] = -x; print(S[1,1])
S[2,0] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3; print(S[2,0])
S[2,1] = -3*x^2-1; print(S[2,1])
S[3,0] = (1/2)*x^6-(31/6)*x^3; print(S[3,0])
S[3,1] = 2*x^2+3; print(S[3,1])
S[4,0] = x^3; print(S[1,0])
S[4,1] = -5; print(S[1,1])

K2 = matrix(SR, 5, 5)
print('K2(x,t): ***************************************************')
K2[1,1] = 0; print(K2[1,1])
K2[1,2] = 1; print(K2[1,2])
K2[1,3] = 1; print(K2[1,3])
K2[1,4] = 0; print(K2[1,4])
K2[2,1] = x-1; print(K2[2,1])
K2[2,2] = t; print(K2[2,2])
K2[2,3] = 0; print(K2[2,3])
K2[2,4] = -x; print(K2[2,4])
K2[3,1] = x-t; print(K2[3,1])
K2[3,2] = 0; print(K2[3,2])
K2[3,3] = 0; print(K2[3,3])
K2[3,4] = -3*t^2; print(K2[3,4])
K2[4,1] = 2*x-3*t; print(K2[4,1])
K2[4,2] = 0; print(K2[4,2])
K2[4,3] = 0; print(K2[4,3])
K2[4,4] = 0; print(K2[4,4])
print()

u = matrix(SR, 5, NUMBER_OF_ITERATIONS+1)
print('u(x): ***************************************************')
print('-------------------- For m = 1: --------------------')
for i in range(1, 5):
    u[i,1] = h*(gg[i]-S[i,0]-h*integrate(K2.row(i)*g.subs(x==t),t, h*(gg[i]-S[i,0]-integrate(K2.row(i)*g.subs(x==t),t, 0, x)); print(u[i,1].full_simplify())
print('')

for m in range(2, NUMBER_OF_ITERATIONS+1):
    print('-------------------- For m = ' + str(m) + ': --------------------')
    for i in range(1, 5):
        u[i,m] = (1+h)*u[i,m-1]-h*S[i,m-1]-h*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, 0, x); print(u[i,m].full_simplify())
    print()

There seems to be something wrong with the code when I added the S[*,*] variable because hand calculation gives u[1,1]=-1/3x^3+3x+1/14x^7+1/24x^6-65/48x^4 which is slightly different from the one i obtain from the coding.

Error while assigning iteration on variable S[i,m-1]

Hi. I have the following code: I did some minor changes in the code by adding the variable S[i,m-1], apart from the S[*,*] mentioned in the coding, the values of S's are zero. The actual formula to calculate the first iteration is : u_{i,1}(x)=h*[g_i(x)-S_{i,0}(x)-\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*g_j(t)dt], while for second iteration onwards uses: u_{i,m}(x)=(1+h)*u_{i,m-1}(x)-h*S_{i,m-1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,m-1}(t)dt]u_{i,m}(x)=(1+h)*u_{i,m-1}(x)-h*S_{i,m-1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,m-1}(t)dt. Here is the full code (which was helped by @dsejas).

from __future__ import print_function

NUMBER_OF_ITERATIONS = 10

h, t, x = var('h t x')

g = vector(SR, 5)
print('g(t): ***************************************************')
g[1] = -t^3-t; print(g[1])
g[2] = (1/4)*t^5-(1/4)*t^4-(1/2)*t^3-3*t^2-1; print(g[2])
g[3] = (1/2)*t^6-(31/6)*t^3+2*t^2+3; print(g[3])
g[4] = t^3-5; print(g[4])
print()
gg = vector(SR, 5)
print('gg(t): ***************************************************')
gg[1] = -x^3-x; print(gg[1])
gg[2] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1; print(gg[2])
gg[3] = (1/2)*x^6-(31/6)*x^3+2*x^2+3; print(gg[3])
gg[4] = x^3-5; print(gg[4])
print()

S = matrix(SR, 5, 5)
print('S(x): ***************************************************')
S[1,0] = -x^3; print(S[1,0])
S[1,1] = -x; print(S[1,1])
S[2,0] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3; print(S[2,0])
S[2,1] = -3*x^2-1; print(S[2,1])
S[3,0] = (1/2)*x^6-(31/6)*x^3; print(S[3,0])
S[3,1] = 2*x^2+3; print(S[3,1])
S[4,0] = x^3; print(S[1,0])
S[4,1] = -5; print(S[1,1])

K2 = matrix(SR, 5, 5)
print('K2(x,t): ***************************************************')
K2[1,1] = 0; print(K2[1,1])
K2[1,2] = 1; print(K2[1,2])
K2[1,3] = 1; print(K2[1,3])
K2[1,4] = 0; print(K2[1,4])
K2[2,1] = x-1; print(K2[2,1])
K2[2,2] = t; print(K2[2,2])
K2[2,3] = 0; print(K2[2,3])
K2[2,4] = -x; print(K2[2,4])
K2[3,1] = x-t; print(K2[3,1])
K2[3,2] = 0; print(K2[3,2])
K2[3,3] = 0; print(K2[3,3])
K2[3,4] = -3*t^2; print(K2[3,4])
K2[4,1] = 2*x-3*t; print(K2[4,1])
K2[4,2] = 0; print(K2[4,2])
K2[4,3] = 0; print(K2[4,3])
K2[4,4] = 0; print(K2[4,4])
print()

u = matrix(SR, 5, NUMBER_OF_ITERATIONS+1)
print('u(x): ***************************************************')
print('-------------------- For m = 1: --------------------')
for i in range(1, 5):
    u[i,1] = h*(gg[i]-S[i,0]-integrate(K2.row(i)*g.subs(x==t),t, 0, x)); print(u[i,1].full_simplify())
print('')

for m in range(2, NUMBER_OF_ITERATIONS+1):
    print('-------------------- For m = ' + str(m) + ': --------------------')
    for i in range(1, 5):
        u[i,m] = (1+h)*u[i,m-1]-h*S[i,m-1]-h*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, 0, x); print(u[i,m].full_simplify())
    print()

There seems to be something wrong with the code when I added the S[*,*] variable because hand calculation gives u[1,1]=-1/3x^3+3x+1/14x^7+1/24x^6-65/48x^4 which is slightly different from the one i obtain from the coding.

Error while assigning iteration on variable S[i,m-1]

Hi. I have the following code: I did some minor changes in the code by adding the variable S[i,m-1], apart from the S[*,*] mentioned in the coding, the values of S's are zero. The actual formula to calculate the first iteration is : u_{i,1}(x)=h*[g_i(x)-S_{i,0}(x)-\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*g_j(t)dt], while for second iteration onwards uses: u_{i,m}(x)=(1+h)*u_{i,m-1}(x)-h*S_{i,m-1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,m-1}(t)dt. Here is the full code (which was helped by @dsejas).

Update: 2nd january: after 3 hours of discussion and re-deriving all iterations with my sv, we figured out that the iterations for this modified HAM only differs for m=1 and m=2 (the difference is that there is an inclusion of S[*,*] variable). From m=3,...,10 the iterations are exactly the same as HAM (S[*,*] are all equal to zero) that we successfully did before. So I altered the code by leaving m=1 and m=2 separately and only doing the iterations for m=3 till 10 (which I obtained from the code in the previous post). However upon calculating CC, the error is suppose to be some number x 10^(-15). Can you help me skim through the coding to see if I did a mistake anywhere.

from __future__ import print_function

NUMBER_OF_ITERATIONS = 10

h, t, x = var('h t x')

g = vector(SR, 5)
print('g(t): ***************************************************')
g[1] = -t^3-t; print(g[1])
g[2] = (1/4)*t^5-(1/4)*t^4-(1/2)*t^3-3*t^2-1; print(g[2])
g[3] = (1/2)*t^6-(31/6)*t^3+2*t^2+3; print(g[3])
g[4] = t^3-5; print(g[4])
print()
 gg = vector(SR, 5)
print('gg(t): ***************************************************')
gg[1] = -x^3-x; print(gg[1])
gg[2] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1; print(gg[2])
gg[3] = (1/2)*x^6-(31/6)*x^3+2*x^2+3; print(gg[3])
gg[4] = x^3-5; print(gg[4])
print()

S = matrix(SR, 5, 5)
print('S(x): ***************************************************')
S[1,0] = -x^3; print(S[1,0])
S[1,1] = -x; print(S[1,1])
S[2,0] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3; print(S[2,0])
S[2,1] = -3*x^2-1; print(S[2,1])
S[3,0] = (1/2)*x^6-(31/6)*x^3; print(S[3,0])
S[3,1] = 2*x^2+3; print(S[3,1])
S[4,0] = x^3; print(S[1,0])
S[4,1] = -5; print(S[1,1])

K2 = matrix(SR, 5, 5)
print('K2(x,t): ***************************************************')
K2[1,1] = 0; print(K2[1,1])
K2[1,2] = 1; print(K2[1,2])
K2[1,3] = 1; print(K2[1,3])
K2[1,4] = 0; print(K2[1,4])
K2[2,1] = x-1; print(K2[2,1])
K2[2,2] = t; print(K2[2,2])
K2[2,3] = 0; print(K2[2,3])
K2[2,4] = -x; print(K2[2,4])
K2[3,1] = x-t; print(K2[3,1])
K2[3,2] = 0; print(K2[3,2])
K2[3,3] = 0; print(K2[3,3])
K2[3,4] = -3*t^2; print(K2[3,4])
K2[4,1] = 2*x-3*t; print(K2[4,1])
K2[4,2] = 0; print(K2[4,2])
K2[4,3] = 0; print(K2[4,3])
K2[4,4] = 0; print(K2[4,4])
print()

u = matrix(SR, 5, NUMBER_OF_ITERATIONS+1)
print('u(x): ***************************************************')
print('-------------------- For m = 1: --------------------')
for i in range(1, 5):
    u[i,1] = h*(gg[i]-S[i,0]-integrate(K2.row(i)*g.subs(x==t),t, u[1,1] = h*(gg[1]-S[1,0]-h*integrate(K2.row(1)*g.subs(x==t),t, 0, x)); print(u[i,1].full_simplify())
print('')

print(u[1,1].full_simplify())
u[2,1] = h*(gg[2]-S[2,0]-h*integrate(K2.row(2)*g.subs(x==t),t, 0, x)); print(u[2,1].full_simplify())
u[3,1] = h*(gg[3]-S[3,0]-h*integrate(K2.row(3)*g.subs(x==t),t, 0, x)); print(u[3,1].full_simplify())
u[4,1] = h*(gg[4]-S[4,0]-h*integrate(K2.row(4)*g.subs(x==t),t, 0, x)); print(u[4,1].full_simplify())
print()

u[1,2] = (1+h)*u[1,1]-h*S[1,1]-h*integrate(K2.row(1)*u.column(1).subs(x==t),t, 0, x); print(u[1,2].full_simplify())
u[2,2] = (1+h)*u[2,1]-h*S[2,1]-h*integrate(K2.row(2)*u.column(2).subs(x==t),t, 0, x); print(u[2,2].full_simplify())
u[3,2] = (1+h)*u[3,1]-h*S[3,1]-h*integrate(K2.row(3)*u.column(3).subs(x==t),t, 0, x); print(u[3,2].full_simplify())
u[4,2] = (1+h)*u[4,1]-h*S[4,1]-h*integrate(K2.row(4)*u.column(4).subs(x==t),t, 0, x); print(u[4,2].full_simplify())
print()
for m in range(2, range(3, NUMBER_OF_ITERATIONS+1):
    print('-------------------- For m = ' + str(m) + ': --------------------')
    for i in range(1, 5):
        u[i,m] = (1+h)*u[i,m-1]-h*S[i,m-1]-h*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, (1+h)*u[i,m-1]-h*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, 0, x); print(u[i,m].full_simplify())
    print()
AA=gg[1]+u[1,1]+u[1,2]+u[1,3]+u[1,4]+u[1,5]+u[1,6]+u[1,7]+u[1,8]+u[1,9]+u[1,10];AA
BB=AA.subs(h=-1,x=0.1);BB
CC=abs(BB-0.1);CC

There seems to be something wrong with the code when I added the S[*,*] variable because hand calculation gives u[1,1]=-1/3x^3+3x+1/14x^7+1/24x^6-65/48x^4 which is slightly different from the one i obtain from the coding.