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