Ask Your Question
0

error while assigning variables

asked 2019-12-23 23:37:57 -0500

Sha gravatar image

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.

edit retag flag offensive close merge delete

Comments

u is not an array. so the error is from u[1]=x

Nasser gravatar imageNasser ( 2019-12-24 02:42:50 -0500 )edit

How can i fix that? What's the correct way of writing it. Thank you.

Sha gravatar imageSha ( 2019-12-24 02:43:53 -0500 )edit

I 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]

Nasser gravatar imageNasser ( 2019-12-24 03:06:54 -0500 )edit

Hello, @Sha! Is this 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.

dsejas gravatar imagedsejas ( 2019-12-24 17:17:54 -0500 )edit

@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..

Sha gravatar imageSha ( 2019-12-25 20:58:16 -0500 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2020-01-02 17:37:35 -0500

dsejas gravatar image

Hello, @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 variablet, 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!

edit flag offensive delete link more

Comments

Thank you so much. This helped me a lot.

Sha gravatar imageSha ( 2020-01-03 10:00:31 -0500 )edit
1

answered 2019-12-24 03:16:56 -0500

Emmanuel Charpentier gravatar image

This 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, whose reading and perusal is currently the best shortcut to Sage I'm aware of.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-12-23 23:37:57 -0500

Seen: 143 times

Last updated: Jan 02