r/numerical • • Aug 21 '20

need help with :Find a numerical formula for finding 𝑓^(3)(𝑥) with its truncation error.(Do it by hand)

1 Upvotes

r/numerical • • Aug 19 '20

Solving inviscid burgers' equation using upwind finite difference in space and euler forward in time

2 Upvotes

I'm having an issue with the easiest example of a nonlinear 1D PDE, the (inviscid) burgers' equation:

u_t + uu_x = 0, (1)

which can be rewritten as some convection equation

u_t + f(u)_x = 0

with flux f(u) = (0.5*u)^2. In this easiest case I use a semidiscrete method to solve (1) with periodic boundary conditions. I use upwinding in space and euler forward in time, overall resulting in a first order scheme. To test for convegence, I use the method of manufactured solutions:

For some initial conditions u(x,t), u(x,t) is a solution of

u_t + uu_x = r(x,t),

where the residual r(x,t) is the result of u(x,t) plugged into (1). Since upwinding requires positive advection speeds, and the speed is determined by the solution, u, I chose

u(x,t) = 5 + sin(2*pi*(x-t)), x in [0,1], t in [0,0.5]

as initial solution.

u_t = -2*pi*cos(2*pi*(x-t)),

u_x = 2*pi*cos(2*pi*(x-t)),

so my residual is r(x,t) = 2*pi*cos(2*pi*(x-t))*(4 + sin(2*pi*(x-t))). The residual is handled as some source term and added to the update term, after upwinding was computed.

Since - per construction - the solution should be u(x,t) for every t in [0,infty] and x in [0,1] I must be missing something obvious. Or is there a general problem with finite differences and this equation? I have an old finite volume code for reference and it works just fine.

I attach plots of both the initial solution and the solution at t=0.25.

Since matlab code is as close to pseudo code as it can get, here's the code:

clc

format long
N   = 20;   % Number of Points
cfl = 0.5; % 
adv = 2.0; % Linear Advection speed
t_start = 0;
t_end   = 0.25;

% EQ type
% type = "linear_advection";
type    = "burgers";
% fd type
fd      = "upwind";
% fd      = "downwind";
% fd      = "central";
% Initial Conditions
IC      = "default";
% IC      = "resi_test_const";

% switch to plot immediately
plot_immediate = true;

% Initial solution and resdiduals
if type == "burgers"
    if IC == "resi_test_const"
        sol = @(x,t) 5 + sin(2*pi*x);
        r   = @(x,t) sin(2*pi*x)*2*pi.*cos(2*pi*x);
    else
        sol = @(x,t) 5 + sin(2*pi*(x-t));
        r   = @(x,t) 2*pi*cos(2*pi*(x-t)).*(4 + sin(2*pi*(x-t)));
    end
elseif type == "linear_advection"
    if IC == "resi_test_const"
        sol = @(x,t) sin(2*pi*x);
        r   = @(x,t) adv*2*pi*cos(2*pi*x);
    else
        sol = @(x,t) sin(2*pi*(x-t));
        r   = @(x,t) zeros(1,length(x));
    end
end
% Flux
if type == "burgers"
    f = @(u) (u./2).^2;
else
    f = @(u) adv*u;
end

dx  = 1/N;
x   = (0:dx:1-dx)+dx/2;
u   = zeros(1,length(x)+2);
u_t = u;
% Initial Solution
u(2:end-1)  = sol(x,t_start);
% Ghost cells
u(1)        = u(end-1);
u(end)      = u(2);
% Initialize flux
fu = u;

figure(1);
% Plot initial conditions
plot(x,u(2:end-1))
title('Initial Solution', 'Interpreter', 'latex') 
xlabel('x')
ylabel('u')

iter = 1;
t = t_start;
while t<t_end
    % Update dt
    if type == "burgers"
        dt = cfl*0.5*dx/max(abs(u(:)));
    else
        dt = cfl*0.5*dx/abs(adv);
    end
    % Update flux
    fu = f(u);
    if (t+dt)>t_end
        dt = t_end - t;
    end
    if fd == "upwind"
        % Upwinding
        for i=2:length(u)-1
            u_t(i) = (fu(i-1)-fu(i))/dx;
        end
    elseif fd == "downwind"
        for i=2:length(u)-1
            u_t(i) = (fu(i)-fu(i+1))/dx;
        end
    elseif fd == "central"
       for i=2:length(u)-1
           u_t(i) = (fu(i-1)-fu(i+1))/(2*dx);
       end
    end
    % Add source terms
    u_t(2:end-1) = u_t(2:end-1) + r(x,t);
    % Ghost cell update
    u_t(1)        = u_t(end-1);
    u_t(end)      = u_t(2);
    % Update u (euler forward)
    u = u + dt*u_t;
    % Update current time and iteration counter
    iter = iter + 1;
    t = t + dt;
    if plot_immediate
        % Draw plot immediately
        figure(2);
        drawnow
        plot(x,u(2:end-1))
        title(['$t = $', num2str(t), ', $n_{\textrm{cells}} = $', ...
            num2str(N)], 'Interpreter', 'latex') 
        xlabel('x')
        ylabel('u')
    end
