-2

I want to perform integration of an equation with one variable and few constants in the form of symblos namely, A,k,p whose values I have planned to enter after integration. Integrating equation with only one variable but with no other symbol(containing some constant value) is simple and direct, but for the case in which symbols are present, integration using quad from scipy.integrate gives an error. Actually quad() requires the values of all symbols to be pre-defined. I want a solution where I don't have to enter the values for symbols beforehand but take an input and substitue them afterwards.

To make the problem even more clear,

I want to integrate a wave equation

A * sin(kx + wt) , where A is amplitude, w is angular velocity, t is time and x is position (x is only variable )

quad() requires to define the values for A, k and p before integrating but what I want to do is first integrate A * sin(kx + wt) and then substitue the values for A, k and p in the result by taking user input .

Please suggest a method to do so :)

MSD
  • 154
  • 2
  • 12
  • 2
    Feel like tellins us _what_ that error is? – Christofer Ohlsson Dec 18 '17 at 09:45
  • do you have numpy and scipy installed? `pip install numpy` `pip install scipy`. – jan-seins Dec 18 '17 at 09:46
  • `NameError Traceback (most recent call last) in () 6 k=1 7 p=56 ----> 8 print quad(integrand,0,np.pi,args=(A,k,p)) NameError: name 'A' is not defined ` **this is the error** – MSD Dec 18 '17 at 10:01
  • 1
    The error message is pretty clear and it's pretty clear what you have to do. – Psytho Dec 18 '17 at 10:23
  • The issue was solved :) and I got a solution for the question(which is correct, I don't understand why people have downvoted it !) – MSD May 11 '18 at 10:47

2 Answers2

1

We can use sympy library for integrating a function without assigning values for its symbols(having some constant real number as its value) (A,k,p and s).

Example code from one of my projects:

from sympy import *
A = symbol('A')
k = symbol('k')
p = symbol('p')
x = symbol('x')
s = symbol('s')

f = integrate(exp(-1*s*x)*A*sin(k*x+p),(x,0,oo))

f = f.subs(A,50)

print str(f)

(x,0,oo) means integrate wrt x from zero (0) to infinty(oo)

What the code requires to be done is, first define constants A, k, p and s as well as the variable x as symbols and then use integrate() to integrate the expression.

integrate() returns the integrated expression and now, f.subs(symbol,value) is used to substitue the values of symbols.

in f.subs(symbol,value) , f is the variable containing the integrated expression, symbol is the symbol defined earlier(like A,k,p) and value is the constant value for that symbol.

This method also helps to get definite integration(from x=a to x=b) of equations. What one has to do is substitute the value of x with b and store it in some variable m using m = f.subs(x,b) and then use n = f.subs(x,a) to substitute the value of x with a and store it in some variable n. Now, substract m and n using m - n to get the definite integral of the expression

There is one more possibe solution to the question.

What can be done is define a function which takes user inputs for A, k and p and then use quad() to integrate the expression, this will wipe out the need to first integrate the expression and then take user input to substitue the values of symbolic constants

MSD
  • 154
  • 2
  • 12
0

It appears you are not defining the variable "A", try doing

A=15

Or anything like that, just make sure you define A before calling it.

  • but i want to use A as as constant like on integrating Asin(x) we get -Acos(x) +c – MSD Dec 18 '17 at 10:37
  • Well you have to define it as something or else the code will not work, currently it tries to find A and finds nothing. If you want it to be a constant then you must define it as that constant – Foster Williams Dec 18 '17 at 10:39