numerical-methods

newtons methods implementation

爷,独闯天下 提交于 2019-12-08 11:56:31
问题 i have posted a few hours ago question about newtons method,i got answers and want to thanks everybody,now i have tried to implement code itself #include <iostream> #include <math.h> using namespace std; #define h powf(10,-7) #define PI 180 float funct(float x){ return cos(x)-x; } float derivative (float x){ return (( funct(x+h)-funct(x-h))/(2*h)); } int main(){ float tol=.001; int N=3; float p0=PI/4; float p=0; int i=1; while(i<N){ p=p0-(float)funct(p0)/derivative(p0); if ((p-p0)<tol){ cout<

odeint memory usage and execution time

六月ゝ 毕业季﹏ 提交于 2019-12-08 11:34:59
问题 I've noticed that odeint uses very little memory when compared to my implementation of the RK4 algorithm or Mathematica. For the same step size, odeint uses about 3.11GB while my program uses 7GB and with Mathematica, I have to manually increase the pagefile size to 40GB or else it runs out of memory. (Edit: CPU usage is only 18% ) I am curious on how this is possible because when I save the results, the data file is almost the same size in all three cases. However, when it comes to execution

Parallel exact matrix diagonalization with Python

那年仲夏 提交于 2019-12-08 05:08:07
问题 Is anyone aware of an implemented version (perhaps using scipy/numpy) of parallel exact matrix diagonalization (equivalently, finding the eigensystem)? If it helps, my matrices are symmetric and sparse. I would hate to spend a day reinventing the wheel. EDIT: My matrices are at least 10,000x10,000 (but, preferably, at least 20 times larger). For now, I only have access to a 4-core Intel machine (with hyperthreading, so 2 processes per core), ~3.0Ghz each with 12GB of RAM. I may later have

Triangular indices for multidimensional arrays in numpy

梦想的初衷 提交于 2019-12-08 04:03:37
问题 We know that np.triu_indices returns the indices of the triangular upper part of a matrix, an array with two dimensions. What if one wants to create indices as in the following code? indices = [] for i in range(0,n): for j in range(i+1,n): for k in range(j+1,n): indices.append([i,j,k]) in a num-pythonic way? 回答1: In general you can get a list of indexes that follow the logic of the code you put with from itertools import combinations ndim = 3 # number of dimensions n = 5 # dimension's length

Finding Numerical Derivative using a shift matrix in MATLAB

夙愿已清 提交于 2019-12-08 03:47:44
问题 We were given an assignment to find the numerical first and second derivative using given steps: 1) Define two arrays, x and y=f (x) (Use any function) 2) Define the differential operator as a matrix (e.g. for f' , define the matrix D= (1/2h)(U-L) where U is an Uppershift Matrix, and L is Lowershift Matrix. 3) Calculate the derivative by multiplying the differential operator matrix by the function array. I have written this code: %QUESTION 3- DIFFERENTIAL OPERATOR h=2; x = 2:h:8 y = x.^2 %the

Numerical Differentiation

五迷三道 提交于 2019-12-08 02:45:09
问题 How can I calculate the numerical second derivative of a function involving an exponential and a singularity at infinity. Unfortunately, the numerical derivative by Ridder's methods provided in "Numerical Recipes in C" can only calculate the first derivative (It requires analytical expression of the function beforehand.) Furthermore I have tried Chebyshev approximation and differentiating the function afterwards but the values given were way off the actual values. I have also tried some

Fastest numerical solution of a real cubic polynomial?

被刻印的时光 ゝ 提交于 2019-12-07 20:11:06
问题 R question: Looking for the fastest way to NUMERICALLY solve a bunch of arbitrary cubics known to have real coeffs and three real roots. The polyroot function in R is reported to use Jenkins-Traub's algorithm 419 for complex polynomials, but for real polynomials the authors refer to their earlier work. What are the faster options for a real cubic, or more generally for a real polynomial? 回答1: Fleshing out Arietta's answer above: > a <- c(1,3,-4) > m <- matrix(c(0,0,-a[1],1,0,-a[2],0,1,-a[3]),

SQL Numeric data type truncating value?

☆樱花仙子☆ 提交于 2019-12-07 11:55:16
问题 I really hope some SQL guru out there can assist with this one (and my apologies if this has been answered before. I did try and find a similar post but to no avail): declare @theanswer numeric(38,16) select @theanswer = 0.01 / 0.0074464347 select @theanswer The above results in 1.3429245542165000 but the following (which looks the same to me) declare @val1 numeric(38,16); declare @val2 numeric(38,16); set @val1 = 0.01; set @val2 = 0.0074464347; select @val1/@val2 results with 1.342924 and

std::pow gives a wrong approximation for fractional exponents

拜拜、爱过 提交于 2019-12-07 04:59:07
问题 Here is what I mean trying to do double x=1.1402 double pow=1/3; std::pow(x,pow) -1; result is 0 but I expect 0.4465 the equation is (1 + x) ^3= 1.1402 , find x . 回答1: 1/3 is done as integer arithmetic, so you're assigning 0 to pow . Try pow(x, 1.0/3.0); 回答2: 1/3 is 0. That's integer division. Try: double pow = 1.0 / 3.0; For: #include <iostream> #include <cmath> int main(void) { double x = 1.1402; double pow = 1.0/3.0; std::cout << std::pow(x, pow) - 1; } 回答3: Many have stated that 1/3 = 0,

Solving a delay differential equation (DDE) system constrained to give nonnegative solutions

若如初见. 提交于 2019-12-07 04:23:18
问题 In MATLAB, ode45 has a parameter called NonNegative which constrains the solutions to be nonnegative. They even wrote a paper about how this method works and how it's not something as stupid as just setting y_i to 0 whenever it becomes negative, as that won't generally work. Now, MATLAB also has dde23 for solving delay differential equations, but there is no equivalent NonNegative parameter for this integrator. Unfortunately, I am tasked with adding a delay to an existing ODE system which is