在包含一些参数的 python 中集成 [英] Integration in python containing some parameter

查看:55
本文介绍了在包含一些参数的 python 中集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以符号的形式对具有一个变量和几个常量的方程进行积分,即 A,k,p 我计划在积分后输入其值.积分方程只有一个变量而没有其他符号(包含一些常数值)是简单直接的,但对于存在符号的情况,使用quad from积分scipy.integrate 给出错误.实际上 quad() 需要预先定义所有符号的值.我想要一个解决方案,我不必事先输入符号的值,而是输入并在之后替换它们.

为了更清楚地说明问题,

我想对波动方程进行积分

<块引用>

A * sin(kx + wt) ,其中 A 是振幅,w 是角速度,t 是时间,x 是位置(x 只是变量)

quad() 需要在积分前定义 A、k 和 p 的值,但我想做的是首先积分 A * sin(kx+ wt),然后通过用户输入替换结果中 A、k 和 p 的值.

请建议一种方法:)

解决方案

我们可以使用 sympy 库来集成一个函数,而无需为其符号赋值(具有一些常量实数作为其值)<强>(A、k、p 和 s).

来自我的一个项目的示例代码:

from sympy import *A = 符号('A')k = 符号('k')p = 符号('p')x = 符号('x')s = 符号('s')f = 积分(exp(-1*s*x)*A*sin(k*x+p),(x,0,oo))f = f.subs(A,50)打印 str(f)

<块引用>

(x,0,oo) 表示从零(0) 无穷大(哦)

代码需要做的是,首先定义常量A、k、p和s以及变量xsymbols,然后使用integration() 对表达式进行积分.

integrate() 返回积分表达式,现在,f.subs(symbol,value) 用于替换符号的值.

<块引用>

f.subs(symbol,value) 中,f 是包含积分表达式的变量,symbol 是之前定义的符号(如 A,k,p)value 是该符号的常量值.

此方法还有助于获得方程的明确积分(从 x=ax=b).需要做的是用b替换x的值,并使用m = f.subs(x,b)m中code> 然后使用 n = f.subs(x,a) 用 a 替换 x 的值并将其存储在某个变量 n.现在,使用 m - n 减去 m 和 n 得到表达式的定积分

<块引用>

这个问题还有一个可能的解决方案.

可以做的是定义一个函数,它接受用户对A、k 和 p 的输入,然后使用 quad() 对表达式进行积分,这将消除需要先对表达式进行积分,然后再根据用户输入来替换符号常量的值

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 :)

解决方案

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

这篇关于在包含一些参数的 python 中集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