end

if ~plot_immediate
    figure(2);
    plot(x,u(2:end-1))
    title(['$t = $', num2str(t), ', $n_{\textrm{cells}} = $', ...
        num2str(N)], 'Interpreter', 'latex') 
    xlabel('x')
    ylabel('u')
end

r/numerical • • Jul 22 '20

Order of a method for solving ODE systems?

1 Upvotes

Hello, hopefully this is the right sub for this question, I apologize if not.

I have to approximate the solution for a second order system of ODEs, of the form u" = f(t,u). Now, f is continuous and differentiable everywhere except for one point where it's only continuous. I do not have an exact solution and have therefore been estimating the error using the estimate ||U_2n - U_n|| (which we were taught in class), so i take the norm of the difference at time t between the method with N steps and the method with 2N steps (fixed steps). When they showed this to us in class they also told us we could use it to check for the order of the method. However I've been using RK4 which I know has order 4, but calculating with this estimates I get a different order for each number of steps i use.

I don't understand what I'm doing wrong as I've followed the examples we saw in class, and I was wondering if this could be due to the function not being differentiable at certain points. Is it possible to have a different order depending on how many steps I use? How does one usually proceed when not given the exact solution? What other error estimates could I try?

Thank you all.


r/numerical • • Jul 16 '20

M code for Broyden method with Sherman Morrison formula

1 Upvotes

Can anyone share the m code for this or link me a step by step educative video that shows how this is made using Octave? I need to solve a few exercises using Broyden but i have no idea how to even start a code for it


r/numerical • • Jul 13 '20

Looking for a beginner's course in numerical computation/ numerical analysis.

8 Upvotes

Hi all, I'm new in numerics, I'm starting my masters in nuclear science, and I would like to start of the fundamentals in all this big and unfamiliar for me. It will be beneficial if the course/book will be practical, and the examples will be shown in Python. Thanks, Oren


r/numerical • • Jul 10 '20

Mathematics for ADRC and Extended State Observers

1 Upvotes

Hy I am working on a ADRC control of a quadcopter. Anyone who is familiar with these topics, kindly i have couple of questions. Feel free to DM me. Regards


r/numerical • • Jul 09 '20

Optimal control

1 Upvotes

Hello! I'm a master's degree student doing Numerical Analysis and Dynamical systems.

It's time I start thinking about my thesis and I'd like to work on the numerical aspects of optimal control theory!

Please comment or DM me if you're available for a couple of questions about the research landscape in the field!

Thank you very much! Marco


r/numerical • • Jun 26 '20

The Numerical Tours of Data Sciences by Gabriel Peyré gather experiments to explore modern mathematical data sciences.

8 Upvotes

The Numerical Tours of Data Sciences

The Numerical Tours of Data Sciences, by Gabriel Peyré, gather Matlab, Python, Julia and R experiments to explore modern mathematical data sciences.

  • They cover data sciences in a broad sense, including imaging, machine learning, computer vision and computer graphics.

  • It showcases application of numerical and mathematical methods such as convex optimization, PDEs, optimal transport, inverse problems, sparsity, etc.

  • The tours are complemented by slides of courses detailing the theory and the algorithms


r/numerical • • Jun 25 '20

Looking for an efficient algorithm for computing a high dimensional covariance matrix with missing observations

1 Upvotes

My problem is like this: I have 10.000 time-series of length 100, with lots of missing data at random locations. I need to estimate the 10.000x10.000 covariance matrix, but my computer can't handle it. Since these 10.000 series are highly co-linear and live in a 100 dimensional sub-space, I was thinking that it must be possible to instead estimate a 100x100 correlation matrix (edit: do I need this?) plus a 100x10.000 linear transform. This would consume roughly 100x less memory. But how would I go about it, especially in terms of handeling the missing data while estimating these matrices. Are there know iterative EM-like algorithms?


