Integration

Integration by Monte Carlo is not included. We have not taught this in the class yet. We have seen what is B-H hysteresis loop and understood the physics behind it. The area is measure of the heat loss in the magnetic material.. The Monte Carlo method is used to find the area of the BH loop by means of Random numbers..


Integration by Trapezoidal method


/*this code is doing integration by Trapezoidal method*/ #include <stdio.h> #include <math.h> main() { int n=20; int i; double x1=0.0; double x2=1.0; double h, sum, d; double func(double); h=(x2-x1)/n; /* h is increament along x axis */ printf("%f \n",h); /*=============================================*/ sum=0.0; for(i=1; i<=n; i++) { d=(func(x1)+func(x1+h))*h/2.0; x1=x1+h; sum=sum+d; printf("%d %f %f \n",i,x1,sum); } printf("%f \n",sum); return(0); } /*== the function f(x,y) is defined as ===*/ double func(double x) { double fn; fn=x*x*x; return(fn); }



Integration by Simpson 1/3 Rule
/*this code find the value of the integration using Simpson Rule*/ #include <stdio.h> #include <math.h> main() { int n=100; int i; double a=0.0; double b=1.0; double h, x1, sum, r; double func(double); h=(b-a)/(2.0*n); /* h is increament along x axis */ printf("%f \n",h); /*=============================================*/ x1 = a; sum=0.0; for(i=1; i<=n; i++) { r=(func(x1)+4.0*func(x1+h)+func(x1+2.0*h))*h/3.0; x1=x1+2.0*h; sum=sum+r;/* printf("%d %f %f \n",i,x1,sum);*/ } printf("%0.15f \n",sum); return(0); } /*== the function and f(x,y) =x*sin(x)*exp(-x) is written as below ===*/ double func(double x) { double fn; fn=x*sin(x)*exp(-x); return(fn); }