By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

newton raphson method

P: 3
can you help me please with this problem..
i want a c program using newton raphson method for solving 18 equations...
the equations are not of degree greater than 1...
i need the program to input my 18 equations and give me the result
May 3 '07 #1
Share this Question
Share on Google+
11 Replies


P: 24
Where are you stuck?
May 3 '07 #2

P: 6
can you help me please with this problem..
i want a c program using newton raphson method for solving 18 equations...
the equations are not of degree greater than 1...
i need the program to input my 18 equations and give me the result
Ok here is the program for you. I am providing you the whole source code. But mind you, the correct way of learning things is by making mistakes. I would have liked it much, if you would have put whatever source code you could develop for the problem, on your own, but.. never mind.

Take care in future. Learning is the process which should start from self.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<math.h>
  3. #define LB -1
  4. #define UB 1
  5. using namespace std;
  6. class Newton
  7. {
  8.  
  9.     double a,b,x0,x1,tolerance;
  10.     double Fa,Fb,Fx0,h,root[100],dFx0,Fx1,cv,pv,Fcv;
  11.     int rootIndex;
  12.  
  13.     public:
  14.         Newton()
  15.         {
  16.             tolerance = 10E-4;
  17.             rootIndex = -1;
  18.         }
  19.  
  20.         double fun_value(int,double);
  21.         void find_root(int);
  22.         bool   validInterval(double);
  23.         double deriv_fun(int,double);
  24.  
  25. };
  26. bool   Newton::validInterval(double z)
  27. {
  28.     if(z<0)
  29.         return true;
  30.     else
  31.         return false;    
  32. }
  33. void Newton::find_root(int degree)
  34. {
  35.     int count = 1;
  36.     char ch;
  37.     double diff;
  38.     h = (UB-LB)/(double)degree;
  39.     while(rootIndex < degree)
  40.     {
  41.         if(rootIndex != -1)
  42.             h = h /2;
  43.         rootIndex = 0;        
  44.         for(int i = 0;i<degree;i++)
  45.         {
  46.             a = LB + (i*h);
  47.             b = LB + ((i+1)*h);
  48.             while(1)
  49.             {
  50.                     Fa = fun_value(degree,a);
  51.                     Fb = fun_value(degree,b);
  52.                     if(validInterval(Fa*Fb))
  53.                     {
  54.                         x0 = (a+b)/2;
  55.                         pv=x0;
  56.                         while(1)
  57.                         {    
  58.                             cv = pv - (fun_value(degree,pv)/deriv_fun(degree,pv));
  59.                             Fcv = fun_value(degree,cv);
  60.                             if(fabs(Fcv)<tolerance)
  61.                             {
  62.                                 root[rootIndex++] = cv;
  63.                                 goto end;
  64.                             }        
  65.                             pv = cv;
  66.                         }    
  67.                     }     
  68.                     else
  69.                     {
  70.                         root[rootIndex++] = a;
  71.                         break;
  72.                     }
  73.  
  74.             }//end of While    
  75.         end:    
  76.         cout<<"";
  77.         }//end of For
  78.     }//end of for
  79.     cout<<"\n\nThe roots are===>\n";
  80.     for(int i = 0;i<rootIndex;i++)
  81.         cout<<"  "<<i+1<<".  "<<root[i]<<endl;
  82. }
  83. double Newton::fun_value(int degree ,double x)
  84. {
  85.  
  86.     switch(degree)
  87.     {
  88.          case 0:
  89.              return 1;
  90.          case 1:
  91.              return x;
  92.          case 2:
  93.               return(2*x*x - 1);
  94.           case 3:
  95.              return (4*x*x*x - 3*x);
  96.          case 4:
  97.              return(8*x*x*x*x - 8*x*x + 1);
  98.          case 5:
  99.              return(16*x*x*x*x*x - 20*x*x*x + 5*x);
  100.     }
  101. }
  102.  
  103. double Newton::deriv_fun(int degree ,double x)
  104. {
  105.     switch(degree)
  106.     {
  107.          case 0:
  108.              return 0;
  109.          case 1:
  110.              return 1;
  111.          case 2:
  112.               return(4*x);
  113.           case 3:
  114.              return (12*x*x - 3);
  115.          case 4:
  116.              return(32*x*x*x - 16*x);
  117.          case 5:
  118.              return(80*x*x*x*x - 60*x*x + 5);
  119.     }
  120. }
  121.  
  122. main()
  123. {
  124.     Newton N;
  125.     int degree;
  126.     double *root;
  127.     cout<<"Enter the degree of equation it should be in between 0 to 5\n";
  128.     cin>>degree;
  129.     N.find_root(degree);//calculates the root
  130.     return 0;
  131. }
  132.  

