Plotting the error vs the change in 1/dx

In summary, the conversation discusses the process of plotting the error vs the change in 1/dx by integrating the solution with different values of dx and taking the maximum error. The code for doing this in both Python and MATLAB is provided. The Python code involves using the numpy and pylab libraries and creating an array of different dx values. The integration process is then carried out within a for loop. However, there are some issues with the shape of the data.
  • #1
Dustinsfl
2,281
5
In order to plot the error vs the change in 1/dx, I need to integrate the the solution with different a dx and take the max error. However, I don't know how I can set this up in python or matlab.

In python, my code for just one dx is:
Code:
import numpy as np
L = 80.0
N = 512
dt = 0.0002
tmax = 10
nmax = int(np.floor(tmax / dt))
dx = L / N
x = np.arange(-L / 2.0, L / 2.0 - dx, dx)
k = np.hstack((np.arange(0, N / 2.0 - 1.0),
               np.arange(-N / 2.0, 0))).T * 2.0 * np.pi / L
k1 = 1j * k
k3 = (1j * k) ** 3
u = 2 * (2 / (np.exp(x + 20.0) + np.exp(-x - 20.0))) ** 2
udata = u
tdata = 0.0for nn in range(1, nmax + 1):
    du1 = (-np.fft.ifft(k3 * np.fft.fft(u)) -
           3 * np.fft.ifft(k1 * np.fft.fft(u ** 2)))
    v = u + 0.5 * du1 * dt
    du2 = (-np.fft.ifft(k3 * np.fft.fft(v)) -
           3 * np.fft.ifft(k1 * np.fft.fft(v ** 2)))
    v = u + 0.5 * du2 * dt
    du3 = (-np.fft.ifft(k3 * np.fft.fft(v)) -
           3 * np.fft.ifft(k1 * np.fft.fft(v ** 2)))
    v = u + du3 * dt
    du4 = (-np.fft.ifft(k3 * np.fft.fft(v)) -
           3 * np.fft.ifft(k1 * np.fft.fft(v ** 2)))
    u = u + (du1 + 2.0 * du2 + 2.0 * du3 + du4) * dt / 6.0
    if np.mod(nn, np.ceil(nmax / 20.0)) == 0:
        udata = np.vstack((udata, u))
        tdata = np.vstack((tdata, nn * dt))
So to have an array of different dxs, I have written
Code:
N = 2 ** np.arange(-4, 10, dtype = np.float64)
but then how do I adjust the rest of code? Or is there a better way to be able to obtain the error plot which is of the form \(\exp(-c\cdot dx)\)?

If python doesn't suit you, I have MATLAB code too:
Code:
  L = 80; 
  N = 512;
  dt = 0.0002; 
  tmax = 10; 
  nmax = round(tmax/dt);
  dx = L/N; 
  x = (-L/2:dx:L/2-dx)'; 
  k = [0:N/2-1 -N/2:-1]'*2*pi/L; 
  k1 = 1i*k;
  k2 = (1i*k).^2;
  k3 = (1i*k).^3;
  u = 2*sech(x + 20).^2;
  udata = u;
  tdata = 0;
  
  % integration begins
  for nn = 1:nmax
    du1 = -ifft(k3.*fft(u)) - 3*ifft(k1.*fft(u.^2));  
    v = u + 0.5*du1*dt;
    du2 = -ifft(k3.*fft(v)) - 3*ifft(k1.*fft(v.^2));  
    v = u + 0.5*du2*dt;
    du3 = -ifft(k3.*fft(v)) - 3*ifft(k1.*fft(v.^2));  
    v = u + du3*dt;
    du4 = -ifft(k3.*fft(v)) - 3*ifft(k1.*fft(v.^2));
    u = u + (du1 + 2*du2 + 2*du3 + du4)*dt/6;
    if mod(nn, round(nmax/20)) == 0
       udata = [udata u]; 
       tdata = [tdata nn*dt];
    end
  end
  % integration ends
I have even less of a clue on what do for the MATLAB code.

Here is an image of what I am trying to create:
http://imageshack.us/a/img35/4462/6b67.jpg
 
