newton raphson method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kartikegarg
    New Member
    • May 2007
    • 3

    newton raphson method

    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
  • stroken
    New Member
    • Mar 2007
    • 24

    #2
    Where are you stuck?

    Comment

    • sandy123
      New Member
      • Feb 2007
      • 6

      #3
      Originally posted by kartikegarg
      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.

      Code:
      #include<iostream.h>
      #include<math.h>
      #define LB -1
      #define UB 1
      using namespace std;
      class Newton
      {
      	
      	double a,b,x0,x1,tolerance;
      	double Fa,Fb,Fx0,h,root[100],dFx0,Fx1,cv,pv,Fcv;
      	int rootIndex;
      	
      	public:
      		Newton()
      		{
      			tolerance = 10E-4;
      			rootIndex = -1;
      		}
      			       
      		double fun_value(int,double);
      		void find_root(int);
      		bool   validInterval(double);
      		double deriv_fun(int,double);
      		
      };
      bool   Newton::validInterval(double z)
      {
      	if(z<0)
      		return true;
      	else
      		return false;	
      }
      void Newton::find_root(int degree)
      {
      	int count = 1;
      	char ch;
      	double diff;
      	h = (UB-LB)/(double)degree;
      	while(rootIndex < degree)
      	{
      		if(rootIndex != -1)
      			h = h /2;
      		rootIndex = 0;		
      		for(int i = 0;i<degree;i++)
      		{
      			a = LB + (i*h);
      			b = LB + ((i+1)*h);
      			while(1)
      			{
      					Fa = fun_value(degree,a);
      					Fb = fun_value(degree,b);
      					if(validInterval(Fa*Fb))
      					{
      						x0 = (a+b)/2;
      						pv=x0;
      						while(1)
      						{	
      							cv = pv - (fun_value(degree,pv)/deriv_fun(degree,pv));
      							Fcv = fun_value(degree,cv);
      							if(fabs(Fcv)<tolerance)
      							{
      								root[rootIndex++] = cv;
      								goto end;
      							}		
      							pv = cv;
      						}	
      					}	 
      					else
      					{
      						root[rootIndex++] = a;
      						break;
      					}
      					
      			}//end of While	
      		end:	
      		cout<<"";
      		}//end of For
      	}//end of for
      	cout<<"\n\nThe roots are===>\n";
      	for(int i = 0;i<rootIndex;i++)
      		cout<<"  "<<i+1<<".  "<<root[i]<<endl;
      }
      double Newton::fun_value(int degree ,double x)
      {
      	
      	switch(degree)
      	{
      		 case 0:
      		 	return 1;
      		 case 1:
      		 	return x;
      		 case 2:
      	  		return(2*x*x - 1);
      	 	 case 3:
      		 	return (4*x*x*x - 3*x);
      		 case 4:
      		 	return(8*x*x*x*x - 8*x*x + 1);
      		 case 5:
      		 	return(16*x*x*x*x*x - 20*x*x*x + 5*x);
      	}
      }
      
      double Newton::deriv_fun(int degree ,double x)
      {
      	switch(degree)
      	{
      		 case 0:
      		 	return 0;
      		 case 1:
      		 	return 1;
      		 case 2:
      	  		return(4*x);
      	 	 case 3:
      		 	return (12*x*x - 3);
      		 case 4:
      		 	return(32*x*x*x - 16*x);
      		 case 5:
      		 	return(80*x*x*x*x - 60*x*x + 5);
      	}
      }
      
      main()
      {
      	Newton N;
      	int degree;
      	double *root;
      	cout<<"Enter the degree of equation it should be in between 0 to 5\n";
      	cin>>degree;
      	N.find_root(degree);//calculates the root
      	return 0;
      }

      Cheers!

      -Sandy

      Comment

      • svlsr2000
        Recognized Expert New Member
        • Feb 2007
        • 181

        #4
        Originally posted by sandy123
        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.

        Code:
        #include<iostream.h>
        #include<math.h>
        #define LB -1
        #define UB 1
        using namespace std;
        class Newton
        {
         
        	double a,b,x0,x1,tolerance;
        	double Fa,Fb,Fx0,h,root[100],dFx0,Fx1,cv,pv,Fcv;
        	int rootIndex;
         
        	public:
        		Newton()
        		{
        			tolerance = 10E-4;
        			rootIndex = -1;
        		}
         
        		double fun_value(int,double);
        		void find_root(int);
        		bool validInterval(double);
        		double deriv_fun(int,double);
         
        };
        bool Newton::validInterval(double z)
        {
        	if(z<0)
        		return true;
        	else
        		return false;	
        }
        void Newton::find_root(int degree)
        {
        	int count = 1;
        	char ch;
        	double diff;
        	h = (UB-LB)/(double)degree;
        	while(rootIndex < degree)
        	{
        		if(rootIndex != -1)
        			h = h /2;
        		rootIndex = 0;		
        		for(int i = 0;i<degree;i++)
        		{
        			a = LB + (i*h);
        			b = LB + ((i+1)*h);
        			while(1)
        			{
        					Fa = fun_value(degree,a);
        					Fb = fun_value(degree,b);
        					if(validInterval(Fa*Fb))
        					{
        						x0 = (a+b)/2;
        						pv=x0;
        						while(1)
        						{	
        							cv = pv - (fun_value(degree,pv)/deriv_fun(degree,pv));
        							Fcv = fun_value(degree,cv);
        							if(fabs(Fcv)<tolerance)
        							{
        								root[rootIndex++] = cv;
        								goto end;
        							}		
        							pv = cv;
        						}	
        					}	 
        					else
        					{
        						root[rootIndex++] = a;
        						break;
        					}
         
        			}//end of While	
        		end:	
        		cout<<"";
        		}//end of For
        	}//end of for
        	cout<<"\n\nThe roots are===>\n";
        	for(int i = 0;i<rootIndex;i++)
        		cout<<" "<<i+1<<". "<<root[i]<<endl;
        }
        double Newton::fun_value(int degree ,double x)
        {
         
        	switch(degree)
        	{
        		 case 0:
        			 return 1;
        		 case 1:
        			 return x;
        		 case 2:
        			 return(2*x*x - 1);
        		  case 3:
        			 return (4*x*x*x - 3*x);
        		 case 4:
        			 return(8*x*x*x*x - 8*x*x + 1);
        		 case 5:
        			 return(16*x*x*x*x*x - 20*x*x*x + 5*x);
        	}
        }
         
        double Newton::deriv_fun(int degree ,double x)
        {
        	switch(degree)
        	{
        		 case 0:
        			 return 0;
        		 case 1:
        			 return 1;
        		 case 2:
        			 return(4*x);
        		  case 3:
        			 return (12*x*x - 3);
        		 case 4:
        			 return(32*x*x*x - 16*x);
        		 case 5:
        			 return(80*x*x*x*x - 60*x*x + 5);
        	}
        }
         
        main()
        {
        	Newton N;
        	int degree;
        	double *root;
        	cout<<"Enter the degree of equation it should be in between 0 to 5\n";
        	cin>>degree;
        	N.find_root(degree);//calculates the root
        	return 0;
        }

        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.

        Comment

        • sandy123
          New Member
          • Feb 2007
          • 6

          #5
          Originally posted by svlsr2000
          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

          Comment

          • kartikegarg
            New Member
            • May 2007
            • 3

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

            Comment

            • kartikegarg
              New Member
              • May 2007
              • 3

              #7
              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

              Comment

              • sandy123
                New Member
                • Feb 2007
                • 6

                #8
                Originally posted by kartikegarg
                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

                Comment

                • spranto
                  New Member
                  • May 2007
                  • 15

                  #9
                  Originally posted by sandy123
                  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!

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    '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

                    Comment

                    • spranto
                      New Member
                      • May 2007
                      • 15

                      #11
                      Originally posted by AdrianH
                      '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?

                      Comment

                      • AdrianH
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1251

                        #12
                        Originally posted by spranto
                        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

                        Comment

                        Working...