The largevalue on fipy internal boundary condition - python

I have tried internal boundary condition in code below.
I found that while I have not set an external boundary condition, the solved result will depend on the LargeValue. Besides, while I increase the largeValue, I must redefine the equation again, otherwise the equation couldn't be changed by just setting a new value to LargeValue.
I have used the sweep method to try to get a better result, but it does not work.
Below is my code. Is there any mistake? Hope someone will help me!
for step in range(steps):
equation2=DiffusionTerm(coeff=perittivity)==ImplicitSourceTerm(largeValue*mask)-largeValue*mask*value
potential.setValue(0)
k=0.5
residual=1
while residual>1e-10 and abs(k-residual)>1e-18 :
k=residual
residual=equation2.sweep(potential)
if __name__=="__main__":
viewer.plot()
print step,residual,k-residual#,equation2,largeValue
largeValue=Variable(value = largeValue.value*1.1)

Related

Solving implicit equation in python using f solve

I want to write a program to ask for the values of Q,y,b,x,S0 then find the value of n from the following image
I used f solve to write this code:
from scipy.optimize import fsolve
def f(n,Q=float(input("Q=")),y=float(input("y=")),b=float(input("b=")),x=float(input("x=")),S_0=float(input("S0="))):
return (1/n)*((y*(b+x*y))**(5/3))/((b+2*y*(1+x**2)**(1/2))**(2/3))*S_0-Q
a=fsolve(f,1)
print(a)
print(f(a))
But it gives a false result as output for my inputs here:
Q=21
y=7.645
b=2
x=1
S0=0.002
/usr/lib/python3/dist-packages/scipy/optimize/minpack.py:236: RuntimeWarning: The iteration is not making good progress, as measured by the
improvement from the last ten iterations.
warnings.warn(msg, RuntimeWarning)
[ 1.]
[-20.68503025]
I wrote this in Online Python. I don't know what is the meaning of this error. also the output is wrong. answer should be n=0.015 for this specific input. how can I fix this code?
I rearranged your equation, and this somehow gets your expected result. I'm really not quite sure what is the issue, sorry!
return (S_0/Q)*((y*(b+x*y))**(5/3)/(b+2*y*(1+x**2)**(1/2))**(2/3))-n

Fitting curves in Jupyter Notebook without using the fit function

For a task for school, I have to write my own fit function by using the least squares method. The problem is I don't know how to do that, specifically I don't know how to minimize my function to calculate my fit parameters.The problem here is also that my fit function is not linear, so my book says I have to try guess some values for my fit parameters and then minimize my function. But still I don't know how do to that. The code that you can find below is my code right now, I got it from somebody but I don't understand what it does so :).
Thanks in advance!
def fit(x,mu,gamma,back,A):
return A*(gamma/((x-mu)**2+gamma**2))+back
def Ls_rechte(y):
Ls = 0
for i in range(len(Positie)):
Ls = Ls + (Intensiteit[i]- fit(Positie[i],y[0],y[1],y[2],y[3]))**2/(FoutI[i]**2)
return Ls
nu = len(Positie)-4
mini = minimize(Ls_rechte,(150,0,100,1))
display(mini)

Making an own signal source using Python Module in GNURadio