Last edited:
Mathematics news on Phys.org
  • #2
I have progressed some with the code in Python but its having a shape problem. Hopefully some can help square it way.

Code:
import numpy as np
import pylab

L = 80.0
dt = 0.0002
tmax = 10
nmax = int(np.floor(tmax / dt))
deltax = []
error = []

for N in [80., 100., 120., 140., 160., 180., 200., 220., 240., 260., 280., 300.,
          320., 340., 360.]:
    dx = L / N
    deltax.append(dx)
    x = np.arange(-L / 2.0, L / 2.0 - dx, dx)
    k = np.hstack((np.arange(0, N / 2.0 - 1.0),
                   np.arange(-N / 2.0, 0))).T * 2.0 * np.pi / L
    k1 = 1j * k
    k3 = (1j * k) ** 3
    u = (2 * (2 / (np.exp(x + 20.0) + np.exp(-x - 20.0))) ** 2)
    udata = u
    tdata = 0.0
    #  integration
    for nn in range(1, nmax + 1):
        du1 = (-np.fft.ifft(k3 * np.fft.fft(u)) -
        3 * np.fft.ifft(k1 * np.fft.fft(u ** 2)))
        v = u + 0.5 * du1 * dt
        du2 = (-np.fft.ifft(k3 * np.fft.fft(v)) -
        3 * np.fft.ifft(k1 * np.fft.fft(v ** 2)))
        v = u + 0.5 * du2 * dt
        du3 = (-np.fft.ifft(k3 * np.fft.fft(v)) -
        3 * np.fft.ifft(k1 * np.fft.fft(v ** 2)))
        v = u + du3 * dt
        du4 = (-np.fft.ifft(k3 * np.fft.fft(v)) -
        3 * np.fft.ifft(k1 * np.fft.fft(v ** 2)))
        u = u + (du1 + 2.0 * du2 + 2.0 * du3 + du4) * dt / 6.0
    #    error.append(max(abs(udata[:,-1] - 2. *
    #                     (2. / (np.exp(x - 20) + np.exp(-x - 60))))))
        if np.mod(nn, np.ceil(nmax / 20.0)) == 0:
            udata = np.vstack((udata, u))
            tdata = np.vstack((tdata, nn * dt))
 

FAQ: Plotting the error vs the change in 1/dx

What is "Plotting the error vs the change in 1/dx"?

"Plotting the error vs the change in 1/dx" is a visual representation of the relationship between the error in a mathematical calculation and the change in the value of 1/dx. This type of plot is commonly used in scientific research to analyze the accuracy and precision of mathematical models and calculations.

How is "Plotting the error vs the change in 1/dx" useful in scientific research?

This type of plot allows scientists to visualize the behavior of mathematical models and identify any patterns or trends in the error as the value of 1/dx changes. It can also help determine the optimal value of 1/dx to use in a calculation for the most accurate results.

What factors can affect the error in a calculation when plotting against the change in 1/dx?

The error in a calculation can be influenced by a variety of factors, including the precision of the data used, the complexity of the mathematical model, and any assumptions made during the calculation. These factors can all impact the accuracy of the results and should be considered when analyzing a plot of error vs change in 1/dx.

Are there any limitations to using "Plotting the error vs the change in 1/dx" in scientific research?

Like any type of data visualization, there are limitations to using "Plotting the error vs the change in 1/dx" in scientific research. This method may not be suitable for all types of mathematical models or calculations, and it is important to carefully consider the data and assumptions being used before interpreting the results.

How can scientists improve the accuracy of their calculations when using "Plotting the error vs the change in 1/dx"?

To improve the accuracy of their calculations, scientists can choose a smaller value for 1/dx, use more precise data, and carefully evaluate any assumptions made during the calculation. They can also use statistical methods to identify and minimize sources of error in their models.

Similar threads

Replies
1
Views
3K
Replies
4
Views
7K
Replies
1
Views
992
Replies
3
Views
2K
Replies
8
Views
4K
Replies
4
Views
1K
Back
Top