Python Forum
how can I solve fsolve function error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can I solve fsolve function error?
#1
import matplotlib.pyplot as plt  
import numpy as np
from scipy.optimize import fsolve

R = 0.08206
Tc = 150.687
Pc = 47.994
ω = -0.00219
T = 100

a = 0.42748*R**2*Tc**2/Pc
b = 0.08664*R*Tc/Pc
α = (1 + (0.48 + 1.574*ω - 0.176*ω**2)*(1 - (T/Tc)**(0.5)))**2


X = np.linspace(1, 731, 100) 
k = [] 
for dx in X: 
    P=dx
    def f(V):
        return P - R*T/(V - b)  + a*α/ V/(V + b)
    k.append(fsolve(f,0.05)*dx/R/T)
plt.xlabel("Pressure (atm)")
plt.ylabel("Compressibility Z")
plt.title("Z-p plot at 100K, Ar")
plt.plot(X, k, label = 'SRK')
plt.show()
This is my code for SRK Equation of state.
For some reason, when the temperature is high enough, (ex:T = 300) it won't have any problem.
[Image: P4zwO.png]
But when the temperature is low, like the code that I mentioned above, it will start to print out at error saying [Image: 5CKTN.png]
I've changed the number around a bit for my initial guess, but it won't help either

What changes should I make to solve this problem?
Reply
#2
It was just cross-posted on Stackoverflow
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Giving a negative reputation for announcing that you cross-posted without disclosing it is childish.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
In the stackoverflow thread you state that the 100k curve should look a lot like the 300k curve. I don't think that is true. Lowering the temperature not only lowers the compressibility, it also changes the shape of the curve, making it much sharper. It might be that fsolve is giving up because the equation is too non-linear and you are bouncing around but not getting any closer to the solution, or it could be that there is no solution. I noticed I can make the warning go away by starting with a really small starting value for V (0.000001). The resulting curve looks like a diagonal line to me. No jaggies and no curve.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 2 2,826 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  how to solve "a bytes-like object is required, not 'str'" error maiya 2 3,795 Jul-28-2020, 07:03 PM
Last Post: bowlofred
  How to Solving non-linear equation using scipy.optimize fsolve with variable list djhak 3 4,529 Jun-10-2020, 04:12 PM
Last Post: Gribouillis
  How to solve a function and get x, y and z? TheZenMan 2 2,282 Mar-22-2019, 01:10 PM
Last Post: scidam
  fsolve Mahdi1994 0 3,219 Mar-26-2018, 01:42 PM
Last Post: Mahdi1994
  Defining an fsolve function to call later hegdep 1 3,078 Oct-25-2017, 07:38 AM
Last Post: hegdep
  Error in using the output of one function in another function (beginner) MadsPJ 6 5,035 Mar-13-2017, 03:06 PM
Last Post: MadsPJ

Forum Jump:

User Panel Messages

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