I want to use cos() function in my code to make my own signal source, but i don't know how to get the output with both the quadrature phase too EXACTLY like what a built-in signal source outputs.
Can anyone please help me with the python code to make a signal source using Python Module in flow graph.
DETAILS:
The code i wrote is:
def work(self, input_items, output_items):
fs_local = 30e6;
for i in range(0, fs_local):
output_items[0][i]= math.cos(2*pi*(math.pi)/fs_local)
return len(output_items[0])
The problem is that output items length is perhaps by default fixed to be 4096.
so any range in for loop not equal to output_items(i.e. for i in range(0, len(output_items[0]) or input_items(disabled in my case) is not going to work and does not show any output.
And if i use output_items length exactly as i wrote in above paragraph, then it will truncate the range to 4096 and thus outputs a ramp which is not my desired output.
Similarly not returning "len(output_items[0])" and instead returning "len(fs_local) won't show any results either.
Also how to output a 90 degree shifted cos wave too along with correctly outputting the cosine wave itself just like a built-in signal source?

Function definition and function call produce syntax error in python 3, even when copied and pasted

I am trying to solve the problem of a least squares fit of a power law spliced to a third order polynomial in python using gradient descent. I have computed gradients with respect to the parameters in Matlab. The boundary conditions I computed by hand. I am running into a syntax error in my chi-squared minimization algorithm, which must take into account the boundary conditions. I am doing this for a machine learning class in which I am completing a somewhat self-directed and self-proposed long term project, but I am stuck because of this syntax error that I am not sure how to overcome. I will not get class credit for this. It is simply something to put on my resume.
def polypowerderiv(x,a1,b1,c1,a2,b2,c2,d2,boundaryx,ydat):
#need to minimize square of ydat-polypower
#from Mathematica, to be careful
gradd2=2*(d2+c2*x+b2*x**2+a2*x**3-ydat)
gradc2=gradd2*x
gradb2=gradc2*x
grada2=gradb2*x
#again from Mathematica, to be careful
gradc1=2(c+a1*x**b1-ydat)
grada1=gradc1*x**b1
gradb1=grada1*a1*log(x)
return [np.sum(grada1),np.sum(gradb1),\
np.sum(gradc1),np.sum(grada2),np.sum(gradb2),\
np.sum(gradc2),np.sum(gradd2)]
def manualleastabsolutedifference(xdat, ydat, params,seed, maxiter, learningrate):
chisq=0 #chisq is the L2 error of the fit relative to the ydata
dof=len(xdat)-len(params)
xparams=seed
for step in np.arange(maxiter):
a1,b1,c1,a2,b2,c2,d2=params
chisq=polypowerlaw(xdat,params)
for i in np.arange(len(xdat)):
grad=np.zeros(len(seed))
for i in np.arange(seed):
polypowerlawboundarysolver=\
polypowerboundaryconstraint(xdat,a1,b1,c1,a2,b2,c2)
boundaryx=minimize(polypowerlawboundarysolver,x0=1000)
#hard coded to be half of len(xdat)
chisq+=abs(ydat-\
polypower(xdat,a1,b1,c1,a2,b2,c2,d2,boundaryx)
grad=\
polypowerderiv(xdat,a1,b1,c1,\
a2,b2,c2,d2,boundaryx,ydat)
params+=learningrate*grad
return params
The error I get is:
File "", line 14
grad=polypowerderiv(xdat,a1,b1,c1,a2,b2,c2,d2,boundaryx,ydat)
^
SyntaxError: invalid syntax
Also, I'm having some small trouble with formatting. Please help. This one of my first few posts to Stack Overflow ever, after many years of up and down votes. Thank you for your extensive help, community.
As per Alan-Fey, you forgot a closing bracket:
chisq+=abs(ydat-\
polypower(xdat,a1,b1,c1,a2,b2,c2,d2,boundaryx)
should be
chisq+=abs(ydat-\
polypower(xdat,a1,b1,c1,a2,b2,c2,d2,boundaryx))

Python - Scipy : ode module : issue enabling the step option of the solver

I wanted to store the different integration steps taken by the solver itself when I call it :
solver1.integrate(t_end)
So I did a while loop and enabled the step option setting its value to True:
while solver1.successful() and solver1.t < t0+dt:
solver1.integrate(t_end,step=True)
time.append(solver1.t)
Then I plot y, the result of integration and here comes my issue. I have instabilities which appear in a located area :
I thought it was because of the loop or something like that so I checked the result removing the step :
while solver1.successful() and solver1.t < t0+dt:
solver1.integrate(t_end)
And surprise ... I have the correct result :
It's a quite weird situation ... I'd be grateful if someone of your guys could help me with this issue.
EDIT :
To set the solver I do :
solver1 = ode(y_dot,jac).set_integrator('vode',with_jacobian=True)
solver1.set_initial_value(x0,t0)
And I store the result using .append()
When you set step=True you are indirectly giving the vode._integrator.runner (a Fortran subroutine) an instruction to use itask=2, and the default is itask=1. You can get more details about this runner doing:
r._integrator.runner?
In SciPy 0.12.0 documentation you will not find an explanation about what is going on for the different itask=1 or itask=2, but you can find it here:
ITASK = An index specifying the task to be performed.
! Input only. ITASK has the following values and meanings.
! 1 means normal computation of output values of y(t) at
! t = TOUT(by overshooting and interpolating).
! 2 means take one step only and return.
! 3 means stop at the first internal mesh point at or
! beyond t = TOUT and return.
! 4 means normal computation of output values of y(t) at
! t = TOUT but without overshooting t = TCRIT.
! TCRIT must be input as RUSER(1). TCRIT may be equal to
! or beyond TOUT, but not behind it in the direction of
! integration. This option is useful if the problem
! has a singularity at or beyond t = TCRIT.
! 5 means take one step, without passing TCRIT, and return.
! TCRIT must be input as RUSER(1).

Categories