473,614 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

newton raphson method

3 New Member
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
11 9120
stroken
24 New Member
Where are you stuck?
May 3 '07 #2
sandy123
6 New Member
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
svlsr2000
181 Recognized Expert New Member
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
sandy123
6 New Member
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
kartikegarg
3 New Member
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
kartikegarg
3 New Member
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
sandy123
6 New Member
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
spranto
15 New Member
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
1,251 Recognized Expert Top Contributor
'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

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

Similar topics

6
29313
by: sekitoleko | last post by:
c program for newton raphson algorithm for finding roots of a polynomial
2
1473
by: aljoker2003 | last post by:
i need to solve this problem quickly please i want a help from you http://abo3s3s.com/download.php?filename=e331daf644.zip
1
3002
by: dynamo | last post by:
Hi guys,i was wondering if anyone knows the code to solve equations using the newton raphson method in matlab.Or at least the algorithm.
2
2859
by: thiofdelux | last post by:
I need a c++ program that uses newton raphson method to find the rooys of a function. The program would prompt to put in the degree of the ploynomial, the error bound 10^(n), and the first approximation. Anybody please help if you can. i have been working on this for quite a while. Thanks
1
3926
by: lionkng | last post by:
I need a simple code for finding roots in c programme using Newton-raphson method
1
6086
by: ripal ruparelia | last post by:
how to solve x-e^-x=0 by newton raphson method
0
8198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8142
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8591
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8294
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8444
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5549
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.