using System;
public class Newton_Ralphson
{
public double compute(double x,int n,double[] P) // x, degree, coefficients
{
double pol = 0;
for (int i = 0;i < n;i++)
{
for (int j = 1; j < n - i; j++)
x *= x;
pol += P[n - i] * x;
}
pol += P[0];
return pol;
}
public double[] secondary_Pol(int n, double[] P) //slope
{
double[] secP = new double[n - 1];
for (int i = 0; i < n - 1; i++)
secP[i] = P[i + 1];
return secP;
}
public double Main()
{
Console.WriteLine("Estimate the solution for a nth degree polynomial!!! ");
int n = 0;
Console.WriteLine("Enter the degree of the polynomials! ");
n = Int32.Parse(Console.ReadLine());
double[] coefficients = new double[n+1];
for(int i = 0;i < n+1;i++)
{
Console.WriteLine("Enter the coefficients ");
Console.Write("coefficients of x to the power of {0}: ", n - i); //decending coefficients
coefficients[n-i] = Convert.ToDouble(Console.ReadLine());
}
Console.WriteLine("Initial value is: ");
double xint = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("How many times does the function go through? ");
int precision = Convert.ToInt32(Console.ReadLine());
double polyValue = 0;
for (int j = 0; j < precision; j++)
{
polyValue = compute(xint, n, coefficients); //y0 in y = slope(x-x0) + y0
double slope = compute(xint, n - 1, secondary_Pol(n, coefficients));
xint = (-polyValue / slope) + xint;
}
Console.WriteLine(xint);
Console.ReadLine();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
}
当我在行中添加'static'时:public static double Main()会出现另一个错误
错误CS0120:非静态字段,方法或属性'Newton_Ralphson.compute(double,int,double [])'需要对象引用
执行另外两个函数时发生错误
小智 5
请参阅C#入口点功能
C#应用程序,Main()必须是入口点.
原因是因为语言的设计者决定将其作为程序的入口点.他们也可以使用完全不同的方法来查找入口点,例如使用元数据,或为您实例化对象(这将需要无参数构造函数).命名为void main()的另一个原因是它对于来自其他语言的用户来说是直观的.
使用:
public class Newton_Ralphson
{
public static double compute(double x, int n, double[] P) // x, degree, coefficients
{
double pol = 0;
for (int i = 0; i < n; i++)
{
for (int j = 1; j < n - i; j++)
x *= x;
pol += P[n - i]*x;
}
pol += P[0];
return pol;
}
public static double[] secondary_Pol(int n, double[] P) //slope
{
double[] secP = new double[n - 1];
for (int i = 0; i < n - 1; i++)
secP[i] = P[i + 1];
return secP;
}
public static void Main()
{
Console.WriteLine("Estimate the solution for a nth degree polynomial!!! ");
int n = 0;
Console.WriteLine("Enter the degree of the polynomials! ");
n = Int32.Parse(Console.ReadLine());
double[] coefficients = new double[n + 1];
for (int i = 0; i < n + 1; i++)
{
Console.WriteLine("Enter the coefficients ");
Console.Write("coefficients of x to the power of {0}: ", n - i); //decending coefficients
coefficients[n - i] = Convert.ToDouble(Console.ReadLine());
}
Console.WriteLine("Initial value is: ");
double xint = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("How many times does the function go through? ");
int precision = Convert.ToInt32(Console.ReadLine());
double polyValue = 0;
for (int j = 0; j < precision; j++)
{
polyValue = compute(xint, n, coefficients); //y0 in y = slope(x-x0) + y0
double slope = compute(xint, n - 1, secondary_Pol(n, coefficients));
xint = (-polyValue/slope) + xint;
}
Console.WriteLine(xint);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
1 - public double Main()改为 public static void Main()
2 public double[] secondary_Pol(int n, double[] P)改为 public static double[] secondary_Pol(int n, double[] P)
3-Change public double compute(double x,int n,double[] P)to public static double compute(double x, int n, double[] P)
| 归档时间: |
|
| 查看次数: |
55148 次 |
| 最近记录: |