I want to solve an equation in python but I keep getting this error:
__new__() missing 1 required positional argument: 'name'
and my code is as below:
import sympy as sym
sym.init_printing()
x,y,z = sym.symbols('x,y,z')
c1 = sym.Symbol('c1')
f=sym.Eq(2*x**2+y+z,1)
g=sym.Eq(x+2*y+z,c1)
h=sym.Eq(-2*x+y,-z)
sym.solve([f,g,h],(x,y,z))
Related
For a class, I am using solve_ivp to solve a differential equation function that I customize using keyword arguments. Here is my differential equation function integrate and the function sineCurrent I pass to it to help generate the differential equation. I already know that calling sineCurrent within integrate works. My issue arises when I try to pass integrate and by extension sineCurrent to solve_ivp. My class requires that they all remain separate functions.
def integrate(time,**kwargs):
func = kwargs['current']
V = kwargs['voltage']
Cm = 0.2 # membrane capacitance in nF
R = 100 # membrane resistance in mega-ohms
V_rest = -60 # resting membrane voltage in mV
I = func(time,**kwargs) # passing arguments current input function
s = I*R
dVdt = (V_rest-V+s)/(Cm*R)
return dVdt
def sineCurrent(time,**kwargs):
# Passing frequency and current arguments, setting defaults to f=0.5 kHz and I=2 nA
defaultKwargs = { 'freq': float("0.5"), 'Imax': float("2"),}
kwargs = { **defaultKwargs, **kwargs }
freq = kwargs['freq']
Imax = kwargs['Imax']
# Error message if time vector not included
if len(time) == 0:
return "sineCurrent requires at least one input parameter, time"
# Calculating sineCurrent
I = Imax*np.sin(2*pi*freq*time)
return I
I tried several methods to pass integrate to solve_ivp. First, I tried using the basicsolve_ivp syntax.
t = np.arange(-10,100,0.1)
V_init= np.array([50])
soln = solve_ivp(integrate,t,current=sineCurrent,[t[0], t[-1]],V_init)
This yielded the error SyntaxError: positional argument follows keyword argument. Next I tried using a lambda function.
t = np.arange(-10,100,0.1)
V_init= np.array([50])
soln = solve_ivp(integrate=lambda t,current=sineCurrent: [t[0], t[-1]],V_init)
This led to the same error message as before. So I moved the keyword arguments to the end.
t = np.arange(-10,100,0.1)
V_init= np.array([50])
soln = solve_ivp(integrate,[t[0], t[-1]],V_init,args=(t,current=sineCurrent,voltage=V_init))
This led to the error message SyntaxError: invalid syntax. I am at a loss as to what to try next. I know that my integrate function can produce an np.ndarray when arguments are passed to it properly, shown below, but I cannot figure out how to pass integrate to solve_ivp.
dVdt = integrate(t,current=sineCurrent,voltage=V_init)
I am currently learning how to do differential equations on Python using SymPy.
def problem():
alpha, beta, t = symbols('alpha beta t', real=True)
s = Function('s')(t)
expr = Eq(s.diff(t), alpha*s**2 + beta*s)
print(expr)
print(sympy.dsolve(expr, simplify=True, ics={s(0): 1000}))
It always gives me TypeError: object 's' is not callable when putting the initial condition (it works and give me general solution with C1 when I remove ics)
Anyone can help with this?
I'm trying to use MATLAB engine to call a MATLAB function in Python, but I'm having some problems. After manage to deal with NumPy arrays as input in the function, now I have some error from MATLAB:
MatlabExecutionError: Undefined function 'simple_test' for input
arguments of type 'int64'.
My Python code is:
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
eng.cd()
Nn = 30
x= 250*np.ones((1,Nn))
y= 100*np.ones((1,Nn))
z = 32
xx = matlab.double(x.tolist())
yy = matlab.double(y.tolist())
Output = eng.simple_test(xx,yy,z,nargout=4)
A = np.array(Output[0]).astype(float)
B = np.array(Output[1]).astype(float)
C = np.array(Output[2]).astype(float)
D = np.array(Output[3]).astype(float)
and the Matlab function is:
function [A,B,C,D] = simple_test(x,y,z)
A = 3*x+2*y;
B = x*ones(length(x),length(x));
C = ones(z);
D = x*y';
end
Is a very simple example but I'm not able to run it!
I know the problem is in the z variable, because when I define z=32 the error is the one I mentioned, and when I change for z=32. the error changes to
MatlabExecutionError: Undefined function 'simple_test' for input
arguments of type 'double'.
but I don't know how to define z.
I'm new to coding, so have patience please. I have an exercise where the aim is to solve a differential equation, where there is a function that has an integral of the form dx cos(x)**2 between 0 and t.
I've tried defining a function, then using the definition to attempt to get a final value at the end of the programme.
import numpy
from scipy import integrate
def dydt(C,y,t):
return -C*y
def f_2(x):
return numpy.sin(x)**2
def C(t):
return 1+ integrate.quad(f_2,0,t)
t=numpy.linspace(0,10,100)
y=integrate.odeint(dydt,[1.0],t)
This is the error I'm getting: TypeError: dydt() missing 1 required positional argument: 't' --> I know I have clearly defined my t, so what am I doing wrong? Cheers!
dydt(C,y,t) should be dydt(C,y)
import numpy
from scipy import integrate
def dydt(C,y):
return -C*y
def f_2(x):
return numpy.sin(x)**2
def C(t):
return 1+ integrate.quad(f_2,0,t)
t=numpy.linspace(0,10,100)
y=integrate.odeint(dydt,[1.0],t) # works fine
print(y)
Basically what the title states. I'm getting what I think to be numerical errors propagating in my code, so am attempting to improve by changing all the types to mpcs, with a high precision. However, some of my calculations are symbolic and sympy doesn't seem to like the mpcs.
val += (1.0/2.0)*(1.0/kConversionFactor)**(ci) * ( A*k**(ln-lm+2*ci) - 1.0j*B*k**(ln+lm+1+2*ci) )
TypeError: unsupported operand type(s) for *: 'mpc' and 'Symbol'
k is the sympy symbol A and B are mpc's
Updated following comments:
val += (1.0/2.0)*(1.0/kConversionFactor)**(ci) * (mpcToSympy(A)*k**(ln-lm+2*ci) - sy.I*mpcToSympy(B)*k**(ln+lm+1+2*ci) )
....
def mpcToSympy(mpc_):
return ( sy.Float(str(mpc_.real),DPS) + sy.Float(str(mpc_.imag),DPS)*sy.I )
See here: Convert from mpf to Sympy Float without losing precision