Skip to main content
deleted 8 characters in body
Source Link
    import javax.swing.*;
    import java.io.FileWriter;
    
    /***
     * Author: Carlos Eduardo da Silva Lima
     * Modified Euler method (Heun)
     * Enter t_0: start time
     * Enter x_0: starting position
     * Enter h: Step h, from Heun's algorithm
     * Enter N: Iteration amount
     * Textbook/Biography: Numerical Calculus Neide Berthold Franco
     */
    
    public class Heun
    {
        public static void main(String[] args)
        {
            double t_0 = 0.0;
            double x_0 = 1.0;
            double h   = 1E-4;
            int    N   = 100000;
    
            Heun(0.0t_0,1.0x_0,1E-4h,100000N);
    
        }// End of main method main
    
    
        // Enter the expression for the derivative dy(t,x)/dt = f(t,x)
        public static double f(double t, double x)
        {
            return -1.2*x+7*Math.exp(-0.3*t);
        }
    
        // This method implements Heun's algorithm
        public static void Heun(double t_0, double x_0, double h, int N)
        {
            double k1, k2, k3;
            double [] t = new double[N]; // time t
            double [] x = new double[N]; // position in the inactive time t, x(t)

            t[0] = t_0; // Initial time t0
            x[0] = x_0; // Initial position x0, in the initial time t0
    
            for(int i = 0; i<=N-2; i++)
            {
                k1 = f(t[i],x[i]);
                k2 = f(t[i]+(h/3),x[i]+((h*k1)/3));
                k3 = f(t[i]+((2*h)/3),x[i]+((2*h*k2)/3));
    
                x[i+1] = x[i] + ((h*(k1+3*k3))/4);
                t[i+1] = t[i] + h;
            }
    
            // On-screen output (Promp)
            for(int i = 0; i<=N-2; i++)
            {
                // System.out.printf("t = %f | x = %f\n",t[i],x[i]);
            }

            try
            {
                // Creation of .txt or .dat file
                FileWriter arquivo0 = new FileWriter("Heun.txt",false);
    
                for(int j = 0; j<(N-1); j++)
                {
                    arquivo0.write(t[j]+"       "+x[j]+"       "+"\n");
                }
        
                // Closing the .txt or .dat file
                arquivo0.close();
            }
            catch (Exception e)
            {
                System.out.println("Erro "+e.getMessage());
            }
            finally
            {
                System.out.println("Fim do bloco try-catch-finally");
                JOptionPane.showMessageDialog(null,".txt(or .dat) files created successfully!");
            }
        }// End of Heun's method
    
    }// End of main class Heun
    import javax.swing.*;
    import java.io.FileWriter;
    
    /***
     * Author: Carlos Eduardo da Silva Lima
     * Modified Euler method (Heun)
     * Enter t_0: start time
     * Enter x_0: starting position
     * Enter h: Step h, from Heun's algorithm
     * Enter N: Iteration amount
     * Textbook/Biography: Numerical Calculus Neide Berthold Franco
     */
    
    public class Heun
    {
        public static void main(String[] args)
        {
            double t_0 = 0.0;
            double x_0 = 1.0;
            double h   = 1E-4;
            int    N   = 100000;
    
            Heun(0.0,1.0,1E-4,100000);
    
        }// End of main method main
    
    
        // Enter the expression for the derivative dy(t,x)/dt = f(t,x)
        public static double f(double t, double x)
        {
            return -1.2*x+7*Math.exp(-0.3*t);
        }
    
        // This method implements Heun's algorithm
        public static void Heun(double t_0, double x_0, double h, int N)
        {
            double k1, k2, k3;
            double [] t = new double[N]; // time t
            double [] x = new double[N]; // position in the inactive time t, x(t)

            t[0] = t_0; // Initial time t0
            x[0] = x_0; // Initial position x0, in the initial time t0
    
            for(int i = 0; i<=N-2; i++)
            {
                k1 = f(t[i],x[i]);
                k2 = f(t[i]+(h/3),x[i]+((h*k1)/3));
                k3 = f(t[i]+((2*h)/3),x[i]+((2*h*k2)/3));
    
                x[i+1] = x[i] + ((h*(k1+3*k3))/4);
                t[i+1] = t[i] + h;
            }
    
            // On-screen output (Promp)
            for(int i = 0; i<=N-2; i++)
            {
                // System.out.printf("t = %f | x = %f\n",t[i],x[i]);
            }

            try
            {
                // Creation of .txt or .dat file
                FileWriter arquivo0 = new FileWriter("Heun.txt",false);
    
                for(int j = 0; j<(N-1); j++)
                {
                    arquivo0.write(t[j]+"       "+x[j]+"       "+"\n");
                }
        
                // Closing the .txt or .dat file
                arquivo0.close();
            }
            catch (Exception e)
            {
                System.out.println("Erro "+e.getMessage());
            }
            finally
            {
                System.out.println("Fim do bloco try-catch-finally");
                JOptionPane.showMessageDialog(null,".txt(or .dat) files created successfully!");
            }
        }// End of Heun's method
    
    }// End of main class Heun
    import javax.swing.*;
    import java.io.FileWriter;
    
    /***
     * Author: Carlos Eduardo da Silva Lima
     * Modified Euler method (Heun)
     * Enter t_0: start time
     * Enter x_0: starting position
     * Enter h: Step h, from Heun's algorithm
     * Enter N: Iteration amount
     * Textbook/Biography: Numerical Calculus Neide Berthold Franco
     */
    
    public class Heun
    {
        public static void main(String[] args)
        {
            double t_0 = 0.0;
            double x_0 = 1.0;
            double h   = 1E-4;
            int    N   = 100000;
    
            Heun(t_0,x_0,h,N);
    
        }// End of main method main
    
    
        // Enter the expression for the derivative dy(t,x)/dt = f(t,x)
        public static double f(double t, double x)
        {
            return -1.2*x+7*Math.exp(-0.3*t);
        }
    
        // This method implements Heun's algorithm
        public static void Heun(double t_0, double x_0, double h, int N)
        {
            double k1, k2, k3;
            double [] t = new double[N]; // time t
            double [] x = new double[N]; // position in the inactive time t, x(t)

            t[0] = t_0; // Initial time t0
            x[0] = x_0; // Initial position x0, in the initial time t0
    
            for(int i = 0; i<=N-2; i++)
            {
                k1 = f(t[i],x[i]);
                k2 = f(t[i]+(h/3),x[i]+((h*k1)/3));
                k3 = f(t[i]+((2*h)/3),x[i]+((2*h*k2)/3));
    
                x[i+1] = x[i] + ((h*(k1+3*k3))/4);
                t[i+1] = t[i] + h;
            }
    
            // On-screen output (Promp)
            for(int i = 0; i<=N-2; i++)
            {
                // System.out.printf("t = %f | x = %f\n",t[i],x[i]);
            }

            try
            {
                // Creation of .txt or .dat file
                FileWriter arquivo0 = new FileWriter("Heun.txt",false);
    
                for(int j = 0; j<(N-1); j++)
                {
                    arquivo0.write(t[j]+"       "+x[j]+"       "+"\n");
                }
        
                // Closing the .txt or .dat file
                arquivo0.close();
            }
            catch (Exception e)
            {
                System.out.println("Erro "+e.getMessage());
            }
            finally
            {
                System.out.println("Fim do bloco try-catch-finally");
                JOptionPane.showMessageDialog(null,".txt(or .dat) files created successfully!");
            }
        }// End of Heun's method
    
    }// End of main class Heun
