Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends

I am trying to compute a definite integral of a matrix the matrix is 2*10

I need to compute its definite integral from zero to t as we know the results must coincide to the sinus table.

Here is the code before reaching the goal gives error

The error probably is for the last lines.

I would be so thankful if you guide me please.

import numpy as np
import math
import matplotlib.pyplot as plt
from scipy import integrate
from scipy.integrate import quad 
import numpy as np
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
from scipy.linalg import sqrtm
import scipy.integrate as it
from numpy import vectorize

############

#from numba import vectorize, float64
##############################
# Constants
tmax = 20
t = np.arange(0.0, tmax, 1)
t0=0
m=len(t)
#print(t)
etest=np.matrix([[np.sqrt(0),np.sqrt(1),np.sqrt(2) ,np.sqrt(3),np.sqrt(4),np.sqrt(5),np.sqrt(6),np.sqrt(7),np.sqrt(8),np.sqrt(9)],
             [np.sqrt(10),np.sqrt(11),np.sqrt(12), np.sqrt(13),np.sqrt(14),np.sqrt(15),np.sqrt(16),np.sqrt(17),np.sqrt(18),np.sqrt (19)]])
pow = np.power( etest, 2 )
pow0=pow[0]
pow1=pow[1]

def norm2etest(xx):
       sum= pow0+pow1
       return sum
ans2=norm2etest(etest)
print(ans2)

def outerfun(norm2etest):
    return np.cos(norm2etest)

def integrandtest(xx):
    return outerfun(norm2etest(xx))
ttt=outerfun(norm2etest(etest))
print(ttt)

for tnow in np.arange(0.0, tmax, 1):
 numnow=1*tnow
 num=np.array(numnow,dtype=int)
@np.vectorize
def integratetest(i):
    def integrandtest(t):
        return outerfun(norm2etest(t))[i]
    I, ff = quad(integrandtest, t0,tnow)
    return I
###########################
INTERVALtest=np.arange(0,10)
ANS1=integratetest(INTERVALtest)
print(INTERVALtest)
print(ANS1.size)
Python



And the error is
Python
<pre>Traceback (most recent call last):
  File "C:\Users\user\OneDrive\Desktop\44integral.py", line 58, in <module>
    ANS1=integratetest(INTERVALtest)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\function_base.py", line 2163, in __call__
    return self._vectorize_call(func=func, args=vargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\function_base.py", line 2241, in _vectorize_call
    ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\function_base.py", line 2201, in _get_ufunc_and_otypes
    outputs = func(*inputs)
  File "C:\Users\user\OneDrive\Desktop\44integral.py", line 54, in integratetest
    I, ff = quad(integrandtest, t0,tnow)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\integrate\quadpack.py", line 352, in quad
    points)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\integrate\quadpack.py", line 463, in _quad
    return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
TypeError: only size-1 arrays can be converted to Python scalars


What I have tried:

I have tested it several times but I could not find the reason.
Posted
Updated 13-Aug-21 22:02pm

Getting your code to run does not mean it is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
Sara Collins 14-Aug-21 4:03am    
Thank you so much for your kind and comprehensive reply.
I did and the last line
ANS1=integratetest(INTERVALtest)
seems to make the error what is strange is the the similiar code elsewhere works.
Look at the error message relating to your program:
  File "C:\Users\user\OneDrive\Desktop\44integral.py", line 58, in <module>
    ANS1=integratetest(INTERVALtest)

...

  File "C:\Users\user\OneDrive\Desktop\44integral.py", line 54, in integratetest
    I, ff = quad(integrandtest, t0,tnow)

TypeError: only size-1 arrays can be converted to Python scalars

Somewhere (as in your previous question) you are passing invalid parameters in to a numpy function.
 
Share this answer
 
Comments
Sara Collins 14-Aug-21 4:06am    
Thank you very much yes but the command vectorize which I have used must convert the
function to a vector valued one and then I am giving the array INTERVALtest
So I suppose that
it must act with this too.
Also this procedure which I have considered here works for another program with e of the dimension 2*1000 but here does not work.
Richard MacCutchan 14-Aug-21 4:18am    
You are making assumptions which may not be valid. As I suggested before, you need to use the debugger to examine exactly what values you are passing to, and receiving from, the various numpy functions.
Sara Collins 14-Aug-21 5:21am    
What other method is to vectorize a function and then compute the integral for each component?
Richard MacCutchan 14-Aug-21 5:22am    
Sorry, I have no idea; this is calculus which I have not studied since 1961.
Sara Collins 14-Aug-21 5:24am    
I see. 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