首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用渐近解方程式?

如何使用渐近解方程式?
EN

Stack Overflow用户
提问于 2019-05-25 17:29:31
回答 2查看 161关注 0票数 1

我需要写一段代码来求解McLachlan模型方程。要在for循环中用不同的参数(x和h)替换c的值,该怎么做??!

我有用matlab编写的代码,它使我真正需要的东西..但是同样的想法对python不起作用,我得到了错误!!

代码语言:javascript
复制
Traceback (most recent call last):
  File "func.py", line 18, in <module>
    (x * f ** (1 / h) - x * c ** (1 / h))
NameError: name 'c' is not defined

下面是我的python代码

代码语言:javascript
复制
import numpy
from sympy import Symbol, solve

v_p = input("Enter perculion threshold:")
sigma_P = input("Enter MOFs conductivity:")
sigma_F = input("Enter filler conductivity:")

p = float(sigma_P)
f = float(sigma_F)

x = Symbol('c')
A = (1 - float(v_p) / float(v_p))

for h in numpy.arange(1, 1.5, 0.1):
   for x in numpy.arange(0, 1, 0.1):
       print(solve([
           (
                   (x * f ** (1 / h) - x * c ** (1 / h))
                   /
                   (f ** (1 / h) + A * c ** (1 / h))
           )
           /
           (
                   (p ** (1 / h) - c ** (1 / h) - x * p ** (1 / h) + x * c ** (1 / h))
                   /
                   (p ** (1 / h) + A * c ** (1 / h))
           )
       ], [c]))

这是用matlab写的代码。

代码语言:javascript
复制
syms sigma_c
A=4.777
sigma_f = 550
sigma_p = 1.7 * 10 ^ (-11)

for h = 2:10
    for j = 1:10
        v_f = 0.1 * j;
        ans = solve([(((v_f) * (((sigma_f) ^ (1 / h)) - ((sigma_c) ^ (1 / h))))/(((sigma_f) ^ (1 / h)) + ((A) * ((sigma_c) ^ (1 / h))))) + (((1 - v_f) * (((sigma_p) ^ (1 / h)) - ((sigma_c) ^ (1 / h))))/(((sigma_p) ^ (1 / h)) + ((A) * ((sigma_c) ^ (1 / h))))) == 0], [sigma_c]);
        answer = double(ans)
        arr(h,j) = answer;
        end
end

disp(arr)
EN

回答 2

Stack Overflow用户

发布于 2019-05-25 20:37:27

您会收到"SyntaxError: invalid SyntaxError“,因为并非所有的括号都是闭合的。下面的代码建议进行格式化,以便在计算中提供更多概述。我希望应该在第25行添加')‘,但是这显然是模棱两可的,你应该用自己的想法来验证这一点。

请注意,'c‘仍然是未定义的,没有它你的代码将无法工作。

代码语言:javascript
复制
import numpy
from sympy import Symbol, solve

v_p = input("Enter perculion threshold:")
sigma_P = input("Enter MOFs conductivity:")
sigma_F = input("Enter filler conductivity:")

p = float(sigma_P)
f = float(sigma_F)

x = Symbol('c')
A = (1 - float(v_p) / float(v_p))

for h in numpy.arange(1, 1.5, 0.1):
    for x in numpy.arange(0, 1, 0.1):
        print(solve([
            (
                    (x * f ** (1 / h) - x * c ** (1 / h))
                    /
                    (f ** (1 / h) + A * c ** (1 / h))
            )
            /
            (
                    (p ** (1 / h) - c ** (1 / h) - x * p ** (1 / h) + x * c ** (1 / h))
                    /
                    (p ** (1 / h) + A * c ** (1 / h))
            )
        ], [c]))
票数 1
EN

Stack Overflow用户

发布于 2019-05-26 04:47:42

对于符号部分,SymPy可以提供很多帮助。如果您复制并粘贴工作公式,然后将其替换为您尝试在Python版本中使用的符号,则会得到一个与您在Python版本中输入的表达式不同的表达式:

代码语言:javascript
复制
>>> eq2=S('''(((v_f) * (((sigma_f) ^ (1 / h)) - ((sigma_c) ^ (1 / h)))
)/(((sigma_f) ^ (1 / h)) + ((A) * ((sigma_c) ^ (1 / h))))) + ((
(1 - v_f) * (((sigma_p) ^ (1 / h)) - ((sigma_c) ^ (1 / h))))/((
(sigma_p) ^ (1 / h)) + ((A) * ((sigma_c) ^ (1 / h)))))'''.replace('^','**'))
>>> eq2 = eq2.subs(
'v_f','x').subs(
'sigma_f','f').subs(
'sigma_c','c').subs(
'sigma_p','p')
>>> factor_terms(eq2)
x*(-c**(1/h) + f**(1/h))/(A*c**(1/h) + f**(1/h)) + (1 - x)*(-c**(1/h) + p**(1/h))/(
A*c**(1/h) + p**(1/h))

但好消息是,这两个方程都可以象征性地求解c**(1/h),因为它在表达式中是二次的,所以您可以在计算x和h的值后将它们替换到解中。一种方便的方法是,例如,

代码语言:javascript
复制
>>> soln = Tuple(*solve(x**2 - y, x))
>>> for yi in (2, 3):
...    print(soln.subs(y, yi).n())  # the .n() to evaluate the values
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56303541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档