added 14 characters in body
Source Link
import javax.swing.*;
import java.io.FileWriter;

/***
 * Author: Carlos Eduardo da Silva Lima
 * Modified Euler method (Heun)
 * Enter t_0: start time
 * Enter x_0: starting position
 * Enter h: Step h, from Heun's algorithm
 * Enter N: Iteration amount
 * Textbook/Biography: Numerical Calculus Neide Berthold Franco
 */

public class Heun
{
    public static void main(String[] args)
    {
        double t_0 = 0.0;
        double x_0 = 1.0;
        double h   = 1E-4;
        int    N   = 100000;

        Heun(0.0,1.0,1E-4,100000);

    }// End of main method main


    // Enter the expression for the derivative dy(t,x)/dt = f(t,x)
    public static double f(double t, double x)
    {
        return -1.2*x+7*Math.exp(-0.3*t);
    }

    // This method implements Heun's algorithm
    public static void Heun(double t_0, double x_0, double h, int N)
    {
        double k1, k2, k3;
        double [] t = new double[N]; // time t
        double [] x = new double[N]; // position in the inactive time t, x(t)

        t[0] = t_0; // Initial time t0
        x[0] = x_0; // Initial position x0, in the initial time t0

        for(int i = 0; i<=N-2; i++)
        {
            k1 = f(t[i],x[i]);
            k2 = f(t[i]+(h/3),x[i]+((h*k1)/3));
            k3 = f(t[i]+((2*h)/3),x[i]+((2*h*k2)/3));

            x[i+1] = x[i] + ((h*(k1+3*k3))/4);
            t[i+1] = t[i] + h;
        }

        // On-screen output (Promp)
        for(int i = 0; i<=N-2; i++)
        {
            // System.out.printf("t = %f | x = %f\n",t[i],x[i]);
        }

        try
        {
            // Creation of .txt or .dat file
            FileWriter arquivo0 = new FileWriter("Heun.txt",false);

            for(int j = 0; j<(N-1); j++)
            {
                arquivo0.write(t[j]+"       "+x[j]+"       "+"\n");
            }
    
            // Closing the .txt or .dat file
            arquivo0.close();
        }
        catch (Exception e)
        {
            System.out.println("Erro "+e.getMessage());
        }
        finally
        {
            System.out.println("Fim do bloco try-catch-finally");
            JOptionPane.showMessageDialog(null,".txt(or .dat) files created successfully!");
        }
    }// End of Heun's method

}// End of main class Heun
    import javax.swing.*;
    import java.io.FileWriter;
    
    /***
     * Author: Carlos Eduardo da Silva Lima
     * Modified Euler method (Heun)
     * Enter t_0: start time
     * Enter x_0: starting position
     * Enter h: Step h, from Heun's algorithm
     * Enter N: Iteration amount
     * Textbook/Biography: Numerical Calculus Neide Berthold Franco
     */
    
    public class Heun
    {
        public static void main(String[] args)
        {
            double t_0 = 0.0;
            double x_0 = 1.0;
            double h   = 1E-4;
            int    N   = 100000;
    
            Heun(0.0,1.0,1E-4,100000);
    
        }// End of main method main
    
    
        // Enter the expression for the derivative dy(t,x)/dt = f(t,x)
        public static double f(double t, double x)
        {
            return -1.2*x+7*Math.exp(-0.3*t);
        }
    
        // This method implements Heun's algorithm
        public static void Heun(double t_0, double x_0, double h, int N)
        {
            double k1, k2, k3;
            double [] t = new double[N]; // time t
            double [] x = new double[N]; // position in the inactive time t, x(t)

            t[0] = t_0; // Initial time t0
            x[0] = x_0; // Initial position x0, in the initial time t0
    
            for(int i = 0; i<=N-2; i++)
            {
                k1 = f(t[i],x[i]);
                k2 = f(t[i]+(h/3),x[i]+((h*k1)/3));
                k3 = f(t[i]+((2*h)/3),x[i]+((2*h*k2)/3));
    
                x[i+1] = x[i] + ((h*(k1+3*k3))/4);
                t[i+1] = t[i] + h;
            }
    
            // On-screen output (Promp)
            for(int i = 0; i<=N-2; i++)
            {
                // System.out.printf("t = %f | x = %f\n",t[i],x[i]);
            }

            try
            {
                // Creation of .txt or .dat file
                FileWriter arquivo0 = new FileWriter("Heun.txt",false);
    
                for(int j = 0; j<(N-1); j++)
                {
                    arquivo0.write(t[j]+"       "+x[j]+"       "+"\n");
                }
        
                // Closing the .txt or .dat file
                arquivo0.close();
            }
            catch (Exception e)
            {
                System.out.println("Erro "+e.getMessage());
            }
            finally
            {
                System.out.println("Fim do bloco try-catch-finally");
                JOptionPane.showMessageDialog(null,".txt(or .dat) files created successfully!");
            }
        }// End of Heun's method
    
    }// End of main class Heun
