ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 02 Jan 2020 17:23:44 -0600Error while assigning iteration on variable S[i,m-1]https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-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)
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);CCWed, 01 Jan 2020 07:32:37 -0600https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/Comment by dsejas for <p>Hi. I have the following code: I did some minor changes in the code by adding the variable <code>S[i,m-1]</code>, apart from the <code>S[*,*]</code> mentioned in the coding, the values of <code>S</code>'s are zero. The actual formula to calculate the first iteration is : <code>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]</code>, while for second iteration onwards uses: <code>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</code>. Here is the full code (which was helped by @dsejas).</p>
<p>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 <code>m=1</code> and <code>m=2</code> (the difference is that there is an inclusion of <code>S[*,*]</code> variable). From <code>m=3,...,10</code> the iterations are exactly the same as HAM (<code>S[*,*]</code> are all equal to zero) that we successfully did before. So I altered the code by leaving <code>m=1</code> and <code>m=2</code> separately and only doing the iterations for m=<code>3 till 10</code> (which I obtained from the code in the previous post). However upon calculating <code>CC</code>, the error is suppose to be some number x <code>10^(-15)</code>. Can you help me skim through the coding to see if I did a mistake anywhere.</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49330#post-id-49330Hello, @Sha! I have been studying your code and your formulas for almost six hours. I think I have an idea of how to solve your problems. Correct me if I am wrong, but I can see you are trying to construct a series associated to a recurrence sequence. If that is correct, I believe I can help you with this. However, the details are too long for this forum. Unfortunately, this forum doesn't show the e-mail addresses of other users, so I don't know where to write you. So, if you don't mind, I would like you to write me an e-mail to the address you will find on my profile comment (it's in human-readable format).Thu, 02 Jan 2020 17:23:44 -0600https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49330#post-id-49330Comment by Sha for <p>Hi. I have the following code: I did some minor changes in the code by adding the variable <code>S[i,m-1]</code>, apart from the <code>S[*,*]</code> mentioned in the coding, the values of <code>S</code>'s are zero. The actual formula to calculate the first iteration is : <code>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]</code>, while for second iteration onwards uses: <code>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</code>. Here is the full code (which was helped by @dsejas).</p>
<p>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 <code>m=1</code> and <code>m=2</code> (the difference is that there is an inclusion of <code>S[*,*]</code> variable). From <code>m=3,...,10</code> the iterations are exactly the same as HAM (<code>S[*,*]</code> are all equal to zero) that we successfully did before. So I altered the code by leaving <code>m=1</code> and <code>m=2</code> separately and only doing the iterations for m=<code>3 till 10</code> (which I obtained from the code in the previous post). However upon calculating <code>CC</code>, the error is suppose to be some number x <code>10^(-15)</code>. Can you help me skim through the coding to see if I did a mistake anywhere.</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49318#post-id-49318@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`Thu, 02 Jan 2020 02:56:23 -0600https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49318#post-id-49318Comment by Sha for <p>Hi. I have the following code: I did some minor changes in the code by adding the variable <code>S[i,m-1]</code>, apart from the <code>S[*,*]</code> mentioned in the coding, the values of <code>S</code>'s are zero. The actual formula to calculate the first iteration is : <code>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]</code>, while for second iteration onwards uses: <code>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</code>. Here is the full code (which was helped by @dsejas).</p>
<p>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 <code>m=1</code> and <code>m=2</code> (the difference is that there is an inclusion of <code>S[*,*]</code> variable). From <code>m=3,...,10</code> the iterations are exactly the same as HAM (<code>S[*,*]</code> are all equal to zero) that we successfully did before. So I altered the code by leaving <code>m=1</code> and <code>m=2</code> separately and only doing the iterations for m=<code>3 till 10</code> (which I obtained from the code in the previous post). However upon calculating <code>CC</code>, the error is suppose to be some number x <code>10^(-15)</code>. Can you help me skim through the coding to see if I did a mistake anywhere.</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49317#post-id-49317@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.Thu, 02 Jan 2020 02:51:49 -0600https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49317#post-id-49317Comment by Sha for <p>Hi. I have the following code: I did some minor changes in the code by adding the variable <code>S[i,m-1]</code>, apart from the <code>S[*,*]</code> mentioned in the coding, the values of <code>S</code>'s are zero. The actual formula to calculate the first iteration is : <code>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]</code>, while for second iteration onwards uses: <code>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</code>. Here is the full code (which was helped by @dsejas).</p>
<p>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 <code>m=1</code> and <code>m=2</code> (the difference is that there is an inclusion of <code>S[*,*]</code> variable). From <code>m=3,...,10</code> the iterations are exactly the same as HAM (<code>S[*,*]</code> are all equal to zero) that we successfully did before. So I altered the code by leaving <code>m=1</code> and <code>m=2</code> separately and only doing the iterations for m=<code>3 till 10</code> (which I obtained from the code in the previous post). However upon calculating <code>CC</code>, the error is suppose to be some number x <code>10^(-15)</code>. Can you help me skim through the coding to see if I did a mistake anywhere.</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49316#post-id-49316Hello @dsejas
0. Yes I did some modification to my K values
1. sorry typo. i have removed the extra bracket at the end.
2. 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?
3. sorry typo again I have removed the 'h' attached to the integral. It is not suppose to be there. thank you.Wed, 01 Jan 2020 23:44:14 -0600https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49316#post-id-49316Comment by dsejas for <p>Hi. I have the following code: I did some minor changes in the code by adding the variable <code>S[i,m-1]</code>, apart from the <code>S[*,*]</code> mentioned in the coding, the values of <code>S</code>'s are zero. The actual formula to calculate the first iteration is : <code>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]</code>, while for second iteration onwards uses: <code>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</code>. Here is the full code (which was helped by @dsejas).</p>
<p>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 <code>m=1</code> and <code>m=2</code> (the difference is that there is an inclusion of <code>S[*,*]</code> variable). From <code>m=3,...,10</code> the iterations are exactly the same as HAM (<code>S[*,*]</code> are all equal to zero) that we successfully did before. So I altered the code by leaving <code>m=1</code> and <code>m=2</code> separately and only doing the iterations for m=<code>3 till 10</code> (which I obtained from the code in the previous post). However upon calculating <code>CC</code>, the error is suppose to be some number x <code>10^(-15)</code>. Can you help me skim through the coding to see if I did a mistake anywhere.</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49308#post-id-49308Hello, @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?Wed, 01 Jan 2020 09:33:24 -0600https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49308#post-id-49308Comment by Sha for <p>Hi. I have the following code: I did some minor changes in the code by adding the variable <code>S[i,m-1]</code>, apart from the <code>S[*,*]</code> mentioned in the coding, the values of <code>S</code>'s are zero. The actual formula to calculate the first iteration is : <code>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]</code>, while for second iteration onwards uses: <code>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</code>. Here is the full code (which was helped by @dsejas).</p>
<p>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 <code>m=1</code> and <code>m=2</code> (the difference is that there is an inclusion of <code>S[*,*]</code> variable). From <code>m=3,...,10</code> the iterations are exactly the same as HAM (<code>S[*,*]</code> are all equal to zero) that we successfully did before. So I altered the code by leaving <code>m=1</code> and <code>m=2</code> separately and only doing the iterations for m=<code>3 till 10</code> (which I obtained from the code in the previous post). However upon calculating <code>CC</code>, the error is suppose to be some number x <code>10^(-15)</code>. Can you help me skim through the coding to see if I did a mistake anywhere.</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49301#post-id-49301@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.Wed, 01 Jan 2020 07:35:47 -0600https://ask.sagemath.org/question/49300/error-while-assigning-iteration-on-variable-sim-1/?comment=49301#post-id-49301