Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Need Help solving second order differential equations SkewedZone Unladen Swallow Posts: 2 Threads: 1 Joined: Jun 2019 Reputation: 0 Likes received: 0 #1 Jun-25-2019, 03:39 AM (This post was last modified: Jun-25-2019, 05:38 AM by Gribouillis. Edited 3 times in total.) So I have been working on a code to solve a coupled system of second order differential equations, in order to obtain the numerical solution of an elastic-pendulum. I've written the code needed to get the results and plot them, but I keep getting the following error: "TypeError: () missing 1 required positional argument: 'd'". I really appreciate any help. Here's the code: ```import numpy as np import matplotlib.pyplot as plt from scipy.integrate import odeint import math ci = [0.01, 0., 0., 0.0156613949, 0.00237, 0.] def solver(t, pinit): return odeint(lambda a,b,d : [a, -4*math.pi*math.pi*a*(math.sqrt(a*a+b*b+d*d)- 3/4)/math.sqrt(a*a+b*b+d*d),b, -4*math.pi*math.pi*b*((a**(2)+b**(2)+d**(2))**(1/2) - 3/4)/(a**(2)+b**(2)+d**(2))**(1/2), d,-4*math.pi*math.pi*d*(math.sqrt(a**(2)+b**(2)+d**(2)) - 3/4)/math.sqrt(a**(2)+b**(2)+d**(2)) - math.pi*math.pi], pinit, t) time = np.arange(0, 500.0, 0.01) solution = solver(time, ci) plt.plot(solution[:,0], solution[:,2]) plt.show() ``` Gribouillis wrote Jun-25-2019, 05:38 AM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button. Gribouillis  Posts: 2,400 Threads: 26 Joined: Jan 2018 Reputation: 224 Likes received: 560 #2 Jun-25-2019, 06:24 AM (This post was last modified: Jun-25-2019, 06:24 AM by Gribouillis. Edited 4 times in total.) I think the first argument of odeint() is a function that computes the first derivative of the state vector at time t, so it's probably something along the line of ```import numpy as np import matplotlib.pyplot as plt from scipy.integrate import odeint ci = [0.01, 0., 0., 0.0156613949, 0.00237, 0.] def func(c, t): a, A, b, B, d, D = c #a, b, d, A, B, D = c # Correct order of arguments? p2 = np.pi ** 2 n = np.linalg.norm([a, b, d]) q = (n - 3 / 4) / n res = [A, -4 * p2 * q * a, B, -4 * p2 * q * b, D, -4 * p2 * q * d - p2] return res def solver(t, pinit): return odeint(func, pinit, t) time = np.arange(0, 500.0, 0.01) solution = solver(time, ci) plt.plot(solution[:,0], solution[:,2]) plt.show() ```Here I assumed that the state vector had the six coordinates a, b, d, and their derivatives which I called A, B, D. I may be wrong in the order of these parameters, so please explain the correct order of the variables. Don't use lambda functions if the expression is too long. It makes code difficult to read. scidam likes this post SkewedZone Unladen Swallow Posts: 2 Threads: 1 Joined: Jun 2019 Reputation: 0 Likes received: 0 #3 Jun-25-2019, 12:14 PM Yes, the vector you assumed was the correct one, and thanks for this information regarding the usage (or lack thereof) of lambda functions. It is properly working now and giving the results it should be giving. Thank you a lot! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post construction of Neural Network for solving Differential equations arshad 0 114 Jun-04-2020, 09:20 AM Last Post: arshad Fitting experimental data with differential equations : Optimization madoko 6 1,657 Jan-17-2019, 11:30 AM Last Post: scidam Solving nth degree simultaneous equations hegdep 3 1,595 Sep-28-2017, 08:33 PM Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)