Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Urgent] build code
#1
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
Reply
#2
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.
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation urgent , Python homework alm 2 3,207 May-09-2021, 11:19 AM
Last Post: Yoriz
  urgent I got a syntax errors alm 2 7,701 Feb-28-2021, 02:54 PM
Last Post: alm
Heart Urgent homework help needed Medou 4 3,678 Nov-24-2020, 09:28 AM
Last Post: buran
  Bifid Genkey (Urgent) Laura123 2 2,677 Mar-09-2020, 08:09 PM
Last Post: micseydel
  Python Homework Help *Urgent GS31 2 3,244 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Need help! Please! Urgent! foxylen 1 2,873 Feb-27-2019, 05:50 PM
Last Post: buran

Forum Jump:

User Panel Messages

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