微分方程数值解法(欧拉方法)

微分方程数值解法(欧拉方法)
假设y'=-x/y,这里采用分离变量法可以得到x^2+y^2=C,是一个圆;
现在假设C=4,并且有初始值为(-2,0),比较用数值方法获得的值与用公式计算的值之间的误差。
用公式进行计算有y=sqrt (  4-x^2) ;
采用欧拉方法是这样的,x每次增加0.1,例如-1.9,-1.8,-1.7...
对应的y的值是这样求的,y1=0.625,(初始值)
y2=y1+y'*0.1=0.625+(--1.9/0.625)*0.1...
类似的方法求出y3,y4...






下面写程序来证明:
(defun pow (num count)
(if (or (> count 1) (eq  count  1) )
      (* num 
         (pow num 
              (- count 1) ) )
      1))


(defun slayer ( count)
(if (or (> count 1) (eq  count  1) )
      (* count 
         (slayer  
              (- count 1) ) )
      1))


(defun slayerex (num count)
(if (or (> count 1) (eq  count  1) )
      (* num 
         (slayerex 
              (-  num  1) 
              (- count 1) ) )
      1))






(defun  expr (x1 y1 )
(+  y1
    (*  0.1
        (-   0
             (/  x1
                 y1)))))


(defun  calc (n x)
(if  (eq  n  0)
       1.0
    (+ (calc (1- n)
             x)
       (*  (pow  x  
                 n)
           (/    1
                 (slayer n))))))
      
(defun  mysqrt (x)
(calc  10
       (*  1/2 
           (log x))))


(defun  formula  (x)
(mysqrt (-  4
            (pow  x  2))))




(defun  exprhelp (x1 )
(if (< (abs (-  x1  -1.9))
       0.1)
        (formula  -1.9)
    (+  (exprhelp  (-  x1  0.1) )
        (*  0.1
            (-   0
                 (/   x1
                      (exprhelp  (-  x1  0.1) )))))))


(defun  exprhelp (x1 )
(if (< (abs (-  x1  -1.9))
       0.1)
        (formula  -1.9)
    (expr  x1  
           (exprhelp  (-  x1  
                          0.1)))))                  


(defun  test (n)
(if (< n 2)
  (progn 
       (print (exprhelp n))
       (print  'compare)
       (print (formula n))      
       (test (+ n 0.1)))
  (print 'over)))


[62]> (test  -1.8)


0.9127306
COMPARE
0.87178
1.0989848
COMPARE
1.0535655
1.2445737
COMPARE
1.2
1.3650969
COMPARE
1.3228757
1.4676538
COMPARE
1.4282857
1.5562305
COMPARE
1.5198687
1.6333399
COMPARE
1.6
1.7006866
COMPARE
1.6703295
1.7594863
COMPARE
1.7320509
1.8106376
COMPARE
1.7860574
1.854821
COMPARE
1.8330306
1.8925605
COMPARE
1.8734995
1.9242636
COMPARE
1.9078783
1.9502475
COMPARE
1.936492
1.9707577
COMPARE
1.9595919
1.9859803
COMPARE
1.977372
1.9960508
COMPARE
1.9899749
2.0010607
COMPARE
1.9974984
2.0010607
COMPARE
2.0
1.9960634
COMPARE
1.9974984
1.9860436
COMPARE
1.9899749
1.9709382
COMPARE
1.977372
1.9506433
COMPARE
1.9595919
1.9250107
COMPARE
1.9364918
1.893842
COMPARE
1.9078783
1.8568801
COMPARE
1.8734993
1.813797
COMPARE
1.8330302
1.7641773
COMPARE
1.7860571
1.7074937
COMPARE
1.7320507
1.6430718
COMPARE
1.670329
1.5700378
COMPARE
1.5999994
1.4872372
COMPARE
1.5198681
1.3931029
COMPARE
1.4282852
1.2854295
COMPARE
1.322875
1.1609575
COMPARE
1.1999992
1.0145266
COMPARE
1.0535647
0.8371039
COMPARE
0.87177867
0.6101308
COMPARE
0.624498
OVER
OVER


以下是修正版本
(defun  test (n)
(if (< n 2)
  (progn 
       (print (expr (- n  0.1) 
                    (formula (- n 0.1))))
       (print  'compare)
       (print (formula n))      
       (test (+ n 0.1)))
  (print 'over)))


[34]> (test  -1.8)


0.92874336
COMPARE
0.87178
1.0782541
COMPARE
1.0535655
1.2149223
COMPARE
1.2
1.3333334
COMPARE
1.3228757
1.4362651
COMPARE
1.4282857
1.5263053
COMPARE
1.5198687
1.6054025
COMPARE
1.6
1.675
COMPARE
1.6703295
1.7361847
COMPARE
1.7320509
1.7897859
COMPARE
1.7860574
1.8364477
COMPARE
1.8330306
1.8766742
COMPARE
1.8734995
1.9108627
COMPARE
1.9078783
1.9393268
COMPARE
1.936492
1.9623119
COMPARE
1.9595919
1.9800043
COMPARE
1.977372
1.9925437
COMPARE
1.9899749
2.0000253
COMPARE
1.9974984
2.0025046
COMPARE
2.0
2.0
COMPARE
1.9974984
1.9924921
COMPARE
1.9899749
1.9799244
COMPARE
1.977372
1.9622004
COMPARE
1.9595919
1.9391794
COMPARE
1.9364918
1.910672
COMPARE
1.9078783
1.8764297
COMPARE
1.8734993
1.836136
COMPARE
1.8330302
1.7893866
COMPARE
1.7860571
1.7356668
COMPARE
1.7320507
1.6743156
COMPARE
1.670329
1.6044737
COMPARE
1.5999994
1.5249994
COMPARE
1.5198681
1.4343344
COMPARE
1.4282852
1.3302655
COMPARE
1.322875
1.2094855
COMPARE
1.1999992
1.0666658
COMPARE
1.0535647
0.8922076
COMPARE
0.87177867
0.6653042
COMPARE
0.624498
OVER
OVER


注意其中几点:
1.mysqrt是按照自己以前的经验写的,不是采用固定点的思维;
2.另外在比较两者的差异的函数中,上面的是只在初始值时校正,而下面的是每上一次都校正,所以显然下面的误差要小些;
3,另外还要注意误差的抵消问题,比如上面的函数刚开始数值法比公式法大,然后慢慢变小,到后面数值法比公式法小,注意这个趋势;而同时在下面的修正法中,数值法总是比公式法大,因为这个曲线是凸的。
4.另外注意exprhelp函数的两个不同版本,时间复杂度有显著区别。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值