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.Fri, 03 Jan 2020 17:00:31 +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.Tue, 24 Dec 2019 06:37:57 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49261#post-id-49261Hello, @Sha! I am so sorry, but I can't find any errors in the code. The only explanation I can give to the difference in our results is a misunderstanding in the formulas. Actually, I am having trouble understanding your notations (there seems to be a little inconsistency in your formulas, so I have made some assumptions about what you intended to convey). I can give you two alternatives:
1. Send me your formulas in mathematical notation in their original form (could be a picture or LaTeX), but not as pseudocode as in your comments.
2. It is possible that I'm making some mistake (this is not my area of study). You will have to close this question, reformulate it with the added knowledge, and ask it again in a different form.
I think I am no more useful or helpful in this case. Sorry!Mon, 30 Dec 2019 17:31:30 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49261#post-id-49261Comment by Sha for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49265#post-id-49265@dsejas sorry about all the confusion. Let me state the formulas here in the original form:
first iteration: `u_{i,1}(x)=-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*g_j(t)dt`
second iteration: `u_{i,2}(x)=(1+h)*u_{i,1}(x)-h\sum_{j=1}^{4}\int_{0}^{x}K_{2,ij}(x,t)*u_{j,1}(t)dt`
third iteration onwards: `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`
I used this online editor to write down the formula (https://www.codecogs.com/latex/eqneditor.php). i am actually looking at 10 iterations, I thought by learning how to do the first two iterations, i can then extend it by myself up to m=10. Thank you for your patience and help. I really appreciate it. I am also so sorry for all the confusion. Should have just posted the original formulas like ages ago.Tue, 31 Dec 2019 03:21:39 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49265#post-id-49265Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49284#post-id-49284Hello, @Sha! I checked the equations, and the code seems to be correct. Just in case, I checked by hand the result for $u_{2,1}$, and I am happy to say that I get the same answer as Sage.
In any case, I have adjusted my code in order to correspond exactly with your equations. Sage uses 0-based indexing, but your equations use 1-based indexing. So, I have left empty the entries `g[0]`, `K2[0,0]` and many similar. Now my code corresponds perfectly with your equations.
I have also made the 10 iterations you wanted to do. Luckily, Sage can do it for us. The code is [here](https://paste.debian.net/1123669/). Take the liberty to change the `NUMBER_OF_ITERATIONS` parameter in order to modify the maximum value for $m$ in $u_{i,m}$.Tue, 31 Dec 2019 19:42:35 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49284#post-id-49284Comment by Nasser for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49156#post-id-49156`u` is not an array. so the error is from `u[1]=x`Tue, 24 Dec 2019 09:42:50 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49156#post-id-49156Comment by Sha for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49157#post-id-49157How can i fix that? What's the correct way of writing it. Thank you.Tue, 24 Dec 2019 09:43:53 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49157#post-id-49157Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49285#post-id-49285By the way, I finally understood why your professor calls them "iterations". You have a recurrence formula! Of course they are iterations! Sorry abut the confusion.
You can check my calculations [here](https://ln2.sync.com/dl/976c1b110/dnij2iyv-zr7nivp3-xwupr94d-mvmakr8w). Please, let me know if I gave you the correct answer, so I can post it as a real answer, and not as a comment. Also, let me know when you check my calculations, since I am sharing it through my private cloud, and it's taking me space. Feel free to download the file.
Oh, finally, notice I have used a function called `full_simplify()` to show the results of the iterations. You can remove them safely, but I recommend using them, since Sage will give you the simplest expression with that.Tue, 31 Dec 2019 19:55:27 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49285#post-id-49285Comment by Nasser for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49158#post-id-49158I think you meant to us subscripted variables? I do not think these are in python but I could be wrong. To make what you want in python `var('x,u'); u=[None]*2; u[1]=x` and this works. this makes empty array `u` of length 2. Since index starts at zero in python. Now you can assign `u[1]`Tue, 24 Dec 2019 10:06:54 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49158#post-id-49158Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49174#post-id-49174Hello, @Sha! Is [this](https://sagecell.sagemath.org/?z=eJyFkkFugzAQRfdI3MG7GIKTeIZk0aonyK5dRilCEXGQCkSWoRy_hgETQquyMvPG874xV10VLEmutal1liQsL-6VNuyu89LYankxeVX6nu_d2BuTvmci1tpVk2q-MqxdBR2ru0p2MZXmH-8Ri22xPu3Ottq-0ijevfdl2Zc_QcgJSULQIQgtXOMEgSDSPhT7CeG516ulXpFedBtcBkUZFGXgchsHVrYX4yruVtCtUGA4y6goo6KMY9tBcJTbQ79j_RRcUXD1S3A1Bj-CRUVqdN5Scgp_hNMu6vM7_1AaYX-A3QxKB2G5ExzEJcQBSnK24hFLZ5VkNTMoHYTnSNJZJVmnixhqRGHUPk4Gp4XlYcFpYakFp4VBi6H5hFnHqEZS25sT-NCATo5LOTo5LuXo5EjyORyuvVncOtj6tdIsZ3nJdFqqjMfBi-8x-zSnnGKKW2gnZUqnJrPzNrr65nkQqqgNRgv1_jNs-PvXtyCk_j8GN5tL9VUXJZfBk8J-gR8Y3Bzz&lang=sage&interacts=eJyLjgUAARUAuQ==) by any chance what you are trying to do? Please, let me know so I can post it as an answer or I can adapt it as necessary.Wed, 25 Dec 2019 00:17:54 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49174#post-id-49174Comment by Sha for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49299#post-id-49299@dsejas wow this is amazing and is done in such a proper way. thank you so much for helping me out. please post the code as an answer. Happy New Year to you all the way from Malaysia.Wed, 01 Jan 2020 13:29:53 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49299#post-id-49299Comment by Sha for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49197#post-id-49197@dsejas yes this looks very good and understandable. But I have 2 questions. I need to keep the h value till the end. and then only sub h=-1. One more thing is that: the integration is in terms of dt where the bounds goes from 0 to x. thank you..Thu, 26 Dec 2019 03:58:16 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49197#post-id-49197Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49203#post-id-49203I modified my code to comply with your requests. You can find it [here](https://sagecell.sagemath.org/?z=eJyFk8GOgjAQhu8mvkNvFqRKZ9DDbvYJvO0ejRJjsJAsYJrC8vhbGCgibpZTma8z3w9Nb7rMWRzfKlPpJI5Zlt9LbdhdZ4Wx1eJqsrJYLpaLNGAmYA37YPVF81XKDGtWXkuqtpZcTan512fAIlusjuHJVpt3GsTb964su_IZhByRJAQtAt_CNY4QCCL1odiNCE-dXs31ivSibXAZFGVQlIHLbeRZ2U4Mq6hdQbtCgf4ko6KMijIO2_aCo9zuu471U3BFwdWL4GoIfgCL8ovRWUPJKfwBjmHQ5Xf-vjTA7gPCCZQOwrwTHMQ5xB5KcjbiEUtnlWQ1EygdhOdI0lklWceD6GtEYdA-TganhfnHgtPCXAtOC70WfXOGyY5BjaS2JyfwYQM6Oc7l6OQ4l6OTI8mnsD_2enbqYOu3UrOMZQXTl0IlPPLelgtmn_qYUUyR-nZSovTFJHbeRpc_PPN8FdhbGdqL6Q0u6vhnZH8H1qnn0_4_xteba_ld5QWX3kuR_Ru_WUcfeg==&lang=sage&interacts=eJyLjgUAARUAuQ==). Is that correct?
One comment: There is no difference if you set `h = -1` at the beginning of the code of if you subs after that.Thu, 26 Dec 2019 05:25:13 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49203#post-id-49203Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49204#post-id-49204I find very odd that you define your functions in terms of `x` and `t`, and then integrate them with respect to `t` from `0` to `x`.
I have to point out that my code doesn't make the substitution of `h`. You can do that with the `subs` command.
Feel free to ask if you have questions.Thu, 26 Dec 2019 05:27:33 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49204#post-id-49204Comment by Sha for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49206#post-id-49206@dsejas actually the formula in HAM (homotopy analysis) research is like this. I have one more last question to ask. in the second iteration, the part v.column(1) is in terms of x which comes from the previous iteration am I right? but in the formula, it actually uses v.column(1) in terms of t. how do I change the variable from the previous iteration (which is in x variable) to t variable, and use it for the integration?Thu, 26 Dec 2019 07:18:08 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49206#post-id-49206Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49211#post-id-49211Hello, @Sha! Homotopy Analysis. Sounds amazing! I am a mathematician, but my area of research is a little far from that.
About your comment, yeah, you're right. I missed that part of `x` and `t`. Sorry. I was almost slept when I wrote my code.
I need a couple of clarifications:
1. When you say "the second iteration", I am assuming you are referring to the "the second loop". Am I right or did I misunderstand something?
2. When the first loop ends, every function is written in terms of `x`. There are some `x`s that were there before the integration, and there are `x`s that came from the integration limits. An I right to assume you want to replace **all** of those `x`s? Because the alternative would be to replace only the `x`s that come from the integration limit.Thu, 26 Dec 2019 17:52:56 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49211#post-id-49211Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49309#post-id-49309I am happy that we finally got it! Happy New Year to you, too!, but from Bolivia. I'll soon post the code as answer.Wed, 01 Jan 2020 16:35:07 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49309#post-id-49309Comment by Sha for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49219#post-id-49219@dsejas 1. when I say the second iteration, I meant `v[i,1]`. the first iteration is represented by `v[i,0]`. The formula for the first iteration is `v[i,0]=-h*sum(integration K2_[ij]*g(t))dt from t=0..x`. to make things easier, I actually changed all `g` that you defined to be in terms of variable `t` and not `x`. For the second iteration, the formula is `v[i,1]=(1+h)*v[i,1](x)-h*sum(integration K2_[ij]*v[j,1](t))dt from t=0..x`. here the term `v[i,1`] and `v[j,1](t)` comes from first iteration. note that `v[i,1]` is in terms of x, but `v[j,1](t)` is in terms of t. both `i` and `j` runs from `1` till `4`.Fri, 27 Dec 2019 09:40:45 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49219#post-id-49219Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49223#post-id-49223Hello, @Sha! You have me extremely confused. I hope you don't mind my following comments, but if you think the are offensive in any sense, I can delete them:
1. What you call "iteration" I call it "loop" (every repetition of a loop is an iteration). That's standard terminology in Computer Science and Math.
2. I also think there is an abuse of notation in your formulas: `v[i,1]=(1+h)*v[i,1](x)-h*sum(integration K2_[ij]*v[j,1](t))dt from t=0..x` should be `v[i,1]=(1+h)*v[i,0](x)-h*sum(integration K2_[ij]*v[j,0](t))dt from t=0..x`.
3. Finally, do you really want `g` to be in terms of `t`? That will radically alter the formula and the results. Note you wrote it in terms of `x` in your code.
Anyway, I am assuming you actually want `g` in terms of `t`. See my comment below.Fri, 27 Dec 2019 16:14:18 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49223#post-id-49223Comment by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49224#post-id-49224You can write `g` in terms of `t` just by rewriting it with `t` instead of `x`. That won't alter anything else than the integral which you have modified in your comment. However, for the sake of following your formulas, I am leaving `g` untouched, and writing it in terms of `t` in the integral defining `v[i,0]`. That can be achieved with the `subs(x==t)` instruction. Also, the same can be done in the integral defining `v[i,1]`, wherever you want to rewrite something in terms of `t`. The resulting code is [here](https://paste.debian.net/1122855/). I hope this time I got it right; otherwise, you could send me your formulas in mathematical notation, so I can translate them to code.Fri, 27 Dec 2019 16:28:26 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49224#post-id-49224Comment by Sha for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49249#post-id-49249@dsejas 1. sorry my supervisor call it as iteration.. so basically in one iteration i will obtain 4 terms. first iteration will give me `v[1,0], v[2,0], v[3,0]` and `v[4,0]`. while for the second iteration, i will obtain 4 terms as well given by `v[1,1], v[2,1], v[3,1]` and `v[4,1]`. for number 2, you're right i made a mistake in the formula. thank you for pointing it out. and for number 3, yes i want `g` to be in terms of `t`. for example, `v[1,0]=-h*(integration(g2(t))+integration(g3(t))).` sorry i need to point out also that `K2[0,3] = 0`. my mistake. `v[2,0]=-h*(integration((x-1)*(g1(t)))+integration((-x)*(g4(t)))+integration((t)*(g2(t)))).` the bounds for integration are all the same from `0` to `x`.Sun, 29 Dec 2019 14:40:51 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49249#post-id-49249Comment by Sha for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49253#post-id-49253@dsejas i have checked the calculation by hands for `v[1,0], v[2,0], v[3,0]` and `v[4,0]` and they are all correct. But I somehow obtained different calculation for the second part which is for `v[1,1], v[2,1], v[3,1]` and `v[4,1]`. for example for `v[1,1]=(1+h)*(v[1,0](x))-h*(integration(K[221]*v[1,0](t)+integrate(K[222]*v[2,0](t))+integrate(K[223]*v[3,0](t))+integrate(K224*v[4,0](t)))`. Hand calculation gave `(1/224x^8-25/168x^7-13/120x^6-1/10x^5+21/8x^4+x^3+1/2x^2-3x)*h^2+(-1/14x^7+37/24x^4-2/3x^3+1/2x^2-3x)*h`.Mon, 30 Dec 2019 03:43:59 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49253#post-id-49253Answer by Emmanuel Charpentier for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?answer=49160#post-id-49160This is a Frequently Asked Question, under very various guises...
The problem is bound to the ambiguity in the uses of the word "variable" :
- A *Python* variable is a *label* (technically some form of pointer) attached to some Python object.
- A *symbolic variable* is a Python object behaving in Sage according to the properties of a mathematical variable in a symbolic expression.
Your first statement, `u,t,x = var('u t x')`, is, ultimately, a shortcut for:
u=SR.var("u")
t=SR.var("t")
x=SR.var("x")
The first statement is itself a shortcut for:
- create a Python object representing a symbolic variable named "u"
- create a label (a Python variable) named "u" attached to this Python object.
Your second statement, `u[1]=x` means *"**Assuming that the Python object pointed to by `u` is a list**, store whatever the Python variable `x` points to in `u`'s second element"*. Since the assumption that `u` is a list *does not hold,* the expression `u[1]` has no meaning, hence the Python interpreter's complaint...
This is explained in detail in ยง1.2 of this [excellent book](http://sagebook.gforge.inria.fr/english.html), whose reading and perusal is currently the best shortcut to Sage I'm aware of.Tue, 24 Dec 2019 10:16:56 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?answer=49160#post-id-49160Answer by dsejas for <p>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].</p>
<pre><code>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]
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?answer=49331#post-id-49331Hello, @Sha! Based on your original question and the exchange of information we had through the comments section, the following code should solve your particular problem for this case:
from __future__ import print_function
NUMBER_OF_ITERATIONS = 10
h, t, x = var('h t x')
g = vector(SR, 5)
print('g(x): ***************************************************')
g[1] = -x^3-x; print(g[1])
g[2] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1; print(g[2])
g[3] = (1/2)*x^6-(31/6)*x^3+2*x^2+3; print(g[3])
g[4] = x^3-5; print(g[4])
print()
K2 = matrix(SR, 5, 5)
print('K2(x,t): ***************************************************')
K2[1,1] = 1; print(K2[1,1])
K2[1,2] = 0; 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; 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*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*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, 0, x); print(u[i,m].full_simplify())
print()
A couple of comments on the previous cod: **1.** The statement `from __future__ import print_function` enables the more modern use of the `print` function. Sage now uses Python3, starting with version 9.0, available since January 1st, 2020. In that case, that line is useless. **2.** I have set the parameter `NUMBER_OF_ITERATIONS` to 10, so Sage iterates your formulas 10 times. You can change that to the particular number you wish. However, I wouldn't recommend using nothing more than 25 if you don't want to wait a lot of time or run out of memory. **3.** Note that instead of the summations in your formulas (instead of using the `sum` function), I have used matrix multiplications (just an optimization). **4.** I have used the function `full_simplify()` simply for Sage to show you the simplified results of your formulas. You can safely remove them. **5.** Although this code uses `g` in terms of the variable`t`, I have written it in terms of `x`, and then I changed the variable with `g.subs(x=t)`. I made that because apparently you need `g(x)` later in your formulas.
I hope this helps!Fri, 03 Jan 2020 00:37:35 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?answer=49331#post-id-49331Comment by Sha for <p>Hello, <a href="/users/1427/sha/">@Sha</a>! Based on your original question and the exchange of information we had through the comments section, the following code should solve your particular problem for this case:</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(x): ***************************************************')
g[1] = -x^3-x; print(g[1])
g[2] = (1/4)*x^5-(1/4)*x^4-(1/2)*x^3-3*x^2-1; print(g[2])
g[3] = (1/2)*x^6-(31/6)*x^3+2*x^2+3; print(g[3])
g[4] = x^3-5; print(g[4])
print()
K2 = matrix(SR, 5, 5)
print('K2(x,t): ***************************************************')
K2[1,1] = 1; print(K2[1,1])
K2[1,2] = 0; 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; 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*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*integrate(K2.row(i)*u.column(m-1).subs(x==t),t, 0, x); print(u[i,m].full_simplify())
print()
</code></pre>
<p>A couple of comments on the previous cod: <strong>1.</strong> The statement <code>from __future__ import print_function</code> enables the more modern use of the <code>print</code> function. Sage now uses Python3, starting with version 9.0, available since January 1st, 2020. In that case, that line is useless. <strong>2.</strong> I have set the parameter <code>NUMBER_OF_ITERATIONS</code> to 10, so Sage iterates your formulas 10 times. You can change that to the particular number you wish. However, I wouldn't recommend using nothing more than 25 if you don't want to wait a lot of time or run out of memory. <strong>3.</strong> Note that instead of the summations in your formulas (instead of using the <code>sum</code> function), I have used matrix multiplications (just an optimization). <strong>4.</strong> I have used the function <code>full_simplify()</code> simply for Sage to show you the simplified results of your formulas. You can safely remove them. <strong>5.</strong> Although this code uses <code>g</code> in terms of the variable<code>t</code>, I have written it in terms of <code>x</code>, and then I changed the variable with <code>g.subs(x=t)</code>. I made that because apparently you need <code>g(x)</code> later in your formulas.</p>
<p>I hope this helps!</p>
https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49340#post-id-49340Thank you so much. This helped me a lot.Fri, 03 Jan 2020 17:00:31 +0100https://ask.sagemath.org/question/49155/error-while-assigning-variables/?comment=49340#post-id-49340