编写一个C#程序,求方程f(x)=x3-5x+2在1.5附近的一个实根。
 用牛顿法求根,不要C语言,不要C++
 真心不会啊等待高手[img=http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/9.gif][/img
高手快点帮帮我吧,现在很是焦急

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static double f(int w_f, int x_f, int y_f, int z_f, double sol_f)      //当根为x时,用来求f(x)的值
            {
                double f_result=w_f*Math.Pow(sol_f,3)+x_f*Math.Pow(sol_f,2)+y_f*sol_f+z_f;
            //     cout<<"f_result is "<<f_result<<endl;             //调试时用
                return f_result;
            }        static double f1(int w_f1, int x_f1, int y_f1, int z_f1, double sol_f1)       //当根为x时,用来求f'(x)的值
            {
                double f1_result=3*w_f1*Math.Pow(sol_f1,2)+2*x_f1*sol_f1+y_f1;
            //    cout<<"f1_result is "<<f1_result<<endl;            //调试时用
                return f1_result;
            }        static double get_solution(int w, int x, int y, int z, double sol)         //求根函数,调用了上面两个函数
            {
                double value,tmp;            value=sol;
                do                           //使用了x1=x0-f(x0)/f'(x0),不断循环逼近
                {
                    tmp=f(w,x,y,z,value);
                    value=value-tmp/f1(w,x,y,z,value);        //        cout<<"funciont_value is "<<tmp<<";value is "<<value<<endl;    //调试时用
                }while(Math.Abs(tmp)>=1e-5);      //当式子的值与0的绝对值小于1e-5时就认为取到了值            return value;
            }        static void Main(string[] args)
            {
                int a = 1, b = 0, c = -5, d = 2;
                double solution = 1.5d, solution_value;            solution_value = get_solution(a, b, c, d, solution);
                if (solution_value <= 1e-5)               //当求得的根很小时,直接让它为0
                    solution_value = 0;
                Console.WriteLine("the solution near {0} is {1}.", solution, solution_value);
            }
        }
    }the solution near 1.5 is 2.00000000000033.
    Press any key to continue . . .