声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 7142|回复: 6

[C/C++] newton raphson算法

[复制链接]
发表于 2007-4-20 10:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
哪位能提供一下newton raphson算法的程序阿,求解非线性方程组的算法,我在网上找不到。
回复
分享到:

使用道具 举报

发表于 2007-4-23 08:41 | 显示全部楼层
一个matlab实例

  1. % This m-file takes care of synthetic division.
  2. % By giving one polynomial and one root this function returns
  3. % the polynomial formed with the other roots of the given polynomial excluding the given root.
  4. % Keerthi Venkateswara Rao-M.Tech-NITC-INDIA
  5. function coeff_second=syn_division(coeff_function,fun_root_new)
  6. order_fun=size((coeff_function),2);
  7. coeff_second=0;
  8. for index=1:size((coeff_function),2)-1
  9.     if index==1
  10.         coeff_second(index)=coeff_function(index);
  11.     else
  12.         coeff_second(index)=coeff_function(index)+fun_root_new*coeff_second(index-1);
  13.     end
  14. end
复制代码

  1. % This m-file calculates the derivative of the function, the limitation of
  2. % this function is, it can calculate only the derivatives of power(x,n)....
  3. % Keerthi Venkateswara Rao-M.tech-NITC-INDIA.
  4. function coeff_derivative=derivate(coeff_function)
  5. der_order=size((coeff_function),2)-1;
  6. coeff_derivative=0;
  7. for index=1:size((coeff_function),2)-1
  8.     coeff_derivative(index)=der_order*coeff_function(index);
  9.     der_order=der_order-1;
  10. end
复制代码
  1. % this m-file calculates the real roots of the given polynomial using
  2. % newton raphson technique.this m-file calls the functions in the two m-files named as syn_division and
  3. % derivate.
  4. % coeff_function is the 1xn matrix conatining the coeff of the polynomial.
  5. % Keerthi Venkateswara Rao-M.Tech-NITC-INDIA
  6. function [final_roots,functionvalue] = newton(coeff_function,initial_guess,error_tolerance,max_iterations)
  7. iterations=0;
  8. max_no_roots=size(coeff_function,2);
  9. final_roots=0;
  10. functionvalue=0;
  11. for no_roots=1:max_no_roots-1
  12.     fun_root_new=initial_guess;
  13.     flag=1;
  14.     coeff_der_function=derivate(coeff_function);
  15.     order_fun=size(coeff_function,2);
  16.     order_der_fun=size(coeff_der_function,2);
  17.     while flag==1
  18.         fun_root_old=fun_root_new;
  19.         fx=0;
  20.         dfx=0;
  21.         nonzero=1;
  22.         while nonzero==1
  23.             powers=order_fun-1;
  24.             for index=1:order_fun
  25.                 fx=fx+coeff_function(index)*fun_root_old^powers;
  26.                 powers=powers-1;
  27.             end
  28.             powers=order_der_fun-1;
  29.             for index=1:order_der_fun
  30.                 dfx=dfx+coeff_der_function(index)*fun_root_old^powers;
  31.                 powers=powers-1;
  32.             end
  33.             if dfx==0
  34.                 fun_root_old=fun_root_old+1;
  35.             else
  36.                 nonzero=0;               
  37.             end               
  38.         end
  39.         iterations = iterations + 1;
  40.         fun_root_new = fun_root_old - fx/dfx;
  41.         if iterations >= max_iterations
  42.             flag=0;
  43.         elseif  abs(fun_root_new-fun_root_old)<=error_tolerance
  44.             flag=0;
  45.             final_roots(no_roots)=fun_root_new;
  46.             functionvalue(no_roots)=fx;
  47.         end
  48.     end
  49.     coeff_function=syn_division(coeff_function,fun_root_new);
  50. end
