Finding the matrix hyperbolic cosine function using Taylor series.
Here’s a Java program that calculates the matrix hyperbolic cosine function using the Taylor series approximation:
import java.util.Arrays; public class MatrixHyperbolicCosine { public static void main(String[] args) { double[][] matrix = {{1, 2}, {3, 4}}; // Example matrix double[][] matrixCosh = matrixCosh(matrix); System.out.println("Matrix Hyperbolic Cosine:"); printMatrix(matrixCosh); } public static double[][] matrixCosh(double[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; double[][] result = new double[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = calculateCosh(matrix[i][j]); } } return result; } public static double calculateCosh(double x) { int iterations = 10; // Number of terms to approximate double result = 1; double term = 1; for (int i = 1; i <= iterations; i++) { term *= x * x / (2 * i * (2 * i - 1)); result += term; } return result; } public static void printMatrix(double[][] matrix) { for (double[] row : matrix) { System.out.println(Arrays.toString(row)); } } }
This program defines a `matrixCosh` function that takes a matrix as input and returns the matrix hyperbolic cosine of each element. The `calculateCosh` function approximates the hyperbolic cosine using the Taylor series with a specified number of iterations.
In the example above, the program calculates the matrix hyperbolic cosine for a 2×2 matrix and prints the resulting matrix. You can modify the `matrix` variable to use a different matrix if needed.
No comments yet! You be the first to comment.