import javax.swing.*;
import java.io.FileWriter;

/***
 * Author: Carlos Eduardo da Silva Lima
 * Modified Euler method (Heun)
 * Enter t_0: start time
 * Enter x_0: starting position
 * Enter h: Step h, from Heun's algorithm
 * Enter N: Iteration amount
 * Textbook/Biography: Numerical Calculus Neide Berthold Franco
 */

public class Heun
{
    public static void main(String[] args)
    {
        double t_0 = 0.0;
        double x_0 = 1.0;
        double h   = 1E-4;
        int    N   = 100000;

        Heun(0.0,1.0,1E-4,100000);

    }// End of main method main


    // Enter the expression for the derivative dy(t,x)/dt = f(t,x)
    public static double f(double t, double x)
    {
        return -1.2*x+7*Math.exp(-0.3*t);
    }

    // This method implements Heun's algorithm
    public static void Heun(double t_0, double x_0, double h, int N)
    {
        double k1, k2, k3;
        double [] t = new double[N]; // time t
        double [] x = new double[N]; // position in the inactive time t, x(t)

        t[0] = t_0; // Initial time t0
        x[0] = x_0; // Initial position x0, in the initial time t0

        for(int i = 0; i<=N-2; i++)
        {
            k1 = f(t[i],x[i]);
            k2 = f(t[i]+(h/3),x[i]+((h*k1)/3));
            k3 = f(t[i]+((2*h)/3),x[i]+((2*h*k2)/3));

            x[i+1] = x[i] + ((h*(k1+3*k3))/4);
            t[i+1] = t[i] + h;
        }

        // On-screen output (Promp)
        for(int i = 0; i<=N-2; i++)
        {
            // System.out.printf("t = %f | x = %f\n",t[i],x[i]);
        }

        try
        {
            // Creation of .txt or .dat file
            FileWriter arquivo0 = new FileWriter("Heun.txt",false);

            for(int j = 0; j<(N-1); j++)
            {
                arquivo0.write(t[j]+"       "+x[j]+"       "+"\n");
            }
    
            // Closing the .txt or .dat file
            arquivo0.close();
        }
        catch (Exception e)
        {
            System.out.println("Erro "+e.getMessage());
        }
        finally
        {
            System.out.println("Fim do bloco try-catch-finally");
            JOptionPane.showMessageDialog(null,".txt(or .dat) files created successfully!");
        }
    }// End of Heun's method

}// End of main class Heun
    import javax.swing.*;
    import java.io.FileWriter;
    
    /***
     * Author: Carlos Eduardo da Silva Lima
     * Modified Euler method (Heun)
     * Enter t_0: start time
     * Enter x_0: starting position
     * Enter h: Step h, from Heun's algorithm
     * Enter N: Iteration amount
     * Textbook/Biography: Numerical Calculus Neide Berthold Franco
     */
    
    public class Heun
    {
        public static void main(String[] args)
        {
            double t_0 = 0.0;
            double x_0 = 1.0;
            double h   = 1E-4;
            int    N   = 100000;
    
            Heun(0.0,1.0,1E-4,100000);
    
        }// End of main method main
    
    
        // Enter the expression for the derivative dy(t,x)/dt = f(t,x)
        public static double f(double t, double x)
        {
            return -1.2*x+7*Math.exp(-0.3*t);
        }
    
        // This method implements Heun's algorithm
        public static void Heun(double t_0, double x_0, double h, int N)
        {
            double k1, k2, k3;
            double [] t = new double[N]; // time t
            double [] x = new double[N]; // position in the inactive time t, x(t)

            t[0] = t_0; // Initial time t0
            x[0] = x_0; // Initial position x0, in the initial time t0
    
            for(int i = 0; i<=N-2; i++)
            {
                k1 = f(t[i],x[i]);
                k2 = f(t[i]+(h/3),x[i]+((h*k1)/3));
                k3 = f(t[i]+((2*h)/3),x[i]+((2*h*k2)/3));
    
                x[i+1] = x[i] + ((h*(k1+3*k3))/4);
                t[i+1] = t[i] + h;
            }
    
            // On-screen output (Promp)
            for(int i = 0; i<=N-2; i++)
            {
                // System.out.printf("t = %f | x = %f\n",t[i],x[i]);
            }

            try
            {
                // Creation of .txt or .dat file
                FileWriter arquivo0 = new FileWriter("Heun.txt",false);
    
                for(int j = 0; j<(N-1); j++)
                {
                    arquivo0.write(t[j]+"       "+x[j]+"       "+"\n");
                }
        
                // Closing the .txt or .dat file
                arquivo0.close();
            }
            catch (Exception e)
            {
                System.out.println("Erro "+e.getMessage());
            }
            finally
            {
                System.out.println("Fim do bloco try-catch-finally");
                JOptionPane.showMessageDialog(null,".txt(or .dat) files created successfully!");
            }
        }// End of Heun's method
    
    }// End of main class Heun
