ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 01 Jan 2020 14:32:37 +0100error while assigning variableshttps://ask.sagemath.org/question/49155/error-while-assigning-variables/ Hi. I have the following coding of iteration where I want to find the values of u[1,1], u[1,2], u[1,3] and u[1,4].
u,t,x = var('u t x')
u[1]=x;u[1]
u[2]=x^2-1;u[2]
u[3]=2*x^2+3;u[3]
u[4]=x^3-5;u[4]
g[1]=-x^3-x;g[1]
g[2]=(1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1;g[2]
g[3]=(1/2)*x^6-(31/6)*x^3+2*x^2+3;g[3]
g[4]=x^3-5;g[4]
K2[1,1]=1;K2[1,1]
K2[1,2]=0;K2[1,2]
K2[1,3]=1;K2[1,3]
K2[1,4]=1;K2[1,4]
K2[2,1]=x-1;K2[2,1]
K2[2,2]=t;K2[2,2]
K2[2,3]=0;K2[2,3]
K2[2,4]=-x;K2[2,4]
K2[3,1]=x-t;K2[3,1]
K2[3,2]=0;K2[3,2]
K2[3,3]=0;K2[3,3]
K2[3,4]=-3*t^2;K2[3,4]
K2[4,1]=2*x-3;K2[4,1]
K2[4,2]=0;K2[4,2]
K2[4,3]=0;K2[4,3]
K2[4,4]=0;K2[4,4]
for i in range(1,4):
u[i,1]=-h*sum(integrate(K2[i,j]*g[j]),j,1,4);u[i,1]
for i in range(1,4):
u[i,2]=(1+h)*u[i,1]-h*sum(integrate(K2[i,j]*u[j,1]),j,1,4);u[i,2]
But it gave me an error saying that: 'sage.symbolic.expression.Expression' object does not support item assignment Can someone explain to me what I might be doing wrong here.ShaTue, 24 Dec 2019 06:37:57 +0100https://ask.sagemath.org/question/49155/Error 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);CCShaWed, 01 Jan 2020 14:32:37 +0100https://ask.sagemath.org/question/49300/apply functions iteratively (modified re-post)https://ask.sagemath.org/question/36159/apply-functions-iteratively-modified-re-post/*(This is a modified re-post of [Apply Functions Iteratively](https://ask.sagemath.org/question/35989/apply-functions-iteratively/) since my original question wasn't answered)*
I have a few functions `R1,R2,R3,R4` which act on vectors. If `X = matrix(3,1,(x0,y0,r))` then I have
`def R1(X):
return matrix(3,1,(X[0,0]/(X[0,0]^2+X[1,0]^2-X[2,0]^2),X[1,0]/(X[0,0]^2+X[1,0]^2-X[2,0]^2),X[2,0]/abs(X[0,0]^2+X[1,0]^2-X[2,0]^2)))`
`def R2(X):
return matrix(3,1,(-X[0,0],X[1,0],X[2,0]))`
`def R3(X):
return matrix(3,1,(-X[0,0]+2,X[1,0],X[2,0]))`
`def R4(X):
return matrix(3,1,(X[0,0],-X[1,0]+2,X[2,0]))`
For instance, if `v = matrix(3,1,(1,1,1))`, then applying R2 to v gives the matrix $(-1,1,1)$.
I'd like to apply about 10 "levels" of iteration of these functions and have the output as a list of vectors.
So, the list should be
$R1(v), R2(v),R3(v),R4(v),R1^2(v),R2(R1(v)),R3(R1(v)),R4(R1(v)),R1^3(v),...,R1^{10}(v)\,...,R4^{10}(v)$
(as vectors)
where $R_j^n = R_j \circ \cdots \circ R_j$ $n$ times.
I know how to apply a single function iteratively, but I don't know how to combine/loop the four together.
Thanks.Daniel LWed, 04 Jan 2017 02:14:39 +0100https://ask.sagemath.org/question/36159/