Cheers!

-Sandy
May 3 '07 #3

Expert 100+
P: 181
Ok here is the program for you. I am providing you the whole source code. But mind you, the correct way of learning things is by making mistakes. I would have liked it much, if you would have put whatever source code you could develop for the problem, on your own, but.. never mind.

Take care in future. Learning is the process which should start from self.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<math.h>
  3. #define LB -1
  4. #define UB 1
  5. using namespace std;
  6. class Newton
  7. {
  8.  
  9.     double a,b,x0,x1,tolerance;
  10.     double Fa,Fb,Fx0,h,root[100],dFx0,Fx1,cv,pv,Fcv;
  11.     int rootIndex;
  12.  
  13.     public:
  14.         Newton()
  15.         {
  16.             tolerance = 10E-4;
  17.             rootIndex = -1;
  18.         }
  19.  
  20.         double fun_value(int,double);
  21.         void find_root(int);
  22.         bool validInterval(double);
  23.         double deriv_fun(int,double);
  24.  
  25. };
  26. bool Newton::validInterval(double z)
  27. {
  28.     if(z<0)
  29.         return true;
  30.     else
  31.         return false;    
  32. }
  33. void Newton::find_root(int degree)
  34. {
  35.     int count = 1;
  36.     char ch;
  37.     double diff;
  38.     h = (UB-LB)/(double)degree;
  39.     while(rootIndex < degree)
  40.     {
  41.         if(rootIndex != -1)
  42.             h = h /2;
  43.         rootIndex = 0;        
  44.         for(int i = 0;i<degree;i++)
  45.         {
  46.             a = LB + (i*h);
  47.             b = LB + ((i+1)*h);
  48.             while(1)
  49.             {
  50.                     Fa = fun_value(degree,a);
  51.                     Fb = fun_value(degree,b);
  52.                     if(validInterval(Fa*Fb))
  53.                     {
  54.                         x0 = (a+b)/2;
  55.                         pv=x0;
  56.                         while(1)
  57.                         {    
  58.                             cv = pv - (fun_value(degree,pv)/deriv_fun(degree,pv));
  59.                             Fcv = fun_value(degree,cv);
  60.                             if(fabs(Fcv)<tolerance)
  61.                             {
  62.                                 root[rootIndex++] = cv;
  63.                                 goto end;
  64.                             }        
  65.                             pv = cv;
  66.                         }    
  67.                     }     
  68.                     else
  69.                     {
  70.                         root[rootIndex++] = a;
  71.                         break;
  72.                     }
  73.  
  74.             }//end of While    
  75.         end:    
  76.         cout<<"";
  77.         }//end of For
  78.     }//end of for
  79.     cout<<"\n\nThe roots are===>\n";
  80.     for(int i = 0;i<rootIndex;i++)
  81.         cout<<" "<<i+1<<". "<<root[i]<<endl;
  82. }
  83. double Newton::fun_value(int degree ,double x)
  84. {
  85.  
  86.     switch(degree)
  87.     {
  88.          case 0:
  89.              return 1;
  90.          case 1:
  91.              return x;
  92.          case 2:
  93.              return(2*x*x - 1);
  94.           case 3:
  95.              return (4*x*x*x - 3*x);
  96.          case 4:
  97.              return(8*x*x*x*x - 8*x*x + 1);
  98.          case 5:
  99.              return(16*x*x*x*x*x - 20*x*x*x + 5*x);
  100.     }
  101. }
  102.  
  103. double Newton::deriv_fun(int degree ,double x)
  104. {
  105.     switch(degree)
  106.     {
  107.          case 0:
  108.              return 0;
  109.          case 1:
  110.              return 1;
  111.          case 2:
  112.              return(4*x);
  113.           case 3:
  114.              return (12*x*x - 3);
  115.          case 4:
  116.              return(32*x*x*x - 16*x);
  117.          case 5:
  118.              return(80*x*x*x*x - 60*x*x + 5);
  119.     }
  120. }
  121.  
  122. main()
  123. {
  124.     Newton N;
  125.     int degree;
  126.     double *root;
  127.     cout<<"Enter the degree of equation it should be in between 0 to 5\n";
  128.     cin>>degree;
  129.     N.find_root(degree);//calculates the root
  130.     return 0;
  131. }
  132.  