deleted 49 characters in body
Source Link
Anton Menshov
  • 8.7k
  • 7
  • 41
  • 94

Good afternoon. Well, aboutI didn't understand your specific physical problem, I didn't understand very well. But about the numerical method of Heun, I made a code in the java languageJava so that you can take it as a base, if you want to rewrite it in fortranFortran. I also leave the image of the excerpt from the equations of the Heun method (This image was taken from the book: http://galdino.pbworks.com/w/file/fetch/134978082/Livro_Neide.pdf, page 407), inIn this code, we generate a file txt (or dat) file with the results, with itand we can use the gnuplot software to generate the graph.

I also add the excerpt from the "Cálculo Numérico" textbook, page 407 with the equations for the Heun method.

Good afternoon. Well, about your specific physical problem, I didn't understand very well. But about the numerical method of Heun, I made a code in the java language so that you can take it as a base, if you want to rewrite it in fortran. I also leave the image of the excerpt from the equations of the Heun method (This image was taken from the book: http://galdino.pbworks.com/w/file/fetch/134978082/Livro_Neide.pdf, page 407), in this code we generate a file txt (or dat) with the results, with it we can use the gnuplot software to generate the graph.

Well, I didn't understand your specific physical problem very well. But about the numerical method of Heun, I made a code in Java so that you can take it as a base if you want to rewrite it in Fortran. In this code, we generate a txt (or dat) file with the results, and we can use the gnuplot to generate the graph.

I also add the excerpt from the "Cálculo Numérico" textbook, page 407 with the equations for the Heun method.

added 4 characters in body
Source Link
Loading
Source Link
Loading