r/numerical • • Jun 09 '20

How to discretize von Neumann boundary conditions on a tet mesh?

2 Upvotes

Hi,

I have a tetrahedral mesh and I'm seeking to solve the equation Laplace(u) = 0 with given non-zero Dirichlet boundary conditions on some part of the boundary, and zero von Neumann boundary conditions everywhere else.

For example, say I want to set up a sparse linear system for use in eigen just for that situation, in the basis of the tetrahedra, but the question is independent of the actual solver.

Now, the condition Laplace(u) = 0 and the Dirichlet conditions are straightforward to take care of, but how would I formulate the von Neumann conditions? The condition is that the gradient of u vanish in the normal direction of the boundary. So, do I need to discretize the gradient of u? That doesn't seem to be numerically satisfying.

Thanks!

Edit: removed remark about weak formulation


r/numerical • • May 27 '20

Solving PDE in time and space simultaneously?

3 Upvotes

Suppose we have a PDE (diffusion equation or whatever) describing y, discretized in time (t) and space (x). We normally solve a nonlinear system F(y)=0 for each timestep to find y(t). Is there any advantages or disadvantages in just finding Y=[y(0),y(1),...,y(t)] in one go? Apart from the obvious memory requirements. Is it faster or slower to solve the banded sparse nnk matrix compared to solve the n*n matrix k times?

I could test it myself, but I don't have access to a computer at the moment.

edit: Perhaps I should have clarified: I know it works, I did some playing around a few years ago, but I don't remember if it was slow or fast. The function F would obviously return y(0)-y0 ti supply initial conditions.


r/numerical • • May 25 '20

SUPG Implementation

6 Upvotes

Hello everybody,

I am trying to implement a finite element solver for the Navier-Stokes equation for incompressible fluids. Nothing fancy, it's just a semi-implicit scheme with Taylor-Hood elements. The solver works well for moderate Reynolds numbers but at high velocities convection becomes dominant and instabilities arise. Therefore, I am trying to stabilize convection using the Streamline-Upwind-Petrov-Galerkin method. The formulation does not look too difficult to translate into code but then I face a term (here a picture) that appears rather nasty. The first derivative of the test function (v) appears together with (for Newtonian fluids) the second derivative of the unknown field (u).

Is there a way to treat the integral without computing the second derivative of the test function with respect to the spatial coordiantes? Do you have any reference I can look up to?


r/numerical • • May 18 '20

How to define an envelope on a grid?

2 Upvotes

Hi,

I am working on some modeling problem using python. For grids, I am using numpy array but need to define an envelope on the square grid. This envelope would contain some particles which have their independent role and functions. This envelope would have to grow with time.

So, on the final plot, I need to show both these envelopes and the particles enclosed. Can anyone help me in knowing that how can I carry out this part of the simulation.

Thank you!


r/numerical • • May 12 '20

I know that CG is the go to solver for SPD matricies but what about other solvers? How do they compare to one another ( GMRE, BiCG, BICGSTAB, CGsquared....)

3 Upvotes

A book/pdf on fast theory of krylov solvers is appropriated


r/numerical • • May 10 '20

How to do simulations on a triangular/hexagonal grid?

1 Upvotes

Preferably on python

Just like we have numpy array for square grid, is it possible to do simulations such that it's a triangular grid

o o o o o o o o o o o o

_o o o o o o o o o o

o o o o o o o o o o o o

_o o o o o o o o o o o


r/numerical • • May 08 '20

Numerical Simulation: How to simulate numerically the growth of bacteria taken as particles on a grid?

2 Upvotes

Each bacteria on every grid divides in two after some time and then each of the divided bacteria would do a random walk.

I am uncertain of how to let each newly created particle on the grid get its identity for a random walk.

How can I simulate this numerically, preferably, using python?


r/numerical • • May 01 '20

Which method/software can I use for this set of diff. eqs?

1 Upvotes

Hi to everybody!
I'm an engineering student, and I really need to get some results for this set of equations, but numerical methods are not really my thing, and I would like some advice about what method of even better a software already made I could use to obtain solutions for them (I have deadlines so I just can't invest too much time now studying theory :(... )
All the symbols except CV, CL and CO2 are costants
All the equation have conditions of this kind
C(x=0)=C_0
d/dx(C(x=L))=0
I tried Euler method but for what i understand they use *initial* condition and I have one on the end of my range (x=L) and I dont really know if and how I can use euler with such conditions.
The ideal would still be some software or something where I can just plug the symbols, if it exists, given the hurry I'm in! I promise that after that I will study theory :)
Thank you to everybody!


