I have a 2×2 matrix, called “one_elect”, which is a function of a variable “r” but I am a little confused as to how to apply the Trapezoidal Method for integrating onto this matrix. I am a decent beginner in C; essentially, I don’t know how to loop this matrix for that particular variable. Please see the code below for a beginner trapezoidal integration code and the code I am working on.
My Code Containing the function I want to use the Integration Method:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Z 1
#define BASISFUN 2
#define UPPER 100 \This is the Upper bound for the integral
#define LOWER 0 \This is the lower bound of the integral
#define SUBINT 50 \This is the number of subintervals for Integration
void makingmatrix(double a[][BASISFUN], double d[][BASISFUN], double alpha_coeff[BASISFUN], double d_coeff[BASISFUN]){
for (int i = 0; i < BASISFUN; i++){
for (int j = 0; j < BASISFUN; j++){
a[i][j] = alpha_coeff[j];
d[i][j] = d_coeff[j];
}
}
printf("nAlpha 2x2 Coefficient Matrix is:n");
for (int i = 0; i < BASISFUN; i++){
for (int j = 0; j < BASISFUN; j++){
printf("%.4ft", a[i][j]);
}
printf("n");
}
printf("nD 2x2 Coefficient Matrix is:n");
for (int i = 0; i < BASISFUN; i++){
for (int j = 0; j < BASISFUN; j++){
printf("%.4ft", d[i][j]);
}
printf("n");
}
printf("n------------------------------------------------------------------n");
};
void one_ele(double a[][BASISFUN], double d[][BASISFUN], double r, double one_electron[][BASISFUN]){
double i_step = (UPPER-LOWER)/SUBINT;
for (int i = 0; i < BASISFUN; i++){
for (int j = 0; j < BASISFUN; j++){
if (i != j){
one_electron[i][j] = (0.5) * (-2 * a[i][j] * r * exp(-a[i][j] * pow(r, 2))) * (-2 * a[j][i] * r * exp(-a[j][i] * pow(r, 2))) + (Z / r);
}
else{
one_electron[i][i] = (0.5) * (-2 * a[i][i] * r * exp(-a[i][i] * pow(r, 2))) * (-2 * a[i][i] * r * exp(-a[i][i] * pow(r, 2))) + (Z / r);
}
}
}
printf("nOne Electron Hamiltonian Elements aren");
for (int i = 0; i < BASISFUN; i++){
for (int j = 0; j < BASISFUN; j++){
printf("%.4ft", one_electron[i][j]);
}
printf("n");
}
printf("n------------------------------------------------------------------n");
}
int main(){
double A1 = 0.532149, A2 = 4.097728, D1 = 0.82559, D2 = 0.28317;
double r;
double alpha_coeff[BASISFUN] = {A1, A2};
double d_coeff[BASISFUN] = {D1, D2};
double a[BASISFUN][BASISFUN], d[BASISFUN][BASISFUN];
makingmatrix(a, d, alpha_coeff, d_coeff);
one_ele(a, d, r, one_electron);
return 0;
}
Sample code for Trapezoidal Rule:
#include <stdio.h>
#include <math.h>
#define BASISFUN 2
#define Z 1.0
#define UPPER 100 \This is the Upper bound for the integral
#define LOWER 0 \This is the lower bound of the integral
#define SUBINT 50 \This is the number of subintervals for Integration
double f(double r) {
// Define your function here
return (0.5) * (-2 * a[i][j] * r * exp(-a[i][j] * pow(r, 2))) * (-2 * a[i][j] * r * exp(-a[i][j] * pow(r, 2))) + (Z / r);
}
int main() {
double stepSize = (UPPER - LOWER) / SUBINT;
double integral = f(a) + f(b);
for (int i = 1; i < SUBINT; i++) {
double r = a + i * stepSize;
integral += 2 * f(r);
}
integral *= stepSize / 2.0;
printf("Required value of integration is: %.3fn", integral);
return 0;
}