Python Forum

Full Version: [Urgent] build code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, I hope y'all are well.
I have an urgent task that must do in 8 hours from now.
This task needs an expert with mathematics related to differential equations and coding in python for a simple problem.

I attracted task as an image.

https://drive.google.com/open?id=13aH4pF...GYSrgyowEj

I prefer this code format.

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def microbes(phi, kappa, mu, L = 1024, Nx=1024,Nt=1201,T=600,display=False):
    """
    Question 2.2
    Simulate microbe competition model

    Input:
    phi,kappa,mu: model parameters
    Nx: Number of grid points in x
    Nt: Number of time steps
    T: Timespan for simulation is [0,T]
    Display: Function creates contour plot of f when true

    Output:
    f,g: Nt x Nx arrays containing solution
    """

    #generate grid
    L = 1024
    x = np.linspace(0,L,Nx)
    dx = x[1]-x[0]
    dx2inv = 1/dx**2

def RHS(y, t, k, r, phi, dx2inv):
    n = y.size//2

    f = y[:n]
    g = y[:n]

    #Compute 2nd derivatives
    d2f = (f[2:]-2*f[1:-1]+f[:-2])*dx2inv
    d2g = (g[2:]-2*g[1:-1]+g[:-2])*dx2inv

    #Construct RHS
    R = f/(f+phi)
    dfdt = d2f + f[1:-1]*(1-f[1:-1])- R[1:-1]*g[1:-1]
    dgdt = d2g - r*k*g[1:-1] + k*R[1:-1]*g[1:-1]
    dy = np.zeros(2*n)
    dy[1:n-1] = dfdt
    dy[n+1:-1] = dgdt

    #Enforce boundary conditions
    a1,a2 = -4/3,-1/3
    dy[0] = a1*dy[1]+a2*dy[2]
    dy[n-1] = a1*dy[n-2]+a2*dy[n-3]
    dy[n] = a1*dy[n+1]+a2*dy[n+2]
    dy[-1] = a1*dy[-2]+a2*dy[-3]

    return dy

rho = mu/kappa
F = rho*phi/(1-rho)
G = (1-F)*(F+phi)
y0 = np.zeros(2*Nx) #initialize signal
y0[:Nx] = F
y0[Nx:] = G + 0.01*np.cos(10*np.pi/L*x) + 0.01*np.cos(20*np.pi/L*x)

t = np.linspace(0,T,Nt)


#compute solution
print("running simulation...")
y = odeint(RHS,y0,t,args=(kappa,rho,phi,dx2inv),rtol=1e-6,atol=1e-6)
f = y[:,:Nx]
g = y[:,Nx:]
print("finished simulation")
if display:
    plt.figure()
    plt.contour(x,t,f)
    plt.xlabel('x')
    plt.ylabel('t')
    plt.title('Contours of f')


return f,g

def newdiff(f,h):
    """
    Question 2.1 i)
    Input:
        f: array whose 2nd derivative will be computed
        h: grid spacing
    Output:
        d2f: second derivative of f computed with compact fd scheme
    """

    d2f = np.zeros_like(f) #modify as needed

    #Coefficients for compact fd scheme
    alpha = 9/38
    a = (696-1191*alpha)/428
    b = (2454*alpha-294)/535
    c = (1179*alpha-344)/2140


    return d2f #modify as needed

def analyzefd():
    """
    Question 2.1 ii)
    Add input/output as needed

    """

    return None #modify as needed


def dynamics():
    """
    Question 2.2
    Add input/output as needed

    """

    return None #modify as needed

if __name__=='__main__':
    x=None
    #Add code here to call functions above and
    #generate figures you are submitting
Is there someone who could solve this task?
I really need this, urgent task, if you do this, it is really fantastic.
Please help me if you are python and mathematic expert.
Couple issues. We don't do your homework, rather can help you sort out errors.
Second, your code is hard to decipher as it is "unpythonic". Properly designed python program use variable names that are descriptive.
Couple obvious things - your first function is called with a lot of parameters but returns nothing, despite the comments. You also accept L as a parameter with a default value of 1024, but you then ignore that and just assign L the value of 1024. No matter, it does not get passed back anyway.
Line 78 is a return that is not inside a function. That will cause an error.

So, what have you done with the code, what errors are you getting?