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;
}
當添加「靜態」進線靜態「主」方法:public static雙主() 還有另一個錯誤顯示向上
錯誤CS0120:一個對象引用是所必需的非靜態字段,方法或屬性「Newton_Ralphson.compute(雙,整型,雙[])」的兩個另外的功能被執行時發生
錯誤
我認爲'Main'必須是'void'。此外,請檢查您的項目的屬性,並取消選中'啓用應用程序框架' –
@ T.S。 - 不,'Main'可以返回'int'或'void'。例如,請參閱此[MSDN](http://msdn.microsoft.com/zh-cn/library/ms228506%28v=vs.90%29.aspx)鏈接。 – Tim
1)*將* static *添加到Main 2)修復*那些*錯誤(在SO上搜索導致它們的原因以及如何修復它們)3)返回並修復關於Main的下一個錯誤 - 總是試着拿一個向前一步。你最終會走到最後。 – user2864740