Click here to Skip to main content
15,881,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In order to compute definite integral $\int_{0}^{t}\exp(||e||^2)$

I have written this code

How can I adapt the number of steps with the step mathematically it seems to be true but when I want to write the code I faced to a problem

I have tried to express the upper bound (i.e. t) by using linespace I think my problem is that I could not state the interval in which "t" is located.


Python
import numpy as np
import math
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
from scipy import integrate
from scipy.integrate import quad
from scipy.integrate import quad
import scipy.integrate as it
import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt
from numpy import linalg as LA
from numpy.linalg import inv
from numpy.linalg import matrix_power
from scipy import linalg
from sympy import *
from numpy.linalg import eig
import numpy
import cvxopt
import cvxpy as cp
from cvxopt import matrix, solvers
from scipy.linalg import sqrtm
from scipy import integrate
##############################
# Constants

tmax = 10
t = np.arange(0.0, tmax, 0.1)
m=len(t)
#print(t)
e=np.matrix([[0,1,2 ,3],
             [0,1,1, 1]])

e1=e[0]

e2=e[1]
a=np.power(e1,2)
b=np.power(e2,2)
nrm2e=(a+b)
#print(e1)
#print(e2)
#print(a)
#print(b)
print(nrm2e)
num=10*t
x = np.linspace(0.0, t, num)
nrm2e_int=it.cumtrapz(nrm2e,x, initial=0)
print(nrm2e_int)


I got this error
Traceback (most recent call last):
  File "C:/Users/user/OneDrive/Desktop/yyyyyyyyyyyyyyyyyyyyyyyyyyyyy.py", line 45, in <module>
    x = np.linspace(0.0, t, num)
  File "<__array_function__ internals>", line 6, in linspace
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\function_base.py", line 120, in linspace
    num = operator.index(num)
TypeError: only integer scalar arrays can be converted to a scalar index


What I have tried:

I guess that it is because of num which is the number of steps (0.1).

Also when I print values of 10*t

I got float numbers while it must be integer.

Could you please guide me? How can I fix it?

Thank u
Posted
Updated 10-Aug-21 2:58am
v4

Python
x = np.linspace(0.0, t, num)

You cannot use floating point numbers for indexes, you must use integers, thus:
Python
x = np.linspace(0, t, num)


The actual problem is that num is not an integral value.

You also have repeated many of your import statements, you should remove the duplicates.
 
Share this answer
 
v2
Comments
Sara Collins 10-Aug-21 4:44am    
Thank you so much. as u advised I changed code




but again I got that error
Sara Collins 10-Aug-21 4:48am    
x = np.linspace(0, t, num)
nrm2e_int=it.cumtrapz(nrm2e,x, initial=0)

error is

Traceback (most recent call last):
  File "C:/Users/user/OneDrive/Desktop/nnnnnn.py", line 47, in <module>
    x = np.linspace(0, t, num)
  File "<__array_function__ internals>", line 6, in linspace
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\function_base.py", line 120, in linspace
    num = operator.index(num)
TypeError: only integer scalar arrays can be converted to a scalar index
Sara Collins 10-Aug-21 5:04am    
When we multiply t by 10 there must not be any float in the num but there are and this is really strange for me.
Richard MacCutchan 10-Aug-21 5:16am    
The variable t is an array, so when you multiply it by 10 it is not an integer. As I said previously, spend some time studying the documentation so you understand what types you are dealing with. You cannot assume that they are all integers.
Sara Collins 10-Aug-21 5:25am    
I see. u r right. How can I convert it in num to integers?
I used floor and int but they did not work.
I have tidied up your code, and added some useful print statements, and this is the output:
t: [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7
 1.8 1.9 2.  2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.  3.1 3.2 3.3 3.4 3.5
 3.6 3.7 3.8 3.9 4.  4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.  5.1 5.2 5.3
 5.4 5.5 5.6 5.7 5.8 5.9 6.  6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.  7.1
 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.  8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9
 9.  9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9]
e1: [[0 1 2 3]]
e2: [[0 1 1 1]]
a: [[0 1 4 9]]
b: [[0 1 1 1]]
nrm2e: [[ 0  2  5 10]]
num: [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15. 16. 17.
 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35.
 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53.
 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71.
 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89.
 90. 91. 92. 93. 94. 95. 96. 97. 98. 99.]
Traceback (most recent call last):
  File "C:\Users\rjmac\Documents\VSCode\Python\cp.py", line 31, in <module>
    x = np.linspace(0, t, num)
  File "<__array_function__ internals>", line 5, in linspace
  File "C:\Users\rjmac\AppData\Local\Python\Python39\lib\site-packages\numpy\core\function_base.py", line 120, in linspace
    num = operator.index(num)
TypeError: only integer scalar arrays can be converted to a scalar index

As you can see, t and num are arrays of floats, so they are not valid in the call to linspace.
 
Share this answer
 
Comments
Sara Collins 10-Aug-21 6:05am    
Thank you so much. I see but how can I convert the mode of it so that I can use it in line space

I have tried the int() and floor() but I did not get the required result
Richard MacCutchan 10-Aug-21 6:37am    
I have run a few tests, and the issue is that num needs to be an integer. So you need to set it to the number of values that you want linspace to generate. So something like
num = len(t)
x = np.linspace(0.0, t, num)
Sara Collins 10-Aug-21 7:51am    
Thank you so much here the length of t differs in the x = np.linspace(0,t,num)
and np.arange(0.0, tmax, 0.1). in the case of linespace "t" is the upper bound of the integral and for determining the segment that t is located in I have multiplied it by 10. because t is variable in linspace I cannot use the length command but if I multiply it at 10 (steps) then the segment which t is located will be obtained the problem is that python considers it as float number and I cannot convert it to integer by current commands such as int() and floor(), so is there another way to convert data to integer or is there a way that gives the length for t (t is not fixed)? .
Richard MacCutchan 10-Aug-21 8:23am    
That was just a suggestion to get a value. I have no idea what this code is supposed to do so I had to make a guess.
Richard MacCutchan 10-Aug-21 8:26am    
As I told you earlier, t is an array so you cannot use its value(s) as the basis for the num value in linspace. You need to work out how many elements you want linspace to generate.
Dear Richard thank you so much it has worked. I really appreciate it.
 
Share this answer
 
Comments
Richard Deeming 10-Aug-21 9:46am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment. As you have done above.

Do not post your comment as another "solution" to your question. And do not mark your comment as the accepted solution to your question.
Sara Collins 11-Aug-21 2:51am    
ok thank you so much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900