numerical-methods,ode,newtons-method,numerical-stability

Usually you chose the solution that is closest to y(i). The correct solution should satisfy y(i+1)=y(i)+h*f(x(i),y(i)) + O(h²) However, for stiff problems the constant in O(h²) can be very large so that this relation is not as helpful as it seems. If L is the Lipschitz constant of f and...

matlab,matrix,regression,numerical-methods

This sounds like a regression problem. Assuming that the unexplained errors in measurements are Gaussian distributed, you can find the parameters via least squares. Basically, you'd have to rewrite the equation so that you get this to the form of ma + nb + oc = p and then you...

python,python-2.7,debugging,numerical-methods,runge-kutta

Unfortunately, you fell into the same trap I've occasionally fallen into: your initial x0 array contains integers, and thus, all resulting x[i] values will be converted to an integer after calculation. Why is that? Because int is the type of your initial conditions: x0 = 500 y0 = 20 The...

You should be getting a compiler error. The first argument to Min_Search_Golden_Section should be a function pointer, but you pass the address of a variable instead. When you get compiler errors, fix them - don't run the program and hope. :) I guess you just meant to write: Min_Search_Golden_Section( &sp_gamma,...

python,numpy,numeric,numerical-methods

I think that what you you are seeing is how Python's float type handles rounding when it runs out of precision. The Wikipedia text you quoted describing epsilon appears to leave this part out. In your example, 1 + delta gets rounded up to 1 + epsilon. Even though a...

Your fx11 function isn't properly vectorized. Try integrate(Vectorize(fx11),lower=1,upper=1.1)$value The problem is that integrate will pass in a vector of values at once to the function you are trying to integrate, it does not evaluate each point separately. Your function works when z0 is of length one uniroot(fx01,interval=c(0,10),z0=1)$root # [1] 1.050555...

matlab,octave,numerical-methods,numerical

Method #1 - Using (-1)^x Just use a linear increment operator to go from 0 to 200000 and multiply the sequence by (-1)^(x+1) to allow the sign of the sequence to alternate: x = 0:200000; y = ((-1).^(x+1)) .* x; The addition of the +1 is important so that the...

c,arrays,matrix,linear-algebra,numerical-methods

jacobi_helper is called with the argument double *u, which is a pointer to a memory address where the results may be stored, but the first thing jacobi_helper does is u = a which means: forget that memory address u and use a instead. You set some values in the array...

matlab,matrix,numerical-methods,differential-equations,differentiation

Because this is a numerical approximation to the solution of the ODE, you are seeking to find a numerical vector that is representative of the solution to this ODE from time x=0 to x=1. This means that your boundary conditions make it so that the solution is only valid between...

matlab,for-loop,graph,numerical-methods