复制代码
发表于 2007-4-23 08:43 | 显示全部楼层
c语言实例

  1. #include <math.h>
  2. #include <iomanip.h>
  3. #include <iostream.h>
  4. #include <process.h>

  5. void usrfun(double x[16],double alpha[16][16],double beta[16])
  6. {
  7.     int np;
  8.     np = 15;
  9.     alpha[1][1] = -2.0 * x[1];
  10.     alpha[1][2] = -2.0 * x[2];
  11.     alpha[1][3] = -2.0 * x[3];
  12.     alpha[1][4] = 1.0;
  13.     alpha[2][1] = 2.0 * x[1];
  14.     alpha[2][2] = 2.0 * x[2];
  15.     alpha[2][3] = 2.0 * x[3];
  16.     alpha[2][4] = 2.0 * x[4];
  17.     alpha[3][1] = 1.0;
  18.     alpha[3][2] = -1.0;
  19.     alpha[3][3] = 0.0;
  20.     alpha[3][4] = 0.0;
  21.     alpha[4][1] = 0.0;
  22.     alpha[4][2] = 1.0;
  23.     alpha[4][3] = -1.0;
  24.     alpha[4][4] = 0.0;
  25.     beta[1]= x[1]*x[1]+x[2]*x[2]+x[3]*x[3]-x[4];
  26.     beta[2]=-x[1]*x[1]-x[2]*x[2]-x[3]*x[3]-x[4]*x[4]+1.0;
  27.     beta[3]=-x[1]+x[2];
  28.     beta[4]=-x[2]+x[3];
  29. }

  30. void main()
  31. {
  32.     //program d10r13
  33.     //driver for routine mnewt
  34.     int i,j,kk,k,ntrial,np,n;
  35.     double tolx,tolf,xx;
  36.     ntrial = 5;
  37.     tolx = 0.000001;
  38.     n = 4;
  39.     tolf = 0.000001;
  40.     np = 15;
  41.     double x[16],alpha[16][16],beta[16];
  42.     for (kk = -1; kk<= 1; kk=kk+2)
  43.     {
  44.         for (k =1; k<=3; k++)
  45.         {
  46.             xx = 0.2 * k * kk;
  47.             cout<< "Starting vector number "<<k<<endl;
  48.             for (i = 1; i<=4; i++)
  49.             {
  50.                 x[i] = xx + 0.2 * i;
  51.                 cout<<"x("<<i<<")= "<<x[i]<<endl;
  52.             }
  53.             for (j = 1;j <=ntrial; j++)
  54.             {
  55.                 mnewt(1,x,n,tolx,tolf);
  56.                 usrfun(x,alpha,beta);
  57.                 cout<<"  i        x(i)            f"<<endl;
  58.                 for (i = 1; i<=n; i++)
  59.                 {
  60.                     cout<< setw(3)<< i;
  61.                     cout<< setw(14)<< x[i];
  62.                     cout<< setw(16)<< -beta[i]<<endl;
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
复制代码

  1. void mnewt(int ntrial,double x[16],int n,double tolx,double tolf)
  2. {
  3.     double alpha[16][16],beta[16];
  4.     int k,i,indx[16];
  5.     double errf,d,errx;
  6.     for (k = 1; k<=ntrial; k++)
  7.     {
  8.         usrfun(x,alpha,beta);
  9.         errf = 0.0;
  10.         for (i = 1; i<=n; i++)
  11.             errf = errf + fabs(beta[i]);
  12.         if (errf <= tolf)  _c_exit();
  13.         ludcmp(alpha,n,indx,d);
  14.         lubksb(alpha,n,indx,beta);
  15.         errx = 0.0;
  16.         for (i = 1; i<=n; i++)
  17.         {
  18.             errx = errx + fabs(beta[i]);
  19.             x[i] = x[i] + beta[i];
  20.         }
  21.         if (errx <= tolx) _c_exit();
  22.     }
  23. }
复制代码

评分

1

查看全部评分

发表于 2009-3-25 21:30 | 显示全部楼层
发表于 2009-4-3 08:58 | 显示全部楼层
谢谢分享,已经有些概念了
发表于 2012-11-15 22:28 | 显示全部楼层
这也太复杂了吧,我的求解牛顿迭代的时候出现了复数,是初始值不对还是精度不对啊
发表于 2012-12-18 16:18 | 显示全部楼层
的确复杂了,有些还是C++
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-12 03:06 , Processed in 0.068656 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表