zoukankan      html  css  js  c++  java
  • matlab实现的嵌套乘法、高精度、二分法

    嵌套乘法的计算:

    [P(x) = 1 - x + x^2 - x^3 + ...+ x ^ {98} - x^{99} ]

    function y = nest( d, c, x, b )
        if nargin < 4, b = zeros(d, 1); end
        y = c(d + 1);
        for i = d : -1 : 1
            y = y.*(x - b(i)) + c(i);
        end
    end
    

    等比数列的实现方式:

    [P(x) = frac{1 - (-x)^{100} } {1 - (-x)} ]

    function y = nestup(x, n, a)
        if nargin < 3, a = 1;   end
        q = -1 * x;
        y = (a * (1 - q ^ n)) / (1 - q);
    end
    

    高精度计算的处理

    [sqrt{c^2 + d} - c ]

    转换为下述形式

    [frac{c^2}{sqrt{c^2 + d} + c} ]

     c = 246886422468;
     d = 13579;
     
     x = sqrt(c * c + d) - c;   % x = 0     wrong answer
     % x must equals to y
     y = d / (sqrt(c * c + d) + c) % y = 2.7500e-08
    

    二分法的实现:

    function xc = CalDetBisect(f, a, b, tol)
       fa = f(a);
       fb = f(b);
       if sign(fa) * sign(fb) >= 0
           error('f(a)f(b)<0  not satisified!')
       end
       while (b - a) / 2 > tol
           c = (a + b) / 2;
           fc = f(c);
           if fc == 0
               break;
           end
           if sign(fc) * sign(fa) < 0
                b = c;
                fb = fc;
           else
                a = c;
                fa = fc;
           end
       end
       xc = (a + b) / 2;
    end
    
  • 相关阅读:
    nohup 运行后台程序
    配置了yum本地源
    rhel 6.7 离线安装docker
    java timer 执行任务
    遇到的sql关键字
    mysql事务和锁
    Mysql命令大全
    mysql keepalived
    mysql主从复制
    mysql从binlog恢复数据
  • 原文地址:https://www.cnblogs.com/wsine/p/4634496.html
Copyright © 2011-2022 走看看