Working out an equation

2024/7/7 5:29:21

I'm trying to solve a differential equation numerically, and am writing an equation that will give me an array of the solution to each time point.

import numpy as np
import matplotlib.pylab as pltpi=np.pi
sin=np.sin
cos=np.cos
sqrt=np.sqrt
alpha=pi/4 
g=9.80665
y0=0.0
theta0=0.0sina = sin(alpha)**2
second_term = g*sin(alpha)*cos(alpha)x0 = float(raw_input('What is the initial x in meters?'))
x_vel0 = float(raw_input('What is the initial velocity in the x direction in m/s?'))
y_vel0 = float(raw_input('what is the initial velocity in the y direction in m/s?'))
t_f = int(raw_input('What is the maximum time in seconds?'))r0 = x0
vtan = sqrt(x_vel0**2+y_vel0**2)
dt = 1000
n = range(0,t_f)
r_n = r0*(n*dt)
r_nm1 = r0((n-1)*dt)
F_r = ((vtan**2)/r_n)*sina-second_term
r_np1 = 2*r_n - r_nm1 + dt**2 * F_r
data = [r0]for time in n:data.append(float(r_np1))
print data

I'm not sure how to make the equation solve for r_np1 at each time in the range n. I'm still new to Python and would like some help understanding how to do something like this.

Answer

First issue is:

n = range(0,t_f) r_n = r0*(n*dt)

Here you define n as a list and try to multiply the list n with the integer dt. This will not work. Pure Python is NOT a vectorized language like NumPy or Matlab where you can do vector multiplication like this. You could make this line work with

n = np.arange(0,t_f) r_n = r0*(n*dt),

but you don't have to. Instead, you should move everything inside the for loop to do the calculation at each timestep. At the present point, you do the calculation once, then add the same only result t_f times to the data list.

Of course, you have to leave your initial conditions (which is a key part of ODE solving) OUTSIDE of the loop, because they only affect the first step of the solution, not all of them.

So:

# Initial conditions
r0 = x0
data = [r0]# Loop along timesteps
for n in range(t_f):# calculations performed at each timestepvtan = sqrt(x_vel0**2+y_vel0**2)dt = 1000r_n = r0*(n*dt)r_nm1 = r0*((n-1)*dt)F_r = ((vtan**2)/r_n)*sina-second_termr_np1 = 2*r_n - r_nm1 + dt**2 * F_r# append result to output listdata.append(float(r_np1))# do something with output list
print data
plt.plot(data)
plt.show()

I did not add any piece of code, only rearranged your lines. Notice that the part:

n = range(0,t_f)
for time in n:

Can be simplified to:

for time in range(0,t_f):

However, you use n as a time variable in the calculation (previously - and wrongly - defined as a list instead of a single number). Thus you can write:

for n in range(0,t_f):

Note 1: I do not know if this code is right mathematically, as I don't even know the equation you're solving. The code runs now and provides a result - you have to check if the result is good.

Note 2: Pure Python is not the best tool for this purpose. You should try some highly optimized built-ins of SciPy for ODE solving, as you have already got hints in the comments.

https://en.xdnf.cn/q/120434.html

Related Q&A

combine rows and add up value in dataframe

I got a dataframe(named table) with 6 columns labeled as [price1,price2,price3,time,type,volume]for type, I got Q and T, arranged like:QTQTTQNow I want to combine the rows with consecutive T and add up…

How to access a part of an element from a list?

import cv2 import os import glob import pandas as pd from pylibdmtx import pylibdmtx import xlsxwriter# co de for scanningimg_dir = "C:\\images" # Enter Directory of all images data_path = os…

How to get invisible data from website with BeautifulSoup

I need fiverr service delivery times but I could get just first packages(Basic) delivery time. How can I get second and third packages delivery time? Is there any chance I can get it without using Sel…

How to get rid of \n and in my json file

thanks for reading I am creating a json file as a result of an API that I am using. My issue is that the outcome gets has \h and in it and a .json file does not process the \n but keeps them, so the f…

Python code to calculate the maximal amount of baggage is allowed using recursive function

I am new to python and I have an assignment, I need to write a recursive function that takes two arguments (Weights, W), weights is the list of weights of baggage and W is the maximal weight a student …

How to flatten a nested dictionary? [duplicate]

This question already has answers here:Flatten nested dictionaries, compressing keys(32 answers)Closed 10 years ago.Is there a native function to flatten a nested dictionary to an output dictionary whe…

Find an element in a list of tuples in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

print dictionary values which are inside a list in python

I am trying to print out just the dict values inside a list in python.car_object = {}cursor = self._db.execute(SELECT IDENT, MAKE, MODEL, DISPLACEMENT, POWER, LUXURY FROM CARS)for row in cursor:objectn…

Triangle of numbers on Python

Im asked to write a loop system that prints the following:0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 0 1 2 3 4 5 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0However, my script prints this:0 1…

Using regex to ignore invalid syntax [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…