Reputation: 111
I need to write a code to solve the McLachlan model equation. to find value for c after substituting with different parameters (x and h ) from for-loops how to do that ??!
I have the code written in matlab that makes what I exactly need .. but the same idea is not working for python I am getting errors !!
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
here is my code in python
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]))
and this is the code written in matlab
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)
Upvotes: 1
Views: 2334
Reputation: 19135
SymPy can help a lot with the symbolic part. If you copy and paste your working equation and then replace it with the symbols you are trying to use in the Python version you will get an expression that is not the same as the one you entered in the Python version:
>>> 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))
But the good news is that either equation can be solved symbolically for c**(1/h)
, since it is quadratic in that expression, so you can then substitute values for x and h into the solutions after you compute them. A convenient way to do this is, for example, is
>>> soln = Tuple(*solve(x**2 - y, x))
>>> for yi in (2, 3):
... print(soln.subs(y, yi).n()) # the .n() to evaluate the values
Upvotes: 0
Reputation: 5708
You receive the "SyntaxError: invalid syntax" because not all parentheses are closed. The code below suggests are formatting to give more overview in the computation. I expect the ')' should be added on line 25, however this is obviously ambigious and you should verify this with your own idea.
Note that 'c' is still undefined, your code won't work without it.
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]))
Upvotes: 1