大神,帮我用牛顿法求方程的根!!!

liyinyue333 2012-12-27 09:07:49
编写一个C#程序,求方程f(x)=x3-5x+2在1.5附近的一个实根。
用牛顿法求根,不要C语言,不要C++
真心不会啊。。。。。。等待高手[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/onion/9.gif][/img
高手快点帮帮我吧,现在很是焦急
...全文
540 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyinyue333 2012-12-27
  • 打赏
  • 举报
回复
大神,真的大神!谢谢你。。。。。 这是个神奇的地方
threenewbee 2012-12-27
  • 打赏
  • 举报
回复 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 . . .
threenewbee 2012-12-27
  • 打赏
  • 举报
回复
牛顿迭代法的精髓就是“凑”,不断逼近结果。
  • 打赏
  • 举报
回复
你先告诉我什么叫做牛顿插值法吧。

110,526

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