# numerical-methods

问题 I am trying to write a short C++ routine to calculate the following function F(i,j,z) for given integers j > i (typically they lie between 0 and 100) and complex number z (bounded by |z| < 100), where L are the associated Laguerre Polynomials: The issue is that I want this function to be callable from within a CUDA kernel (i.e. with a __device__ attribute). Standard library/Boost/etc functions are therefore out of the questions, unless they are simple enough to re-implement on my own - this

## Matlab: Is it possible to numerically solve a system of ode's with a mixture of initial and terminal conditions?

问题 I'm trying to use ode45 to solve a system of ODE's: [X,Y]= ode45(@sys,[0, T],y0); where, function dy = sys(t,y) dy(1) = f_1(y) dy(2) = f_2(y) dy(3) = f_3(y) end The problem is that the function ode45 requires that y0 be initial values [y_1(0), y_2(0), y_3(0)] , while in my system, I only have the values [y_2(0), y_3(0), y_3(T)] available. Mathematically, this set of initial/terminal conditions should be enough to pin down the system, but is there any way I can work with that by ode45 or any

## Python implementation of n-body problem issue

问题 I am currently trying to implement the N-body problem using Euler's method for solving differential equations. However, the graphical outputs do not seem correct, and I'm not sure where the issue in my code is after a while of testing. I'm currently using approximate values for Alpha Centauri A and B to test. This is my code: import numpy as np import matplotlib.pyplot as plt from math import floor # gravitation constant G = 6.67430e-11 # astronomical units au = 1.496e11 sec_in_day = 60 * 60

## Getting eigenvalues from 3x3 matrix in Python using Power method

问题 I'm trying to get all eigenvalues from a 3x3 matrix by using Power Method in Python. However my method returns diffrent eigenvalues from the correct ones for some reason. My matrix: A = [[1, 2, 3], [2, 4, 5], [3, 5,-1]] Correct eigenvalues: [ 8.54851285, -4.57408723, 0.02557437 ] Eigenvalues returned by my method: [ 8.5485128481521926, 4.5740872291939381, 9.148174458392436 ] So the first one is correct, second one has wrong sign and the third one is all wrong. I don't know what I'm doing

## Unsure about how to use event function in Matlab

问题 I am trying to plot a state-space diagram, as well as a time-history diagram, of a dynamical system. There's a catch, though. The state-space is divided into two halves by a plane located at x1 = 0. The state-space axes are x1, x2, x3. The x1 = 0 plane is parallel to the x2/x3 plane. The state-space above the x1 = 0 plane is described by the ODEs in eqx3, whereas the state-space below the x1 = 0 plane is described by the ODEs in eqx4. So, there is a discontinuity on the plane x1 = 0. I have a

## Runge-Kutta 4th order to solve 2nd order ODE system using Python

问题 I'm trying to solve system of two odes numerically by runge-kutta 4th order method. initial system: system to solve: And I have very strange solution graph... I have: Correct graph: I can't find trouble in my runge-kutta. Please, help me. My code is here: dt = 0.04 #initial conditions t.append(0) zdot.append(0) z.append(A) thetadot.append(0) theta.append(B) #derrive functions def zdotdot(z_cur, theta_cur): return -omega_z * z_cur - epsilon / 2 / m * theta_cur def thetadotdot(z_cur, theta_cur)

## Numerical integration: Why does my orbit simulation yield the wrong result?

问题 I read Feynman's Lecture on Physics Chapter 9 and tried to my own simulation. I used Riemann integrals to calculate velocity and position. Although all start-entry is same, my orbit look's like a hyperbola. Here is lecture note: https://www.feynmanlectures.caltech.edu/I_09.html (Table 9.2) import time import matplotlib.pyplot as plt x=list() y=list() x_in=0.5 y_in=0.0 x.append(x_in) y.append(y_in) class Planet: def __init__(self,m,rx,ry,vx,vy,G=1): self.m=m self.rx=rx self.ry=ry self.a=0 self

## How to find coefficients of polynomial equation?

问题 Given two points in the x, y plane: x, f(x) 1, 3 2, 5 I can interpolate them using Lagrange and find f(1.5) , which result in 4 . Thinking a little I managed to find a way to discover the coefficients of the equation: void l1Coefficients(const vector<double> &x, const vector<double> &y) { double a0 = y[0]/(x[0]-x[1]); double a1 = y[1]/(x[1]-x[0]); double b0 = (-x[1]*y[0])/(x[0]-x[1]); double b1 = (-x[0]*y[1])/(x[1]-x[0]); double a = a0 + a1; double b = b0 + b1; cout << "P1(x) = " << a << "x +

## Runge-kutta for coupled ODEs

问题 I’m building a function in Octave that can solve N coupled ordinary differential equation of the type: dx/dt = F(x,y,…,z,t) dy/dt = G(x,y,…,z,t) dz/dt = H(x,y,…,z,t) With any of these three methods (Euler, Heun and Runge-Kutta-4). The following code correspond to the function: function sol = coupled_ode(E, dfuns, steps, a, b, ini, method) range = b-a; h=range/steps; rows = (range/h)+1; columns = size(dfuns)(2)+1; sol= zeros(abs(rows),columns); heun=zeros(1,columns-1); for i=1:abs(rows) if i=

## Exact value of a floating-point number as a rational

问题 I'm looking for a method to convert the exact value of a floating-point number to a rational quotient of two integers, i.e. a / b , where b is not larger than a specified maximum denominator b_max . If satisfying the condition b <= b_max is impossible, then the result falls back to the best approximation which still satisfies the condition. Hold on. There are a lot of questions/answers here about the best rational approximation of a truncated real number which is represented as a floating