新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → C#快餐-16 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2194 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C#快餐-16 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 C#快餐-16


    发信人: nice (春天), 信区: DotNET        
    标  题: C#快餐-16
    发信站: BBS 水木清华站 (Thu Jul 26 02:20:14 2001)


    Lesson 16. Numerical methods in C#.


       In this lesson I will show how to numerically solve algebraic  and ordinary  
    differential equations, and perform numerical integration with Simpson method. I will start with the solution of algebraic equations. The secant method  
    is one of the simplest methods for solving algebraic equations. It is usually
    used as a part of a larger algorithm to improve convergence. As in any  
    numerical algorithm, we need to check that the method is converging to a given  
    precision in a certain number of steps. This is a precaution to avoid an  
    infinite loop.

    //secant method

    using System;
    class Secant
    {
        public delegate double Function(double x); //declare a delegate that tak
    es double and returns double
        public static void secant(int step_number, double point1,double point2,F
    unction f)
        {
            double p2,p1,p0,prec=.0001f; //set precision to .0001
            int i;
            p0=f(point1);
            p1=f(point2);
            p2=p1-f(p1)*(p1-p0)/(f(p1)-f(p0)); //secant formula
            for(i=0;System.Math.Abs(p2)>prec &&i<step_number;i++) //iterate till
    precision goal is not met or the maximum //number of steps is reached
            {
                p0=p1;
                p1=p2;
                p2=p1-f(p1)*(p1-p0)/(f(p1)-f(p0));
            }
            if(i<step_number)
                Console.WriteLine(p2); //method converges
            else
                Console.WriteLine("{0}.The method did not converge",p2);//method
    does not converge
        }
    }
    class Demo
    {//equation f1(x)==0;
        public static double f1( double x)
        {
            return x*x*x-2*x-5;
        }
        public static void Main()
        {
        Secant.secant(5,0,1,new Secant.Function(f1));
        }
    }
    Our second example is a Simpson integration algorithm. We have introduced  
    numerical integration in our discussion of delegates in Lesson12. The Simpson  
    algorithm is more precise the naive integration algorithm I have used there.

    The basic idea of the Simpson algorithm is to sample the integrand in a  
    number of points to get a better estimate of its variations in a given  
    interval. So Simpson method is more precise than the method shown in Lesson12,
    however since Simpson method samples more points it is slower.


    //Simpson integration algorithm
    using System;
    //calculate the integral of f(x) between x=a and x=b by spliting the interva
    l in step_number steps
    class Integral
    {
        public delegate double Function(double x); //declare a delegate that tak
    es and returns double
        public static double integral(Function f,double a, double b,int step_num
    ber)
        {
              double sum=0;
              double step_size=(b-a)/step_number;
             for(int i=0;i<step_number;i=i+2) //Simpson algorithm samples the in
    tegrand in several point which significantly improves //precision.
            sum=sum+(f(a+i*step_size)+4*f(a+(i+1)*step_size)+f(a+(i+2)*step_size
    ))*step_size/3; //divide the area under f(x)     //into step_number rectangl
    es and sum their areas
              return sum;
        }
    }
    class Test
    {
        //simple functions to be integrated
        public static double f1( double x)
        {
        return x*x;
        }
        public static double f2(double x)
        {
        return x*x*x;
        }
        public static void Main()
        {//output the value of the integral.
        Console.WriteLine(Integral.integral(new Integral.Function(f1),1,10,20));

        }
    }


    Finally, let me show a simple code for solving first order ordinary  
    differential equations. The code uses a Runge-Kutta method. The simplest
    method to solve ODE is to do a Taylor expansion, which is called Euler's  
    method. Euler's method approximates the solution with the series of  
    consecutive secants. The error in Euler's method is O(h) on every step  
    of size h. The Runge-Kutta method has an error O(h^4)


    using System;
    //fourth order Runge Kutte method for y'=f(t,y);
    //solve first order ode in the interval (a,b) with a given initial condition
    at x=a and fixed step h.
    class Runge{
        public delegate double Function(double t,double y); //declare a delegate
    that takes a double and returns
    //double
        public static void runge(double a, double b,double value, double step, F
    unction f)
        {
              double t,w,k1,k2,k3,k4;
            t=a;
            w=value;
            for(int i=0;i<(b-a)/step;i++){
                k1=step*f(t,w);
                k2=step*f(t+step/2,w+k1/2);
                k3=step*f(t+step/2,w+k2/2);
                k4=step*f(t+step,w+k3);
                w=w+(k1+2*k2+2*k3+k4)/6;
                t=a+i*step;
                Console.WriteLine("{0} {1} ",t,w);
               }
        }
    }
    class Test
    {
        public static double f1(double t, double y)
        {
        return -y+t+1;
        }
        public static void Main()
        {
        Runge.runge(0,1,1,.1f,new Runge.Function(Test.f1));
        }
    }

    Runge-Kutta methods with a variable step size are often used in practice
    since they converge faster than fixed size methods.

    --

    ※ 修改:·walts 於 Jul 26 10:35:59 修改本文·[FROM: 166.111.142.118]
    ※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.176.234]
    上一篇
    返回上一页
    回到目录
    回到页首
    下一篇


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:25:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/12/27 18:43:44

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    39.063ms