Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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!