首页 / C# / y值的C#梯形规则总和
y值的C#梯形规则总和
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了y值的C#梯形规则总和,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5201字,纯文字阅读大概需要8分钟。
内容图文
![y值的C#梯形规则总和](/upload/InfoBanner/zyjiaocheng/666/452c758eeaf0461d9e423b90207e8cf3.jpg)
老师要求我们使用C#编写梯形规则解决方案;
他希望我们将其分解为三种方法.
下面是问题和到目前为止的代码:
Numerical integration is a technique that is particular suitable for
computer applications. In exercise we will try to implement the
trapezoidal rule. The integral of a mathematical function is the area
between the curve and the x-axis. If the area is divided into little
trapezoids, then the integral is approximated by the area of these
geometrical figures.You will try to find the area under the curve y = 6x^2-7x+2 in the
region from x = 0.5 to x = 1.5 (N.B. your answer should work out to
about be 1.54, however the actual answer is 1.5). The area is given by
the formula
![]()
where y0 and y1 are the height of the vertical lines i.e. the value of
the function.This problem can be decomposed into three parts as follows:
Write a method called EvaluateQuadraticValue(double x, double a, double b, double c) that takes four double arguments: the value of x,
the coefficient of the x2 term, the coefficient of the x and the
constant. The method will compute and return the result value of y
given by the expression y = ax2+ba+c.Write a method called ComputeQuadraticValues(double startX, double increments, int numberOfIntervals, double a, double b, double c) that
takes six arguments: the start value of x, the increments and the
number of intervals and the coefficients of the quadratic equation.
This method will figure out the values of x0, x1, x2 etc. by invoking
the previous question. The results of the methods calls are collected
and returned as a double array.Write a method called ApplyTrapeziodalRule(double startX, double endX, int numberOfIntervals, double a, double b, double c) . The
arguments are described in the previous question. This method calls
the previous method and process the double array that is returned to
compute the area under the curve by applying the formula
到目前为止,我的代码是:
public static double EvaluateQuadraticValue(double x, double a, double b, double c)
{
double y = a * Math.Pow(x, 2) + b * x + c;
Console.WriteLine("The y coordinate for this x is: {0}", y);
return y;
}
public static double[] ComputeQuadraticValues(double startX, double increments, int numberOfIntervals, double a, double b, double c)
{
double[] xPoints = new double[numberOfIntervals];
for (int index = 0; index < numberOfIntervals; index++)
{
xPoints[index] = startX;
Console.WriteLine("X{0} is {1}: ",index, xPoints[index]);
EvaluateQuadraticValue(startX, a, b, c);
startX = startX + increments;
}
return xPoints;
}
public static void ApplyTrapeziodalRule(double startX, double endX, int numberOfIntervals, double a, double b, double c)
{
double increments = Convert.ToInt32(Console.ReadLine());
double[] xPoints = ComputeQuadraticValues(startX, increments, numberOfIntervals, a, b, c);
//double[] values = a * Math.Pow(xPoints[i], 2) + b * xPoints[i] + c;
//double y = xPoints.Sum();
/*for (int i = 0; i < numberOfIntervals; i++)
{
}*/
//Console.WriteLine(y + " sum");
}
目前,我在使用第三种方法时遇到麻烦.
由于我来自ComputeQuadraticValues()的双精度数组是x0,x1,x2等.如何使用此数组获取(y0 2 * y1 2 * y2 …… 2 * yn y(n 1))?
任何提示或技巧,不胜感激!
解决方法:
我同意@MartinLiversage的观点,也认为练习中还有一些其他棘手的部分.我会尽力给您一个好的答案.让我知道这对您如何工作.
这是由6x ^ 2-7x 2表示的曲线,您需要以蓝色计算面积:
您正在做的是一种数值方法,其背后的逻辑是,如果您设法将区域划分为无数个点,则可以更准确地获得总面积.您添加的点越多,结果将越准确.
事实是,在计算机科学中,无限是不可能的,因为资源是有限的,最终您需要设置一个限制.
我将间隔数(N)设置为10,因此您将获得11分,并且示例易于阅读.
现在,您的第一个方法只是用于评估ax ^ 2 bx c形式的函数的助手.
public static double EvaluateQuadraticValue(double x, double a, double b, double c)
{
double y = a*Math.Pow(x,2) + b*x + c;
return y;
}
第二个是我认为问题所在.我会这样实现:
public static double[] ComputeQuadraticValues(double startX, double increments, int numberOfIntervals, double a, double b, double c)
{
//We need numberOfInterval + 1 values
double[] yPoints = new double[numberOfIntervals+1];
for (int index = 0; index <= numberOfIntervals; index++, startX += increments)
{
//evaluate the function and get the y value for this x
yPoints[index] = EvaluateQuadraticValue(startX, a, b, c);
//Console.WriteLine("({0}, {1})", startX, yPoints[index]);
}
return yPoints;
}
最后一个是由Main()函数调用的函数:
public static void ApplyTrapezoidalRule(double startX, double endX, int numberOfIntervals, double a, double b, double c)
{
double increments = (endX - startX)/numberOfIntervals;
Console.WriteLine("increment: " + increments);
//compute the function value for each X (generated from startX + increment).
double[] yPoints = ComputeQuadraticValues(startX, increments, numberOfIntervals, a, b, c);
var first = (double)(endX - startX)/(2*numberOfIntervals);
Console.WriteLine("({0} - {1})/2*{2} = {3}", endX, startX, numberOfIntervals, first);
var sum = yPoints[0];
for (int i = 1; i <= numberOfIntervals; i++)
sum += 2 * yPoints[i];
sum += yPoints[numberOfIntervals];
var result = first * sum;
Console.WriteLine("result: " + result);
}
我声明了更多的变量,因此您将看到该过程,并且代码再次更具可读性.
您会看到this fiddle并使用传入的numberOfIntervals进行播放.请注意,如果增加间隔数,则该值将更加准确.
希望这可以帮助!
内容总结
以上是互联网集市为您收集整理的y值的C#梯形规则总和全部内容,希望文章能够帮你解决y值的C#梯形规则总和所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。