A
ASP.NET explorer
The following piece of code (between comment /***************/ line )works
fine. You can test is in console/win form/web page. Just print strResult.
/**************************************************/
public delegate double DlgFn(double x);
public static double f1(double x) {
return Math.Exp(-(x * x)); //function to be integrated by Simpson's
rule
}
public class Integral {
public static double Integrate(DlgFn f, double a, double b, int n) {
double h = (b - a) / n;//step_size
double sum = 0;
sum += f(a) + f(b);
for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h);
for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h);
return Math.Round((h / 3) * sum, 5);
}
}
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1,
100).ToString();
/**************************************************/
However it does not enable me to extend my application further.
I want to replace the last line of code,
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, 100).ToString();
with the following lines:
double lLim= 0; double uLim= 1;
double n =100; // step no.
DlgFn myFn = new DlgFn(Integral.Integrate);
string strResult =myFn(f1, lLim, uLim, new).ToString();
The reason for doing so is because the variables, lLim, uLim and n will be
used in other methods in application code.
I eagerly look forward for any useful hints/help that could get me move
ahead.
Thanks,
fine. You can test is in console/win form/web page. Just print strResult.
/**************************************************/
public delegate double DlgFn(double x);
public static double f1(double x) {
return Math.Exp(-(x * x)); //function to be integrated by Simpson's
rule
}
public class Integral {
public static double Integrate(DlgFn f, double a, double b, int n) {
double h = (b - a) / n;//step_size
double sum = 0;
sum += f(a) + f(b);
for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h);
for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h);
return Math.Round((h / 3) * sum, 5);
}
}
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1,
100).ToString();
/**************************************************/
However it does not enable me to extend my application further.
I want to replace the last line of code,
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, 100).ToString();
with the following lines:
double lLim= 0; double uLim= 1;
double n =100; // step no.
DlgFn myFn = new DlgFn(Integral.Integrate);
string strResult =myFn(f1, lLim, uLim, new).ToString();
The reason for doing so is because the variables, lLim, uLim and n will be
used in other methods in application code.
I eagerly look forward for any useful hints/help that could get me move
ahead.
Thanks,