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.Sat, 29 Feb 2020 17:10:40 +0100for-loop not computing the valueshttps://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/ Hi. I am quite new to using phyton is SAGE, but what I did here is that I have tried my best to come up with the code based on reading multiple notes on SAGE.
![image description](/upfiles/15826196065614139.jpg)
The values of alpha, n, eta, lambda, h, T, t, kappa, k(x-s) and gamma values are given.
I have tried to do the coding from scratch and this is what I have obtained:
from __future__ import print_function
alpha = 1.2
n = 2
eta = 1.3
lamda = 1
h = 1
T = 1
t = 0.1
h, t, x, s = var('h t x s')
U[t] = 1/3*t^3+1
P[t] = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b[k] = 1
def X(m):
if m <= 1:
return 0
else:
return 1
k[x,s]=s-x
gamma[z]=integrate(x^(z-1)*e^(-x),x, 0, infinity)
u[0]=1
for m in range(1,4):
u[m] = sum(b[k]/factorial(k)*t^k,k,0,n-1) + X[m]*u[m-1]-X[m]*sum(𝚍𝚎𝚛𝚒𝚟𝚊𝚝𝚒𝚟𝚎(u[m-1][0],t,k)/factorial(k)*t^k,k,0,n-1)+h/gamma[eta-alpha]*integrate((t-tau)^(eta-alpha-1)*u[m-1].subs(t=tau)*P[t].subs(t=tau),tau, 0, t); print(u[m].full_simplify())
UU=u[1]+u[2]+u[3]+u[4]:
What I am trying to achieve here is the values of `u_1, u_2, u_3, u_4`. Then calculate `UU = u_1 + u_2 + u_3 + u_4`.
At the moment, I am not getting the answer. Not sure what is causing the error. It took me so much time and effort to come up with this from scratch, hope someone can help me indicate what's wrong with this coding. Thank you in advance.Tue, 25 Feb 2020 09:53:13 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/Comment by dsejas for <p>Hi. I am quite new to using phyton is SAGE, but what I did here is that I have tried my best to come up with the code based on reading multiple notes on SAGE.</p>
<p><img alt="image description" src="/upfiles/15826196065614139.jpg"></p>
<p>The values of alpha, n, eta, lambda, h, T, t, kappa, k(x-s) and gamma values are given.</p>
<p>I have tried to do the coding from scratch and this is what I have obtained:</p>
<pre><code>from __future__ import print_function
alpha = 1.2
n = 2
eta = 1.3
lamda = 1
h = 1
T = 1
t = 0.1
h, t, x, s = var('h t x s')
U[t] = 1/3*t^3+1
P[t] = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b[k] = 1
def X(m):
if m <= 1:
return 0
else:
return 1
k[x,s]=s-x
gamma[z]=integrate(x^(z-1)*e^(-x),x, 0, infinity)
u[0]=1
for m in range(1,4):
u[m] = sum(b[k]/factorial(k)*t^k,k,0,n-1) + X[m]*u[m-1]-X[m]*sum(𝚍𝚎𝚛𝚒𝚟𝚊𝚝𝚒𝚟𝚎(u[m-1][0],t,k)/factorial(k)*t^k,k,0,n-1)+h/gamma[eta-alpha]*integrate((t-tau)^(eta-alpha-1)*u[m-1].subs(t=tau)*P[t].subs(t=tau),tau, 0, t); print(u[m].full_simplify())
UU=u[1]+u[2]+u[3]+u[4]:
</code></pre>
<p>What I am trying to achieve here is the values of <code>u_1, u_2, u_3, u_4</code>. Then calculate <code>UU = u_1 + u_2 + u_3 + u_4</code>. </p>
<p>At the moment, I am not getting the answer. Not sure what is causing the error. It took me so much time and effort to come up with this from scratch, hope someone can help me indicate what's wrong with this coding. Thank you in advance.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50063#post-id-50063Hello, @Sha! I have corrected some errors in your code. However, there seems to be a couple of problems in Sage. On one side, the last integral in the definition of `u[m]` can't be computed directly, so I had to use a couple of auxiliary variables. This helped, but the loop only computes one iteration, and then freezes until Sage gives up.
I suppose there are some optimizations that can be carried out in the loop. Let me try for a couple of days and I'll let you know. Do you have a deadline for this?Tue, 25 Feb 2020 18:24:56 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50063#post-id-50063Comment by dsejas for <p>Hi. I am quite new to using phyton is SAGE, but what I did here is that I have tried my best to come up with the code based on reading multiple notes on SAGE.</p>
<p><img alt="image description" src="/upfiles/15826196065614139.jpg"></p>
<p>The values of alpha, n, eta, lambda, h, T, t, kappa, k(x-s) and gamma values are given.</p>
<p>I have tried to do the coding from scratch and this is what I have obtained:</p>
<pre><code>from __future__ import print_function
alpha = 1.2
n = 2
eta = 1.3
lamda = 1
h = 1
T = 1
t = 0.1
h, t, x, s = var('h t x s')
U[t] = 1/3*t^3+1
P[t] = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b[k] = 1
def X(m):
if m <= 1:
return 0
else:
return 1
k[x,s]=s-x
gamma[z]=integrate(x^(z-1)*e^(-x),x, 0, infinity)
u[0]=1
for m in range(1,4):
u[m] = sum(b[k]/factorial(k)*t^k,k,0,n-1) + X[m]*u[m-1]-X[m]*sum(𝚍𝚎𝚛𝚒𝚟𝚊𝚝𝚒𝚟𝚎(u[m-1][0],t,k)/factorial(k)*t^k,k,0,n-1)+h/gamma[eta-alpha]*integrate((t-tau)^(eta-alpha-1)*u[m-1].subs(t=tau)*P[t].subs(t=tau),tau, 0, t); print(u[m].full_simplify())
UU=u[1]+u[2]+u[3]+u[4]:
</code></pre>
<p>What I am trying to achieve here is the values of <code>u_1, u_2, u_3, u_4</code>. Then calculate <code>UU = u_1 + u_2 + u_3 + u_4</code>. </p>
<p>At the moment, I am not getting the answer. Not sure what is causing the error. It took me so much time and effort to come up with this from scratch, hope someone can help me indicate what's wrong with this coding. Thank you in advance.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50064#post-id-50064My code is [here](https://sagecell.sagemath.org/?z=eJx9Uttq3DAQfTf4Hwb6EMkrrS-7aUrbPLSQQh5yYbOBQlgbJZF37fiyyFJw8wmF5qGPCaG_2E_oyHJDSy-G8WhuZ45mlKu2hizLjTZKZhkU9bZVGraqaDR6mytdtI3v-d7x-dH7g0V28iE7XB4s3i0PT47PYB_mNiaq7UagEU8T32vwgEpq55n5XiXq68HwvY1TS6deaNTRNLYYt0KRnQ1o6KGDErQwO9T3zommNjecBTqdTTDz1HmSQPMBNyBcp_MwmU90uhvGu2jthXHyEs1X4V6CGJfZjWvne9cyh4-kpq99D_ArcqjhLcZG235K4iQaiJxHVp38Mzhg3ZCedZZKx3u8ylrUtSB31oGjk2sltCR9Su54TAOZEt5T1jOIGLQttfUGM2_llW4VOVsw-Nt8Mc9cRCvHPm8Vsi0aUKJZSxL_o2RkK0xPcIa_0yGaW19KcDl8WJolR8xFzePVtDOXHdH7NoMGp0M1s79nwNiNfoCmwIdDNIYRw_LsTE1w3mEu7MUKUZGS4uZKVrKINdgNJsMCAteTD2db9P3p4QvKPcojyleUbyifUZ5-se9Hskyzkv5kHNH_9tuEbjfPl6bBeJk37p1bzNU0N1WVdfj-qyL_RCj9Abob6TI=&lang=sage&interacts=eJyLjgUAARUAuQ==)Tue, 25 Feb 2020 18:25:22 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50064#post-id-50064Comment by John Palmieri for <p>Hi. I am quite new to using phyton is SAGE, but what I did here is that I have tried my best to come up with the code based on reading multiple notes on SAGE.</p>
<p><img alt="image description" src="/upfiles/15826196065614139.jpg"></p>
<p>The values of alpha, n, eta, lambda, h, T, t, kappa, k(x-s) and gamma values are given.</p>
<p>I have tried to do the coding from scratch and this is what I have obtained:</p>
<pre><code>from __future__ import print_function
alpha = 1.2
n = 2
eta = 1.3
lamda = 1
h = 1
T = 1
t = 0.1
h, t, x, s = var('h t x s')
U[t] = 1/3*t^3+1
P[t] = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b[k] = 1
def X(m):
if m <= 1:
return 0
else:
return 1
k[x,s]=s-x
gamma[z]=integrate(x^(z-1)*e^(-x),x, 0, infinity)
u[0]=1
for m in range(1,4):
u[m] = sum(b[k]/factorial(k)*t^k,k,0,n-1) + X[m]*u[m-1]-X[m]*sum(𝚍𝚎𝚛𝚒𝚟𝚊𝚝𝚒𝚟𝚎(u[m-1][0],t,k)/factorial(k)*t^k,k,0,n-1)+h/gamma[eta-alpha]*integrate((t-tau)^(eta-alpha-1)*u[m-1].subs(t=tau)*P[t].subs(t=tau),tau, 0, t); print(u[m].full_simplify())
UU=u[1]+u[2]+u[3]+u[4]:
</code></pre>
<p>What I am trying to achieve here is the values of <code>u_1, u_2, u_3, u_4</code>. Then calculate <code>UU = u_1 + u_2 + u_3 + u_4</code>. </p>
<p>At the moment, I am not getting the answer. Not sure what is causing the error. It took me so much time and effort to come up with this from scratch, hope someone can help me indicate what's wrong with this coding. Thank you in advance.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50065#post-id-50065`U` and `P` look like functions, so you should use parentheses when you define them. `b` and `u` look like lists or dictionaries or vectors, and you need to define them as such before setting entries in them. `u[0]=1` will give an error, for example, unless you have first done something like `u={}`. Also, note that `for m in range(1,4):` will loop over values `m=1`, `m=2`, and `m=3`, but not `m=4`.Tue, 25 Feb 2020 18:39:07 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50065#post-id-50065Comment by Sha for <p>Hi. I am quite new to using phyton is SAGE, but what I did here is that I have tried my best to come up with the code based on reading multiple notes on SAGE.</p>
<p><img alt="image description" src="/upfiles/15826196065614139.jpg"></p>
<p>The values of alpha, n, eta, lambda, h, T, t, kappa, k(x-s) and gamma values are given.</p>
<p>I have tried to do the coding from scratch and this is what I have obtained:</p>
<pre><code>from __future__ import print_function
alpha = 1.2
n = 2
eta = 1.3
lamda = 1
h = 1
T = 1
t = 0.1
h, t, x, s = var('h t x s')
U[t] = 1/3*t^3+1
P[t] = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b[k] = 1
def X(m):
if m <= 1:
return 0
else:
return 1
k[x,s]=s-x
gamma[z]=integrate(x^(z-1)*e^(-x),x, 0, infinity)
u[0]=1
for m in range(1,4):
u[m] = sum(b[k]/factorial(k)*t^k,k,0,n-1) + X[m]*u[m-1]-X[m]*sum(𝚍𝚎𝚛𝚒𝚟𝚊𝚝𝚒𝚟𝚎(u[m-1][0],t,k)/factorial(k)*t^k,k,0,n-1)+h/gamma[eta-alpha]*integrate((t-tau)^(eta-alpha-1)*u[m-1].subs(t=tau)*P[t].subs(t=tau),tau, 0, t); print(u[m].full_simplify())
UU=u[1]+u[2]+u[3]+u[4]:
</code></pre>
<p>What I am trying to achieve here is the values of <code>u_1, u_2, u_3, u_4</code>. Then calculate <code>UU = u_1 + u_2 + u_3 + u_4</code>. </p>
<p>At the moment, I am not getting the answer. Not sure what is causing the error. It took me so much time and effort to come up with this from scratch, hope someone can help me indicate what's wrong with this coding. Thank you in advance.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50075#post-id-50075@dsejas Hi diego. thank you for your reply. I am actually trying to figure out the formula too.. just to check if there's anything wrong with it. I don't have a deadline with this. Thank you for your help. Let me too re check the calculation manually.Wed, 26 Feb 2020 14:01:46 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50075#post-id-50075Comment by Sha for <p>Hi. I am quite new to using phyton is SAGE, but what I did here is that I have tried my best to come up with the code based on reading multiple notes on SAGE.</p>
<p><img alt="image description" src="/upfiles/15826196065614139.jpg"></p>
<p>The values of alpha, n, eta, lambda, h, T, t, kappa, k(x-s) and gamma values are given.</p>
<p>I have tried to do the coding from scratch and this is what I have obtained:</p>
<pre><code>from __future__ import print_function
alpha = 1.2
n = 2
eta = 1.3
lamda = 1
h = 1
T = 1
t = 0.1
h, t, x, s = var('h t x s')
U[t] = 1/3*t^3+1
P[t] = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b[k] = 1
def X(m):
if m <= 1:
return 0
else:
return 1
k[x,s]=s-x
gamma[z]=integrate(x^(z-1)*e^(-x),x, 0, infinity)
u[0]=1
for m in range(1,4):
u[m] = sum(b[k]/factorial(k)*t^k,k,0,n-1) + X[m]*u[m-1]-X[m]*sum(𝚍𝚎𝚛𝚒𝚟𝚊𝚝𝚒𝚟𝚎(u[m-1][0],t,k)/factorial(k)*t^k,k,0,n-1)+h/gamma[eta-alpha]*integrate((t-tau)^(eta-alpha-1)*u[m-1].subs(t=tau)*P[t].subs(t=tau),tau, 0, t); print(u[m].full_simplify())
UU=u[1]+u[2]+u[3]+u[4]:
</code></pre>
<p>What I am trying to achieve here is the values of <code>u_1, u_2, u_3, u_4</code>. Then calculate <code>UU = u_1 + u_2 + u_3 + u_4</code>. </p>
<p>At the moment, I am not getting the answer. Not sure what is causing the error. It took me so much time and effort to come up with this from scratch, hope someone can help me indicate what's wrong with this coding. Thank you in advance.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50076#post-id-50076@johnpalmieri thank you for the feedback John. I really appreciate it.Wed, 26 Feb 2020 14:02:41 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50076#post-id-50076Answer by Juanjo for <p>Hi. I am quite new to using phyton is SAGE, but what I did here is that I have tried my best to come up with the code based on reading multiple notes on SAGE.</p>
<p><img alt="image description" src="/upfiles/15826196065614139.jpg"></p>
<p>The values of alpha, n, eta, lambda, h, T, t, kappa, k(x-s) and gamma values are given.</p>
<p>I have tried to do the coding from scratch and this is what I have obtained:</p>
<pre><code>from __future__ import print_function
alpha = 1.2
n = 2
eta = 1.3
lamda = 1
h = 1
T = 1
t = 0.1
h, t, x, s = var('h t x s')
U[t] = 1/3*t^3+1
P[t] = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b[k] = 1
def X(m):
if m <= 1:
return 0
else:
return 1
k[x,s]=s-x
gamma[z]=integrate(x^(z-1)*e^(-x),x, 0, infinity)
u[0]=1
for m in range(1,4):
u[m] = sum(b[k]/factorial(k)*t^k,k,0,n-1) + X[m]*u[m-1]-X[m]*sum(𝚍𝚎𝚛𝚒𝚟𝚊𝚝𝚒𝚟𝚎(u[m-1][0],t,k)/factorial(k)*t^k,k,0,n-1)+h/gamma[eta-alpha]*integrate((t-tau)^(eta-alpha-1)*u[m-1].subs(t=tau)*P[t].subs(t=tau),tau, 0, t); print(u[m].full_simplify())
UU=u[1]+u[2]+u[3]+u[4]:
</code></pre>
<p>What I am trying to achieve here is the values of <code>u_1, u_2, u_3, u_4</code>. Then calculate <code>UU = u_1 + u_2 + u_3 + u_4</code>. </p>
<p>At the moment, I am not getting the answer. Not sure what is causing the error. It took me so much time and effort to come up with this from scratch, hope someone can help me indicate what's wrong with this coding. Thank you in advance.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?answer=50078#post-id-50078In your handwritten note, one sees that $u_0=1$ and that, for $n\geq 1$,
$$u_n(t)=\sum_{k=0}^{n-1} \frac{b_k}{k!} t^k + \chi_n u_{n-1}(t)
-\chi_n \sum_{k=0}^{n-1} \frac{u_{n-1}^{(k)}(0)}{k!} t^k
+\frac{h}{\Gamma(\eta-\alpha)}
\int_0^t (t-\tau)^{n-\alpha-1} u_{n-1}(\tau) P(\tau) \,d\tau.$$
This expression can be rewritten as follows:
$$u_n(t)=\sum_{k=0}^{n-1} \frac{b_k}{k!} t^k + \chi_n \bigl(u_{n-1}(t) -q_{n-1}(t)\bigr)
+\frac{h}{\Gamma(\eta-\alpha)} I_n(t),$$
where $q_{n-1}$ is the MacLaurin polinomial of $u_{n-1}$ of order $n-1$ and
$$I_n(t) = \int_0^t (t-\tau)^{n-\alpha-1} u_{n-1}(\tau) P(\tau) \,d\tau.$$
You have defined $\chi_n$ as $\chi_n=1$ for $n\geq 2$, as well as $\chi_1=0$ to avoid the term
$u_0(t) -q_0(t)$ in the expression of $u_1$. However, since $u_0(t)=1$, it is obvious that $q_0(t)=1$. Hence $u_0(t) -q_0(t)=0$. This implies that, in fact, you do not need $\chi_n$.
Let us consider $I_n(t)$. I think that $n-\alpha-1$ should be greater than $-1$ for this integral to be convergent. Since $\alpha=1.2$, there is a problem in the case $n=1$.
Finally, what is the sense of $U(t)=t^3/3+1$? This function does not appear in the computation of any $u_n$.
Taking all the above facts into account, I have rewritten your code. I do not define $\chi_n$ (not needed), I ignore $U(t)$ and I take a value of $\alpha$ so that $n-\alpha-1>-1$ for any $n$, such as $\alpha=0.8$. It seems that SageMath does not like, at least in this case, float numbers as exponents, so I write $\alpha=8/10$ instead of $\alpha=0.8$. Here it is the code:
var("t,tau")
lamda = 1
P(t) = 2*t - lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
h = 1
eta = 1.3
alpha = 8/10
nmax = 4
b = nmax*[1]
forget()
assume(t>0)
u = [SR(1).function(t)]
for n in [1..nmax]:
un = sum(b[k]*t^k/factorial(k) for k in [0..n-1])
un += u[n-1](t) - taylor(u[n-1](t), t, 0, n-1)
un += (h/gamma(eta-alpha))*integrate((t-tau)^(n-alpha-1)*u[n-1](tau)*P(tau),tau,0,t)
u.append(un.function(t))
UU(t) = sum(u[k](t) for k in [1..nmax])
forget()
Observe that I introduce the variable `nmax`, for the case being you want to compute more functions $u_n$. Likewise, SageMath needs some help to integrate, so I added the assumption that $t>0$. The `forget` commands delete the effect of any `assume` command; thus, SageMath does not get confused by any previous `assume` you could been using before, and stops using the assumption $t>0$ once $UU$ is computed.
The main loop provides a list with the functions $u_0$, $u_1$,...,$u_{nmax}$. They are symbolic callable functions, so to evaluate, say, $u_3$ at $t=0.5$, you simply write `u[3](0.5)`.
The code also defines $UU$. You can show it
sage: show(UU(t))
[Very large output]
evaluate it at, say, $t=0.9$
sage: UU(0.9).n()
19.7969629979363
or plot it
sage: plot(UU(t), (t,0,1))
[Long time]
![image description](/upfiles/15827304804569542.png)Wed, 26 Feb 2020 16:21:59 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?answer=50078#post-id-50078Answer by dsejas for <p>Hi. I am quite new to using phyton is SAGE, but what I did here is that I have tried my best to come up with the code based on reading multiple notes on SAGE.</p>
<p><img alt="image description" src="/upfiles/15826196065614139.jpg"></p>
<p>The values of alpha, n, eta, lambda, h, T, t, kappa, k(x-s) and gamma values are given.</p>
<p>I have tried to do the coding from scratch and this is what I have obtained:</p>
<pre><code>from __future__ import print_function
alpha = 1.2
n = 2
eta = 1.3
lamda = 1
h = 1
T = 1
t = 0.1
h, t, x, s = var('h t x s')
U[t] = 1/3*t^3+1
P[t] = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b[k] = 1
def X(m):
if m <= 1:
return 0
else:
return 1
k[x,s]=s-x
gamma[z]=integrate(x^(z-1)*e^(-x),x, 0, infinity)
u[0]=1
for m in range(1,4):
u[m] = sum(b[k]/factorial(k)*t^k,k,0,n-1) + X[m]*u[m-1]-X[m]*sum(𝚍𝚎𝚛𝚒𝚟𝚊𝚝𝚒𝚟𝚎(u[m-1][0],t,k)/factorial(k)*t^k,k,0,n-1)+h/gamma[eta-alpha]*integrate((t-tau)^(eta-alpha-1)*u[m-1].subs(t=tau)*P[t].subs(t=tau),tau, 0, t); print(u[m].full_simplify())
UU=u[1]+u[2]+u[3]+u[4]:
</code></pre>
<p>What I am trying to achieve here is the values of <code>u_1, u_2, u_3, u_4</code>. Then calculate <code>UU = u_1 + u_2 + u_3 + u_4</code>. </p>
<p>At the moment, I am not getting the answer. Not sure what is causing the error. It took me so much time and effort to come up with this from scratch, hope someone can help me indicate what's wrong with this coding. Thank you in advance.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?answer=50114#post-id-50114Hello, @sha! I am quite fascinated by @juanjo's answer. I suggest you to take a careful look at his reasoning and derivations. However, I would also like to try to answer your post, since I got different results (and formulation, see below), and I have some tiny improvements.
But first, I would like to point out that I separated every term in your definition of `u[m]`, and I experimented with each one of them. I found out that the computation stops after the first iteration do to the integral
$$\int_0^t(t-\tau)^{\eta-\alpha-1}u_{m-1}(\tau)P(\tau)\;d\tau$$
The problem is that $\eta-\alpha-1=-0.9$, and Sage seems to be unwilling to compute the integral in the second iteration in that case. (I really don't know why!) I tried to mitigate this problem by computing the indefinite integral first, and that helped a little bit. That's the reason why I used the `aux` (the indefinite integral) and `aux1` (the definite integral) temporary variables in the code that I sent you in my previous comment. However, it didn't help much.
Once again, I don't know why, but using rational numbers instead of floating point numbers did work. (This is also pointed out by @juanjo's answer.) As a consequence, I have replaced the definitions of $\alpha$ and $\eta$ using rational numbers:
alpha = 6/5
eta = 13/10
That tells Sage to use rational numbers, not floating point numbers, and that should avoid the problems with the integral.
Anyway, in the following code, I have deleted the kernel `k` (since you don't use it in your calculations). I have also deleted your definition of the `gamma` function, since it is predefined in Sage. On the other hand, notice that
$$\sum_{k=0}^{n-1}\frac{b_k}{k!}t^k$$
and
$$\sum_{k=0}^{n-1}\frac{u_{m-1}^{(k)}}{k!}t^k$$
are the Taylor Polynomials of degree $n-1$ in $t=0$ of the functions $b_ke^t$ and $u_{m-1}(t)$.
Now, I think, @juanjo made a mistake when he said that $\chi$ is unnecessary. I think the problem is that he wrote the original formula (which was written in terms of $m$ and $n$) in terms of just $n$. If I understood correctly, $n$ is a constant and $m$ is your iteration variable (am I right?). If that is the case, the $\chi$ is needed in front of `u[m-1]`, but not in front of your second summation, which is the Taylor Polynomial for $u_{m-1}(t)$ (as I just explained), which turns out to be zero for $m=1$, since $u_0(t)=1$. However, notice that $\chi(m)$ is equivalently defined as $\operatorname{sign}(m-1)$, so I used `sign(m-1)`.
Finally, Sage asked me to specify if $t>0$ or $t<0$ (so it can compute the integral). I assumed $t>0$. That's the command `assume(t>0)`.
If I did everything right, the following code should work:
from __future__ import print_function
NUMBER_OF_ITERATIONS = 4
alpha = 6/5
n = 2
eta = 13/10
lamda = 1
h = 1
T = 1
var('h t x s j tau')
U(t) = 1/3*t^3+1
P(t) = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b_k = 1
u = vector(SR, NUMBER_OF_ITERATIONS)
u[0] = 1
U = u[0]
assume(t>0)
for m in range(1, NUMBER_OF_ITERATIONS):
u[m] = b_k*taylor(e^t, t, 0, n-1) + sign(m-1)*u[m-1] - taylor(u[m-1], t, 0, n-1) + h/gamma(eta-alpha)*integrate((t-tau)^(eta-alpha-1)*(u[m-1].subs(t=tau))*P(tau),tau, t, 0); print(u[m])
U += u[m]
forget()
show(U(t))
print('U(0.1) =', N(U(t=0.1,h=1)))
plot(U(t).subs(h=1), t, 0, 1)
![image description](/upfiles/15829515217141605.png)
I hope this helps! As always, if the answer needs to be modified or re-explained you can ask as a comment or send me a mail.Sat, 29 Feb 2020 05:47:14 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?answer=50114#post-id-50114Comment by Juanjo for <p>Hello, <a href="/users/1427/sha/">@Sha</a>! I am quite fascinated by <a href="/users/25859/juanjo/">@Juanjo</a>'s answer. I suggest you to take a careful look at his reasoning and derivations. However, I would also like to try to answer your post, since I got different results (and formulation, see below), and I have some tiny improvements.</p>
<p>But first, I would like to point out that I separated every term in your definition of <code>u[m]</code>, and I experimented with each one of them. I found out that the computation stops after the first iteration do to the integral</p>
<p>$$\int_0^t(t-\tau)^{\eta-\alpha-1}u_{m-1}(\tau)P(\tau)\;d\tau$$</p>
<p>The problem is that $\eta-\alpha-1=-0.9$, and Sage seems to be unwilling to compute the integral in the second iteration in that case. (I really don't know why!) I tried to mitigate this problem by computing the indefinite integral first, and that helped a little bit. That's the reason why I used the <code>aux</code> (the indefinite integral) and <code>aux1</code> (the definite integral) temporary variables in the code that I sent you in my previous comment. However, it didn't help much.</p>
<p>Once again, I don't know why, but using rational numbers instead of floating point numbers did work. (This is also pointed out by <a href="/users/25859/juanjo/">@Juanjo</a>'s answer.) As a consequence, I have replaced the definitions of $\alpha$ and $\eta$ using rational numbers:</p>
<pre><code>alpha = 6/5
eta = 13/10
</code></pre>
<p>That tells Sage to use rational numbers, not floating point numbers, and that should avoid the problems with the integral.</p>
<p>Anyway, in the following code, I have deleted the kernel <code>k</code> (since you don't use it in your calculations). I have also deleted your definition of the <code>gamma</code> function, since it is predefined in Sage. On the other hand, notice that</p>
<p>$$\sum_{k=0}^{n-1}\frac{b_k}{k!}t^k$$</p>
<p>and</p>
<p>$$\sum_{k=0}^{n-1}\frac{u_{m-1}^{(k)}}{k!}t^k$$</p>
<p>are the Taylor Polynomials of degree $n-1$ in $t=0$ of the functions $b_ke^t$ and $u_{m-1}(t)$.</p>
<p>Now, I think, <a href="/users/25859/juanjo/">@Juanjo</a> made a mistake when he said that $\chi$ is unnecessary. I think the problem is that he wrote the original formula (which was written in terms of $m$ and $n$) in terms of just $n$. If I understood correctly, $n$ is a constant and $m$ is your iteration variable (am I right?). If that is the case, the $\chi$ is needed in front of <code>u[m-1]</code>, but not in front of your second summation, which is the Taylor Polynomial for $u_{m-1}(t)$ (as I just explained), which turns out to be zero for $m=1$, since $u_0(t)=1$. However, notice that $\chi(m)$ is equivalently defined as $\operatorname{sign}(m-1)$, so I used <code>sign(m-1)</code>.</p>
<p>Finally, Sage asked me to specify if $t>0$ or $t<0$ (so it can compute the integral). I assumed $t>0$. That's the command <code>assume(t>0)</code>.</p>
<p>If I did everything right, the following code should work:</p>
<pre><code>from __future__ import print_function
NUMBER_OF_ITERATIONS = 4
alpha = 6/5
n = 2
eta = 13/10
lamda = 1
h = 1
T = 1
var('h t x s j tau')
U(t) = 1/3*t^3+1
P(t) = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b_k = 1
u = vector(SR, NUMBER_OF_ITERATIONS)
u[0] = 1
U = u[0]
assume(t>0)
for m in range(1, NUMBER_OF_ITERATIONS):
u[m] = b_k*taylor(e^t, t, 0, n-1) + sign(m-1)*u[m-1] - taylor(u[m-1], t, 0, n-1) + h/gamma(eta-alpha)*integrate((t-tau)^(eta-alpha-1)*(u[m-1].subs(t=tau))*P(tau),tau, t, 0); print(u[m])
U += u[m]
forget()
show(U(t))
print('U(0.1) =', N(U(t=0.1,h=1)))
plot(U(t).subs(h=1), t, 0, 1)
</code></pre>
<p><img alt="image description" src="/upfiles/15829515217141605.png"></p>
<p>I hope this helps! As always, if the answer needs to be modified or re-explained you can ask as a comment or send me a mail.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50121#post-id-50121It is often difficult to distinguish between $n$ and $m$ in a handwritten text. Perhaps this has been my case. Anyway, it would be convenient that @SHA clarifies the formulae, rewriting them with LaTeX... or a more careful calligraphy ;-))Sat, 29 Feb 2020 16:20:29 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50121#post-id-50121Comment by dsejas for <p>Hello, <a href="/users/1427/sha/">@Sha</a>! I am quite fascinated by <a href="/users/25859/juanjo/">@Juanjo</a>'s answer. I suggest you to take a careful look at his reasoning and derivations. However, I would also like to try to answer your post, since I got different results (and formulation, see below), and I have some tiny improvements.</p>
<p>But first, I would like to point out that I separated every term in your definition of <code>u[m]</code>, and I experimented with each one of them. I found out that the computation stops after the first iteration do to the integral</p>
<p>$$\int_0^t(t-\tau)^{\eta-\alpha-1}u_{m-1}(\tau)P(\tau)\;d\tau$$</p>
<p>The problem is that $\eta-\alpha-1=-0.9$, and Sage seems to be unwilling to compute the integral in the second iteration in that case. (I really don't know why!) I tried to mitigate this problem by computing the indefinite integral first, and that helped a little bit. That's the reason why I used the <code>aux</code> (the indefinite integral) and <code>aux1</code> (the definite integral) temporary variables in the code that I sent you in my previous comment. However, it didn't help much.</p>
<p>Once again, I don't know why, but using rational numbers instead of floating point numbers did work. (This is also pointed out by <a href="/users/25859/juanjo/">@Juanjo</a>'s answer.) As a consequence, I have replaced the definitions of $\alpha$ and $\eta$ using rational numbers:</p>
<pre><code>alpha = 6/5
eta = 13/10
</code></pre>
<p>That tells Sage to use rational numbers, not floating point numbers, and that should avoid the problems with the integral.</p>
<p>Anyway, in the following code, I have deleted the kernel <code>k</code> (since you don't use it in your calculations). I have also deleted your definition of the <code>gamma</code> function, since it is predefined in Sage. On the other hand, notice that</p>
<p>$$\sum_{k=0}^{n-1}\frac{b_k}{k!}t^k$$</p>
<p>and</p>
<p>$$\sum_{k=0}^{n-1}\frac{u_{m-1}^{(k)}}{k!}t^k$$</p>
<p>are the Taylor Polynomials of degree $n-1$ in $t=0$ of the functions $b_ke^t$ and $u_{m-1}(t)$.</p>
<p>Now, I think, <a href="/users/25859/juanjo/">@Juanjo</a> made a mistake when he said that $\chi$ is unnecessary. I think the problem is that he wrote the original formula (which was written in terms of $m$ and $n$) in terms of just $n$. If I understood correctly, $n$ is a constant and $m$ is your iteration variable (am I right?). If that is the case, the $\chi$ is needed in front of <code>u[m-1]</code>, but not in front of your second summation, which is the Taylor Polynomial for $u_{m-1}(t)$ (as I just explained), which turns out to be zero for $m=1$, since $u_0(t)=1$. However, notice that $\chi(m)$ is equivalently defined as $\operatorname{sign}(m-1)$, so I used <code>sign(m-1)</code>.</p>
<p>Finally, Sage asked me to specify if $t>0$ or $t<0$ (so it can compute the integral). I assumed $t>0$. That's the command <code>assume(t>0)</code>.</p>
<p>If I did everything right, the following code should work:</p>
<pre><code>from __future__ import print_function
NUMBER_OF_ITERATIONS = 4
alpha = 6/5
n = 2
eta = 13/10
lamda = 1
h = 1
T = 1
var('h t x s j tau')
U(t) = 1/3*t^3+1
P(t) = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b_k = 1
u = vector(SR, NUMBER_OF_ITERATIONS)
u[0] = 1
U = u[0]
assume(t>0)
for m in range(1, NUMBER_OF_ITERATIONS):
u[m] = b_k*taylor(e^t, t, 0, n-1) + sign(m-1)*u[m-1] - taylor(u[m-1], t, 0, n-1) + h/gamma(eta-alpha)*integrate((t-tau)^(eta-alpha-1)*(u[m-1].subs(t=tau))*P(tau),tau, t, 0); print(u[m])
U += u[m]
forget()
show(U(t))
print('U(0.1) =', N(U(t=0.1,h=1)))
plot(U(t).subs(h=1), t, 0, 1)
</code></pre>
<p><img alt="image description" src="/upfiles/15829515217141605.png"></p>
<p>I hope this helps! As always, if the answer needs to be modified or re-explained you can ask as a comment or send me a mail.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50115#post-id-50115I am really worried that $m$ and $n$ could be the same variable, could you confirm that $n=2$ is a constant and $m$ is the iteration variable?Sat, 29 Feb 2020 05:48:06 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50115#post-id-50115Comment by dsejas for <p>Hello, <a href="/users/1427/sha/">@Sha</a>! I am quite fascinated by <a href="/users/25859/juanjo/">@Juanjo</a>'s answer. I suggest you to take a careful look at his reasoning and derivations. However, I would also like to try to answer your post, since I got different results (and formulation, see below), and I have some tiny improvements.</p>
<p>But first, I would like to point out that I separated every term in your definition of <code>u[m]</code>, and I experimented with each one of them. I found out that the computation stops after the first iteration do to the integral</p>
<p>$$\int_0^t(t-\tau)^{\eta-\alpha-1}u_{m-1}(\tau)P(\tau)\;d\tau$$</p>
<p>The problem is that $\eta-\alpha-1=-0.9$, and Sage seems to be unwilling to compute the integral in the second iteration in that case. (I really don't know why!) I tried to mitigate this problem by computing the indefinite integral first, and that helped a little bit. That's the reason why I used the <code>aux</code> (the indefinite integral) and <code>aux1</code> (the definite integral) temporary variables in the code that I sent you in my previous comment. However, it didn't help much.</p>
<p>Once again, I don't know why, but using rational numbers instead of floating point numbers did work. (This is also pointed out by <a href="/users/25859/juanjo/">@Juanjo</a>'s answer.) As a consequence, I have replaced the definitions of $\alpha$ and $\eta$ using rational numbers:</p>
<pre><code>alpha = 6/5
eta = 13/10
</code></pre>
<p>That tells Sage to use rational numbers, not floating point numbers, and that should avoid the problems with the integral.</p>
<p>Anyway, in the following code, I have deleted the kernel <code>k</code> (since you don't use it in your calculations). I have also deleted your definition of the <code>gamma</code> function, since it is predefined in Sage. On the other hand, notice that</p>
<p>$$\sum_{k=0}^{n-1}\frac{b_k}{k!}t^k$$</p>
<p>and</p>
<p>$$\sum_{k=0}^{n-1}\frac{u_{m-1}^{(k)}}{k!}t^k$$</p>
<p>are the Taylor Polynomials of degree $n-1$ in $t=0$ of the functions $b_ke^t$ and $u_{m-1}(t)$.</p>
<p>Now, I think, <a href="/users/25859/juanjo/">@Juanjo</a> made a mistake when he said that $\chi$ is unnecessary. I think the problem is that he wrote the original formula (which was written in terms of $m$ and $n$) in terms of just $n$. If I understood correctly, $n$ is a constant and $m$ is your iteration variable (am I right?). If that is the case, the $\chi$ is needed in front of <code>u[m-1]</code>, but not in front of your second summation, which is the Taylor Polynomial for $u_{m-1}(t)$ (as I just explained), which turns out to be zero for $m=1$, since $u_0(t)=1$. However, notice that $\chi(m)$ is equivalently defined as $\operatorname{sign}(m-1)$, so I used <code>sign(m-1)</code>.</p>
<p>Finally, Sage asked me to specify if $t>0$ or $t<0$ (so it can compute the integral). I assumed $t>0$. That's the command <code>assume(t>0)</code>.</p>
<p>If I did everything right, the following code should work:</p>
<pre><code>from __future__ import print_function
NUMBER_OF_ITERATIONS = 4
alpha = 6/5
n = 2
eta = 13/10
lamda = 1
h = 1
T = 1
var('h t x s j tau')
U(t) = 1/3*t^3+1
P(t) = 2*t-lamda*(-t^4/24+t^5/15-t^7/126+t^8/72)
b_k = 1
u = vector(SR, NUMBER_OF_ITERATIONS)
u[0] = 1
U = u[0]
assume(t>0)
for m in range(1, NUMBER_OF_ITERATIONS):
u[m] = b_k*taylor(e^t, t, 0, n-1) + sign(m-1)*u[m-1] - taylor(u[m-1], t, 0, n-1) + h/gamma(eta-alpha)*integrate((t-tau)^(eta-alpha-1)*(u[m-1].subs(t=tau))*P(tau),tau, t, 0); print(u[m])
U += u[m]
forget()
show(U(t))
print('U(0.1) =', N(U(t=0.1,h=1)))
plot(U(t).subs(h=1), t, 0, 1)
</code></pre>
<p><img alt="image description" src="/upfiles/15829515217141605.png"></p>
<p>I hope this helps! As always, if the answer needs to be modified or re-explained you can ask as a comment or send me a mail.</p>
https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50123#post-id-50123I agree, @juanjo. It took me some time to understand the formulas, and I am not completely sure that I understood them correctly.
On the other hand, I run some more experiments, and Sage seems unwilling to compute more than 4 iterations of the recurrence formula. The problem continues to be the integral. Apparently, it grows very fast, and Sage has difficulties trying to compute the result. I wonder why.
Quite interestingly, if I remove the integral, the series converges to $t+1$.Sat, 29 Feb 2020 17:10:40 +0100https://ask.sagemath.org/question/50050/for-loop-not-computing-the-values/?comment=50123#post-id-50123