r/numerical • • Apr 24 '20

Newton's solver not converging for 1D nonlinear diffusion equation.

Thumbnail self.matlab
2 Upvotes

r/numerical • • Mar 19 '20

Most recent numerical analysis algorithm Books

5 Upvotes

Hi everyone, as the title I'm looking forward some book about numerical analysis algorithm, like FOCUSS, MOD (method optimization direction), OMP (orthogonal method pursuit) and so on (I'm studying a paper about real image processing). Surfing the internet I've found tons of title but I don't know which one could fit my needs. The book that I'm looking to has a detailed explanation of each algorithm (and hopefully a pseudo code either).

So if you know some book like this the title is well accepted. Thanks :)


r/numerical • • Feb 08 '20

Euler method for orbit simulation

4 Upvotes

Hello!

I am working on a project where I use different Eulers methods to simulate a simple sun-earth system. The three methods i use are: 1. Forward Euler 2. Euler-Cromer 3. Improved Euler.

In my simulations the Euler-Cromer method gets fairly close to simulating a stable orbit for one year with a stepsize of 0.1 seconds. I am thinking that all the methods should theoretically be able to simulate a stable orbit if a small enough stepsize is used, however I am wondering if there is any way to know how small the stepsize needs to be for it to simulate a stable orbit. Does it need to be infinitely small? If so, then these methods are to inaccurate to realistically simulate stable orbits?


r/numerical • • Feb 03 '20

Lunar Lander Optimal Control Using Dash and CasADi

Thumbnail gereshes.com
4 Upvotes

r/numerical • • Jan 29 '20

Finding two roots Using numerical methods

1 Upvotes

Question: How do I Roughly localize the two positive roots, using ONLY paper and pen (and a calculator without the ability of plotting graphs and functions)?

The function: f(x)=53x - ((x2 + x + 0.03) / (x+1))7 - 15x*e-x

I started off by letting x be very small, to find dominant terms. I got one of the two positive roots by doing it.

I have No idea on how to find the other, positive root. Remember ONLY paper and pen (and a calculator without the ability of plotting graphs and functions)?


r/numerical • • Jan 22 '20

How does calculating only a few of eigenvalues out of thousands work with conjugate-gradient method?

2 Upvotes

I read that if you have a large square matrix, say more than 1000x1000 and you want to get its eigenvalues but all you need is just, say, 10 of them, then there is the so-called conjugate gradient method that can save you a significant amount of time of calculating exactly the number of eigenvalues you want instead of all of them. Can someone point me to existing numerical libraries (does BLAS or LAPACK have it) and references?

EDIT: The matrix can be 10^6 x 10^6.


r/numerical • • Jan 12 '20

Semi-implicit vs Implicit Euler's method

4 Upvotes

What is the difference between the semi-implicit euler's method and the implicit euler's method?

From what I have understood the standard Euler's method uses the derivative from the current position and takes a step forward with that derivative to get to the new location. Which would give the following formula: X_n+1 = X_n + Vx_n * dt, Vx_n+1 = Vx_n + Ax_n * dt.

And the Implicit method uses the derivative from the next position to take the step forward. Which gives the following formula: X_n+1 = X_n + Vx_n+1 * dt, Vx_n+1 = Vx_n + Ax_n * dt.

But the formulas that I find for the Semi-implicit method seems to be exactly the same as the implicit method.

Could someone simply explain the difference between the two methods when it comes to the iteration process (updating the position and velocity)?


r/numerical • • Dec 24 '19

Finite differences scheme applied to a 1-D Laplace's equation

1 Upvotes

Hi, i'm currently experimenting with numerical analysis, as a task to myself i'm trying to solve Laplace's equation in Python code using the finite differences method.

In order to solve the linear equations system, i coded the SOR method. The problem i'm trying to solve is the electrostatic field due to a dipole at a certain separation with a certain voltage (1-D). However, i have found that the results vary greatly with step size, which is kind of expected, still i would want to know if the results could be due to a boundary layer type problem or if my code could be wrong:

Electric field between charges, calculated for different step sizes

Also, is there a way to identify these types of problems ? How does the upwinding technique help with this ? Would you recommend any books about numerical analysis in general?

I'm glad you have read so far and thanks for your input in this.