Cheers!

-Sandy
Sandy if u give code, it would definitely stop him from thinking, and this would stop his programming ability.

The world might loose one good programmer.
May 3 '07 #4

P: 6
Sandy if u give code, it would definitely stop him from thinking, and this would stop his programming ability.

The world might loose one good programmer.
Hey svlsr2000,

U r right buddy. I'll take care in future.

Cheers!

-Sandy
May 3 '07 #5

P: 3
hey sandy ur code is giving errors...i am not a comp programmer but need the program for my major project..pls help
May 3 '07 #6

P: 3
hey i am not a comp programmer...i am a mechanical engg. student and need the program for my final yr project...pls help me out..i dont know how to remove errors...pls help me out with a program that has no errors
May 3 '07 #7

P: 6
hey i am not a comp programmer...i am a mechanical engg. student and need the program for my final yr project...pls help me out..i dont know how to remove errors...pls help me out with a program that has no errors
Kartike,

The program that I have posted as an reply is not the exact answer for your problem. However it will help you to cruise towards the actual solution, as it is very close to the sol for your problem. Yes it contains an error( remove the line no 5 to get rid of the error if you are using MS visual studio or a similar compiler in windows environment,.. unix/linux should not cause this error to occur).

Buddy, please try to understand that this community is not for finding source code / solutions for your issue but to invent the solution with a proper learning experience/process.

Am sorry I can't help you further as it will take away all the charm of learning programming. And moreover, the community doesn't permit me to do so.

Hope you understand and look for alternative solutions/resources. But if you ask me, I'll suggest you to start with learning programming. Who knows, you could be the world next best programmer :)

Cheer up!

-Sandy
May 3 '07 #8

P: 15
Kartike,

The program that I have posted as an reply is not the exact answer for your problem. However it will help you to cruise towards the actual solution, as it is very close to the sol for your problem. Yes it contains an error( remove the line no 5 to get rid of the error if you are using MS visual studio or a similar compiler in windows environment,.. unix/linux should not cause this error to occur).

Buddy, please try to understand that this community is not for finding source code / solutions for your issue but to invent the solution with a proper learning experience/process.

Am sorry I can't help you further as it will take away all the charm of learning programming. And moreover, the community doesn't permit me to do so.

Hope you understand and look for alternative solutions/resources. But if you ask me, I'll suggest you to start with learning programming. Who knows, you could be the world next best programmer :)

Cheer up!

-Sandy
I also need this method but I'm programming in VB2005? Is it possible for you to convert it for me or tell me where to find it? Just need to solve a 3 non linear equation system.

Thank you in advance!
May 16 '07 #9

AdrianH
Expert 100+
P: 1,251
'k, please abide by the posting guidelines, no posting of complete source code.

If you need an equation solver and it is not an assignment, why not use Maple? MathCad or other prebuild equation solver?


Adrian
May 16 '07 #10

P: 15
'k, please abide by the posting guidelines, no posting of complete source code.

If you need an equation solver and it is not an assignment, why not use Maple? MathCad or other prebuild equation solver?


Adrian
That's a good idea. But I cannot see how I can integrate it inside the code that I allready have. Can you give me an hint?
May 16 '07 #11

AdrianH
Expert 100+
P: 1,251
That's a good idea. But I cannot see how I can integrate it inside the code that I allready have. Can you give me an hint?
You have one of these on hand? Maple is easy because there is a termanl based version so you can pipe in your commands from there. I'm not sure about MathCad though.


Adrian
May 16 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.