A simple Console Application to ASP.NET application

W

Web learner

I am trying to convert Simpson integration algorithm given in the second section of http://csharpcomputing.com/Tutorials/Lesson16.htm

from Console App to an ASP.NET 2.0 page.

Here is the code
<script runat="server">

protected void Page_Load(object sender, EventArgs e){

class Integral{

public delegate double Function(double x);

public static double integral(Function f, double a, double b, int step_number){

double sum = 0;

double step_size = (b - a) / step_number;


for (int i = 0; i < step_number; i = i + 2)


sum = sum + step_size / 3 *(f(a + i * step_size) + 4 * f(a + (i + 1) * step_size) + f(a + (i + 2) * step_size));

return sum;

}

}

class Test{

//a simple function to be integrated

public static double f1(double x){

return (x * x);

}

Response.Write((Integral.integral(new Integral.Function(f1), 1, 10, 20)).ToString).;

}

}

</script>

I tried other way but erred. As beginner, I do not know what is wrong.
 
G

Guest

You can try the following in the code behind model, I'm not sure how to write
it as inline script. I think part of the problem was trying to declare the
class inside the Page_Load event handler. Since the Response.Write(...) line
replaces the Console.WriteLine(...) code from the example, you should just
keep the f1 definition in the same class. Hope that helps - Chase

protected void Page_Load(object sender, EventArgs e)
{
Response.Write((Integral.integral(new Integral.Function(f1), 1, 10,
20)).ToString());
}

public static double f1(double x)
{
return (x * x);
}


class Integral
{

public delegate double Function(double x);

public static double integral(Function f, double a, double b, int
step_number)
{

double sum = 0;

double step_size = (b - a) / step_number;


for (int i = 0; i < step_number; i = i + 2)


sum = sum + step_size / 3 *(f(a + i * step_size) + 4 * f(a + (i + 1)
* step_size) + f(a + (i + 2) * step_size));

return sum;

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top