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

asked 2020-01-01 14:32:37 +0200

Sha gravatar image

updated 2020-01-02 09:48:53 +0200

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)
u[1,1] = h*(gg[1]-S[1,0]-h*integrate(K2.row(1)*g.subs(x==t),t, 0, x)); 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(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*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
edit retag flag offensive close merge delete

Comments

@dsejas hi sorry for bothering you.. if by chance you can help me look at this code. I modified it from your coding by adding the S variable. This code is for Modified HAM results, while the previous one gave the HAM results.. In my paper, I am suppose to compare these two results in terms of their accuracy. Thank you so much.

Sha gravatar imageSha ( 2020-01-01 14:35:47 +0200 )edit

Hello, @Sha! I can see you also modified the values K2[1,1], K2[1,2], K2[4,1]. Is that correct?

I have a couple of notes: 1. Your second formula has a closing "]", but not an opening "[". 2. You are using S with 0-based indexing in your first formula, but then you should write S[i,m-2] in your second formula, but you use S[i,m-1] . I will correct that to work with 1-based indexing instead: The first formula should use S[i,1] instead of S[i,0], so I will modify some indexes, if you agree. 3. In your code (first formula), you have an h multiplying your integral, but there is another outside the parentheses. However, in your formula, the only h is outside the parentheses. Should i correct that, too?

dsejas gravatar imagedsejas ( 2020-01-01 16:33:24 +0200 )edit

Hello @dsejas

  1. Yes I did some modification to my K values
  2. sorry typo. i have removed the extra bracket at the end.
  3. i discussed with my sv regarding the 0 index in S. He said it is 0-indexed because of the theoretical calculation when deriving the formulas. So it is suppose to start from S[i,0] . For example, u[i,1] (first iteration) uses S[i,0], while u[i,2] (second iteration) uses S[i,1]. By modifying the indexes as you suggested, will it change the formula?
  4. sorry typo again I have removed the 'h' attached to the integral. It is not suppose to be there. thank you.
Sha gravatar imageSha ( 2020-01-02 06:44:14 +0200 )edit

@dsejas latest update after 3 hours discussion: upon 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 in the previous post. So I altered the code by leaving m=1 and m=2 separately and only did 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), which is totally different from what I obtained. Can you help me skim through the coding to see if I did a mistake anywhere.

Sha gravatar imageSha ( 2020-01-02 09:51:49 +0200 )edit

@dsejas

for m=1: 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]

for m=2: u_{i,2}(x)=(1+h)*u_{i,1}(x)-h*S_{i,1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,1}(t)dt

for m=3 till 10: u_{i,m}(x)=(1+h)*u_{i,m-1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,m-1}(t)dt

Sha gravatar imageSha ( 2020-01-02 09:56:23 +0200 )edit