numerical-methods

Conversion of symbolic expression to numeric one for use in quad - use lambdify?

拟墨画扇 提交于 2019-12-11 15:29:45
问题 I want to convert an expression containing symbolic variables to a numeric one so that the expression may be subsequently used in an integration method 'quad'. import numpy import math as m import scipy import sympy #define constants gammaee = 5.55e-6 MJpsi = 3.096916 alphaem = 1/137 lambdasq = 0.09 Ca = 3 qOsq = 2 def qbarsq(qsq): return (qsq+MJpsi**2)/4 def xx(qbarsq, w): return 4*qbarsq/(4*qbarsq-MJpsi**2+w**2) from sympy import * x,NN,a,b,ktsq,qbarsq,w = symbols('x NN a b ktsq qbarsq w')

Souriau method for Characteristic Polynomial

放肆的年华 提交于 2019-12-11 11:23:41
问题 Does anyone know the Souriau method for finding the characteristic polynomial of any n × n matrix? I found out the first coefficient, is obvious, but how can I find out the other coefficients? After I need to inverse the matrix but I know how. #include <iostream> #include <fstream> using namespace std; double trace(double a[5][5],int n){ int i; double trace=0; for(i=0;i<n;i++) trace+=a[i][i]; return trace; } double prod(double a[5][5],double b[5][5],int n) { double c[5][5]; int i,j,k; cout <<

Goldberg's log1p vs. gsl_log1p

为君一笑 提交于 2019-12-11 10:09:20
问题 I am looking for a simple portable implementation of log1p. I have come across two implementations. The first one appears as Theorem 4 here http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html, An implementation of the above double log1p(double p) { volatile double y = p; return ( (1 + y) == 1 ) ? y : y * ( log( 1 + y) / ( ( 1 + y) - 1 ) ); } The second one is in GSL http://fossies.org/dox/gsl-1.16/log1p_8c_source.html double gsl_log1p (const double x) { volatile double y, z; y = 1

C program - taylor series_long formula

女生的网名这么多〃 提交于 2019-12-11 09:54:56
问题 This formula is from a friend of mine --- and I fixed it up for him. But I can't seem to figure out of how to get the right sine calculations per angle. Can someone please help me in getting the right commands in the sin part? Code: #include<stdio.h> #define PI 3.141592653589 #define NUMBER_OF_TERMS 10 double factorial(double x) { double counter, total; counter=x; total=x; while(counter>1) { counter--; total = total * counter; } return total; } double power(double x, double y) { double

vector field on gnuplot with u and v components

十年热恋 提交于 2019-12-11 09:32:08
问题 I'm solving Navier-Stokes equation for incompressible fluid flow through a square region with obstacle. As an output I get X and Y components of velocity as NxN matrix each. How to plot vector field for it in gnuplot. I found this answer but I can't understand what values to put for x, y, dx, dy . Can anyone explain how to use my output to plot vector field? UPDATE I tried doing as @LutzL said, but something seems to be wrong with my code. Is everything is right with this code? int main() {

Find the root of a multivariable equation using scipy.optimize

若如初见. 提交于 2019-12-11 07:36:47
问题 I have the following functions import numpy as np import scipy.optimize as optimize def x(theta1, theta2, w, h, L1, L2): sint1 = np.sin(theta1) cost1 = np.cos(theta1) sint2 = np.sin(theta2) cost2 = np.cos(theta2) i1 = L1 * (cost1 + cost2) + w j1 = L1 * (sint1 - sint2) - h D = np.sqrt((L1*(cost2-cost1)+w)**2+(L1*(sint2-sint1)+h)**2) a = (0.25)*np.sqrt((4*L2**2-D**2)*D**2) return i1/2 + 2*j1*a/(D**2) def y(theta1, theta2, w, h, L1, L2): sint1 = np.sin(theta1) cost1 = np.cos(theta1) sint2 = np

Finding the solution of a summation of exponentials

一曲冷凌霜 提交于 2019-12-11 07:21:33
问题 I'm trying to solve numerically this equation in Python (numpy/scipy, everything is available) In this formula K is a constant, f and g are two terms those depends on the E counter (this is a discrete representation of an integral) where x is the variable I'm looking for. As an example, with E being 3 terms that'd be: also f(E) and g(E) are known. I read about using "fsolve" from numpy, although I can't understand how to automatically generate a function that's a summation of terms. I may do

Verlet algorithm implementation in Python

本秂侑毒 提交于 2019-12-11 04:35:16
问题 I have problem with implementation of verlet algorithm in Python. I tried this code: x[0] = 1 v[0] = 0 t[0] = 0 a[0] = 1 for i in range (0, 1000): x[i+1] = x[i] - v[i] * dt + (a[i] * (dt**2) * 0.5) v[i] = (x[i+1] - x[i-1]) * 0.5 * dt t[i+1] = t[i] + dt But it is not working properly. What is wrong? I am looking for general code for Verlet algorithm. 回答1: Your question isn't very clear, but there are a few sources of error in your code. Eg, for i > 0 x[i+1] = x[i]-v[i]*dt+(a[i]*(dt**2)*0.5)

Discontinuity in results when using scipy.integrate.quad

*爱你&永不变心* 提交于 2019-12-11 02:29:04
问题 I've discovered a strange behavior when using scipy.integrate.quad. This behavior also shows up in Octave's quad function, which leads me to believe that it may have something to do with QUADPACK itself. Interestingly enough, using the exact same Octave code, this behavior does not show up in MATLAB. On to the question. I'm numerically integrating a lognormal distribution over various bounds. For F is cdf of lognormal, a is lower bound and b is upper bound, I find that under some conditions,

Reduced Row Echelon Form (rref)

懵懂的女人 提交于 2019-12-10 18:16:54
问题 I found that rref in Matlab does the gaussian elimination with patial pivoting Let A for example be a 5x9 matrix: 0.4898 0.2760 0.4984 0.7513 0.9593 0.8407 0.3500 0.3517 0.2858 0.4456 0.6797 0.9597 0.2551 0.5472 0.2543 0.1966 0.8308 0.7572 0.6463 0.6551 0.3404 0.5060 0.1386 0.8143 0.2511 0.5853 0.7537 0.7094 0.1626 0.5853 0.6991 0.1493 0.2435 0.6160 0.5497 0.3804 0.7547 0.1190 0.2238 0.8909 0.2575 0.9293 0.4733 0.9172 0.5678 rref(A) gives: 1.0000 0 0 0 0 10.9716 -6.2494 33.3062 16.0275 0 1