Python Forum
How to calculate integral value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to calculate integral value?
#1
I am getting an IndexError, and not sure why? My goal is to calculate the Following integral.
the mathematical expression in attached file
%reset -f
import numpy as np
from scipy import io, integrate, linalg, signal
from scipy.sparse.linalg import cg, eigs
import sympy as sp
import math
from math import *
from sympy import  *
# Define symbols
z = sp.symbols('z')
lh = 10.
L = 1.
h = L / lh
p = 10.
V = ((1. / 2.) + (z / h))**p
Ec = 380.
vc = 0.23
Em = 70.
vm = 0.23
# Effective properties of the CNTRC
Ez = Em + (Ec - Em) * V
fz=z;
fz1=diff(fz,z);
Q11=Ez;
Q55=Ez/(2*(1+vm));
B11=sp.integrate(Q11*z,(z,-h/2,h/2));
Error:
TypeError: 'Float' object cannot be interpreted as an integer
your help is appreciated

Attached Files

.docx   expression.docx (Size: 14.94 KB / Downloads: 93)
Reply
#2
Please elaborate, at least, please:
  • What is the error (please post entire error traceback massage using bbcode error tags).
  • Show the mathematical expression, in the text.
Reply
#3
Long time since I did any maths!

Don't know how to put maths symbols here, so I will use S as the integral symbol.

I seem to remember you can just take the Q out, calculate the result and multiply the result by Q, but I'm not so sure of that.

The definite integral Sxdx between the limits a and b, using Python power symbol ** is very simple:

Quote:(b**2) / 2 - (a**2) / 2

For example:

Quote:a = 0, b=2, Q=3

S 3xdx = 3((2**2) / 2) =6

You don't need any modules for any given Q, z
Reply
#4
Dear Pedroski5, thanks for the replying.
Could you please explain in more with a python example
Reply
#5
Like I said, my maths is a bit rusty, but something like this should do it:

# Python uses x**n to mean x to the power of n
# the Definite Integral of x**ndx between a and b is defined as:
# (b**(n+1)) / (n+1) - (a**(n+1)) / (n+1)
# Let S represent the integration symbol
# result = S Qzdz

# a function to calculate the Definite Integral
def calculateDefInt(Q, a, b, n):
    res = Q * ((b**(n+1)) / (n+1)) - ((a**(n+1)) / (n+1))
    return res

# first collect the values as a list
# append the values as a float type value
values = []
for v in 'Qabn':
    value = input(f'Enter a value for {v} ... ')
    values.append(float(value))

DefInt = calculateDefInt(*values)
If
Output:
values = [3.0, 0.0, 2.0, 1.0]
then
Output:
calculateDefInt(*values) = 6.0
Reply
#6
Hi
[SOLVED]
from scipy.integrate import trapz
import numpy as np
import sympy as sp
from sympy import  *
from sympy import symbols
# Define symbols
z = symbols('z')
Ec=380;Em=70;L=1.;
lh =10.
p=10.
h=L/lh 
V = ((1. / 2.) + (z / h))**p
Ez=(Ec-Em)*V+Em
Q11=Ez;
zz = np.linspace(-h/2, h/2, 600)
# Substitute zz into the symbolic expression Q11
B11_vals = [(z*Q11).subs(z, val) for val in zz]
# Perform numerical integration
B11 = np.trapz(B11_vals, zz)
print(f'B11={B11:4.6f}')
likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Positive integral solutions for Linear Diophantine Equation august 4 2,208 Jan-13-2023, 09:48 PM
Last Post: Gribouillis
  SyntaxError - Integral michitrujillo 1 2,736 Jan-21-2019, 05:59 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020