There are many, many ways, here is one that exposes a few options: clc; clear all, close all; r1 = zeros(10, 1); r2 = zeros(10, 1); x = 1:10; for i=1:10 r1(i) = rand(1); r2(i) = max(rand(1,2)); end figure('Name', 'Values of r1 and r2', 'NumberTitle', 'off'); hold on; axis([0, 11,...

python,algorithm,recursion,numerical-methods,numerical-integration

Your code is refining to meet an error tolerance in each individual subinterval. It's also using a low-order integration rule. Improvements in both of these can significantly reduce the number of function evaluations. Rather than considering the error in each subinterval separately, more advanced codes compute the total error over...

python,numpy,numerical-methods

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 (assuming equal length in each dimension) indices = list(combinations(range(n), r=ndim) or if you want...

c++,arrays,algorithm,for-loop,numerical-methods

Why not try this for(int i=0; i<4 ; ++i) { if( i != j ) cout << planets[i].getvx() << "\n"; } This is the simplest method I could think of without trying anything complex. You just have to check whether i is equal to j inside the loop. If i...

numeric,floating-accuracy,numerical-methods

In general if you want precise results in physical based rendering you don't want to use floats or doubles since they have massive rounding problems and thus introduce errors in your simulation. If you need or want to stick with floats/double you probably should rescale around zero. The reason is...

multidimensional-array,julia-lang,numerical-methods

I'm not too familiar with diff, but from what I understand about what its doing I've made a n-dimensional implementation, that uses Julia features like parametric types and splatting: function mydiff{T,N}(A::Array{T,N}, dim::Int) @assert dim <= N idxs_1 = [1:size(A,i) for i in 1:N] idxs_2 = copy(idxs_1) idxs_1[dim] = 1:(size(A,dim)-1) idxs_2[dim]...

matlab,numerical-methods,newtons-method

According to https://de.mathworks.com/matlabcentral/newsreader/view_thread/23776, numjac is exclusively intended to be used for ODE functions of the format doty = f(t,y). And n_df should be the derivative of n_f, thus use the derivative of f in the iteration point x. Thus n_f = @(x) x - Y(i) - h*f(T(i+1),x); n_df = @(x)...

matlab,matrix,numerical-methods

That's pretty easy to do. Simply loop through values of w and determine what the total number of iterations is at each w. Once the function finishes, check to see if this is the current minimum number of iterations required to get a solution. If it is, then update what...

fortran,gfortran,numerical-methods

xerror is an error reporting routine. Looking at the way it is called, it appears to use Hollerith constants (the ones where "foo" is written as 3hfoo). if(ier.ne.0) call xerror(26habnormal return from qag , * 26,ier,lvl) xerror in turn calls xerrwv, passing along the arguments (plus a few more). This...

matlab,geometry,octave,numerical-methods

Using MATLAB built-in functions The simplest way to do it would be to define the function as it is, then use the fsolve() function. For example (the code is not tested): function y = cctang(t) %t is a 1-by-4 line vector (first two members = t1, last two members =...

python,python-2.7,numerical-methods,numerical,differential-equations

Using Euler for mechanical systems is in general a bad idea. The easiest test case to explore this statement is the simple oscillator x''+x=0 where you will find that the energy of the system grows rapidly. For a general mechanical system you have an equation of motion m*x'' = F(t,x,x')....

Your formula for c is wrong, it should be c = b - (f(b) * (b - a)) / (f(b) - f(a)); see here To prevent reaching MAX_ITER iterations, you may want to watch the change in c something like previousValue = c; c = b - (f(b) * (b...

c#,algorithm,simulation,physics,numerical-methods

Your model calculates the gravity force between two particles twice: for the first particle the force is based on their original coordinates, and for the second particle it is based on an updated position of the first one. This is a clear violation of the Newton's 3rd law. You must...

python,numpy,numerical-methods

I don't understand what exactly your problem is, but here are some things you can do to have A[0] = 0. You can create A to be longer by one index to have the zero as the first entry: # initialize example data import numpy as np B = 1...

matlab,math,numerical-methods,newtons-method

I didn't go through all the code, but at least the first coefficient of the Jacobian should be 0.32 instead of 0.8 to match the function defined one line above. Having the wrong Jacobian can lead to sub-quadratic convergence, although still achieving convergence in some cases.

Unfortunatly you did not tell us what the function sqroot is. I assume it is sqrt. But then you may not start with xlo=-5.0; Set xlo= 0. In the main program ther is a missing bracket. I added one. for (int i=0;i<=nplot;i++) { double xplot=xlo+i*hplot; double fplot; newtondd(xsten, sqrt, n,...

This type of systems is called differential-agebraic equation. There are standard solvers in netlib, DASSL for example. One has to be careful to use the method that covers the (differentiation-)index of the DAE. Typically, solvers cover index-1 or index-2 systems. If the index is higher, one has to use symbolic...

c++,algorithm,for-loop,numerical-methods,runge-kutta

You can do this easily in a loop if you have a C++11 compiler: for (auto& p : {p0, p1, p2, p3}} { cout << p.getvx() << "\n"; } Otherwise the easiest solution would be to make the planets an array rather than four individual objects. Edit: To use an...

octave,numerical-methods,numerical-integration

I have tried your function in Matlab. Your code is not stalling. It is rather that the size of trap_1_midpoints increases exponentionaly. With that the computation time of trap_1 increases also exponentionaly. This is what you experience as stalling. I also found a possible bug in your code. I guess...

numbers,numerical-methods,logarithm

Log is numerical stable but division not so. Imagine y and z are very close to 0. Then y * z has a great probability to be evaluated to 0 and x / (y * z) to overflow. But log(y) even for very small y gives accurate results. So log(x)-log(y)-log(z)...

algorithm,optimization,numerical-methods

For this setting, I'd go with Golden Section Search. Convexity implies unimodality, which is needed by this method. Conversely, this method does not need derivatives. You can find derivatives numerically, but that's another way of saying "multiple function assessments"; might as well use these for golden-section partitions. ...

matlab,equation,numerical-methods,exponential

Here is some code that can find the best solution to your problem, if there is one. In this case, there is no reasonable solution, but defining A by M([4 2]) (for example) does work reasonably well. A = [1 2 3; 3 4 1; 2 4 4] %// the...

matlab,matrix,numerical-methods

The reason why your code isn't working is due to the logic of your if statements inside your for loops. Specifically, you need to accumulate all values for a particular row that don't belong to the diagonal of that row first. Once that's done, you then perform the division. You...

matlab,matrix,numerical-methods

This code for the most part does what it intends to do: compute the numerical approximation to the derivative. However, the way you're computing the derivative is slightly incorrect. Basically, for each point in your array, you want to subtract the point to the left with the point to the...

matlab,numerical-methods,newtons-method

Try replacing the following line u1 = u' + inv(DF)*F'; with u1 = u' - inv(DF)*F'; When I make this change, the solution I get is [60,0,-60] which does correctly solve the three equations....

c++,floating-point,precision,floating-accuracy,numerical-methods

Will it be more stable (or faster, or at all different) in general to implement a simple function to perform log(n) iterated multiplies if the exponent is known to be an integer? The result of exponentiation by squaring for integer exponents is in general less accurate than pow, but...

Let's say that f[i,j] is the value at node (i,j), and h is the size of space step. You already know how to calculate second order derivatives of f, for example fxx[i,j] = (f[i+1,j]-2*f[i,j]+f[i-1,j])/h^2 fyy[i,j] = (f[i,j+1]-2*f[i,j]+f[i,j-1])/h^2 fxy[i,j] = (f[i+1,j+1]-f[i+1,j-1]-f[i-1,j+1]+f[i-1,j-1])/h^2 These are of second degree of accuracy, that is the...

algorithm,numerical-methods,numerical,numerical-integration

numerical integration always return just a number if you do not want the number but function instead then you can not use numerical integration for this task directly Polynomial approach you can use any approximation/interpolation technique to obtain a polynomial representing f(x) then integrate as standard polynomial (just change...

Your best bet is to check what actual implementations do, here is a selection: Boost: http://www.boost.org/doc/libs/1_55_0/boost/math/special_functions/erf.hpp GNU Scientific Library: http://www.gnu.org/software/gsl/ GLibc http://www.gnu.org/software/libc/index.html There are probably others....

matlab,numerical-methods,numerical-integration

You are changing the parameters of your your ODEs discontinuously in time. This results in a very stiff system and less accurate, or even completely wrong, results. In this case, because the your ODE is so simple when I = 0, an adaptive solver like ode45 will take very large...

Your matrix becomes ill-conditioned for large D (degree of the approximation polynomial). You can try to use different kind of polynomial base to do the regression. For e.g. used Chebyshev polynomials instead of x, x^2, x^3, ... , x^D. You can obtain a slightly better result if you center and...

matlab,graph,numerical-methods

Well, you've already created arrays for sin and exp stored in y and z respectively. These arrays were created on the same domain as x. You just need to multiply both arrays together element-wise and plot the graph. It's as simple as doing: plot(x, z.*y); Here, .* stands for element-wise...

python,scipy,sympy,numerical-methods,differential-equations

Here and here are some examples. As for your problem, you can write your equation like: y' + p(t)y - q(t) = 0 and then use dsolve(). import sympy t = sympy.Symbol('t') y = sympy.Function('y')(t) p = sympy.Function('p')(t) q = sympy.Function('q')(t) y_ = sympy.Derivative(y, t) # y' + p(t)y -...

python,numpy,numerical-methods

Two fixes are needed inside integrate_iter. First, when you increment i, you need to not only add 1 to i, but redefine i as i + 1. Second, in your posted code, integrate_iter returns None when the else block is executed. This prevents the improved result from being obtained within...

math,numerical-methods,raytracing,numerical,embree

I think a lot of the problem is using single precision float rather than double precision. Define two functions double dsqr(double x) { return x*x; } double ddot(const Vec3fa &a,Vec3fa &b) { double x1 = a.x, y1 = a.y, z1 = a.z; double x2 = b.x, y2 = b.y, z2...

The difference between a Jacobi solver and a Gauss-Seidel solver is that when you're solving for the solution of a variable x_i at the current iteration, you need to use the information from the previous variables (x_1, x_2, ..., x_{i-1}) as part of the solution for the current variable x_i....

c++,floating-point,precision,floating-accuracy,numerical-methods

You are correct. The precision from 1.0 to 2.0 will be uniform across the surface, like you are using fixed point. The precision from -0.5 to 0.5 will be highest around the center point, and lower near the edges (but still quite good). The precision from 0.0 to 1.0 will...

heres an example, integrating 1/(t^2 + 1) over [0,1000]. It uses adaptive integration with the simplest ruleset since there are no singularities. #include <stdio.h> #include <math.h> #include <gsl/gsl_integration.h> double f (double x, void * params) { double alpha = *(double *) params; double f = alpha / (x * x...

python,scipy,numerical-methods

(This is more an extended comment about the problem than an answer to what to do about it.) This looks like a bug in ellipkinc. I get just one floating point value where the function returns nan, and four adjacent floating point values where the function returns twice the "correct"...

floating-point,precision,numerical-methods

Exponentiation magnifies relative error and, by extension, ulp error. Consider this illustrative example: float x = 0x1.fffffep6; printf ("x=%a %15.8e exp2(x)=%a %15.8e\n", x, x, exp2f (x), exp2f(x)); x = nextafterf (x, 0.0f); printf ("x=%a %15.8e exp2(x)=%a %15.8e\n", x, x, exp2f (x), exp2f(x)); This will print something like x=0x1.fffffep+6 1.27999992e+02 exp2(x)=0x1.ffff4ep+127...

matlab,regression,numerical-methods,linear

Judging from the link you provided, and my understanding of your problem, you want to calculate the line of best fit for a set of data points. You also want to do this from first principles. This will require some basic Calculus as well as some linear algebra for solving...

When performing the integration, MATLAB (or most any other integration software) computes a low-order solution qLow and a high-order solution qHigh. There are a number of different methods of computing the true error (i.e., how far either qLow or qHigh is from the actual solution qTrue), but MATLAB simply computes...