Hi,
i am writing a program to manipulate polynomials. however, i am having problems with my root function. this function is meant to find the real roots of a polynomial using newton-raphson formula. please help me.
#include<iostream>
#include <sstream>
using namespace std;
//A class to handle a list node
class Lnode{
public:
Lnode();
Lnode(int);
int data;
Lnode *next;//This is the pointer to the next node
};
Lnode::Lnode(){
data = 0;
next = NULL;
}
Lnode::Lnode(int d){
data = d;
next = NULL;
}
//Basic Linked List ADT around the Lnode class
class LList{
public:
LList();
LList(int);
int getSize();
void set(int, int);
void print();
void printP();
void add(LList);
void sub(LList);
void mult(LList);
double eval(double);
double pdash(double);
double pdashdash(double);
double root(int);
private:
void addToF(int);//adding a number to the front of the list.
Lnode *head, *tail;//a list has two pointers, head and tail.
//both are pointing to the already defined List node.
int size;
};
//Constructor Definition
LList::LList(){
size = 0;
head = tail = NULL;//initially, there is nothing. none of them exist.
}
LList::LList(int n){
head=tail=NULL;
size = n;
for(int i=size; i>0; i--)
addToF(0);
}
int LList::getSize(){
return size;//to know the size of the list
}
void LList::addToF(int d){
Lnode *nnode = new Lnode(d);//pointer to the list node
nnode -> next=head;
head = nnode;
if(tail==NULL)
tail=head;//head and tail are pointing to the same no.
}
void LList::print(){
Lnode *current = head;
while( current!= NULL)
{
cout<<current->data<<" ";
current = current->next;
}
}
void LList::printP(){
Lnode *current = head;
int degree = size-1;
ostringstream buff;
while( current!= NULL)
{
buff<<current->data<<"x^"<<degree<<" "<<"+";
current = current->next;
degree--;
}
std::string output = buff.str();
output[output.length()-1]='\0';
cout<<output;
}
void LList::set(int c, int val)
{
int i = size-1;
Lnode *current=head;
while(current != NULL)
{
if(i==(c))
{
current->data = val;
break;
}
else
{
current = current->next;
i--;
}
}
}
void LList::add(LList L){
Lnode *ptr1;
Lnode *ptr2;
ptr1 = head;
ptr2 = L.head;
while(ptr1 != NULL && ptr2 != NULL)
{
ptr1->data = ptr1->data+ptr2->data;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
}
void LList::sub(LList L){
Lnode *ptr1;
Lnode *ptr2;
ptr1 = head;
ptr2 = L.head;
while(ptr1 != NULL && ptr2 != NULL)
{
ptr1->data = ptr1->data-ptr2->data;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
}
void LList::mult(LList L){
Lnode *ptr1;
Lnode *ptr2;
ptr1 = head;
ptr2 = L.head;
while(ptr1 != NULL && ptr2 != NULL)
{
ptr1->data = ptr1->data*ptr2->data;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
}
double LList::eval(double x){
double sum = 0;
Lnode *temp = head;
for(int i=size-1; i>=0; i--)
{
sum = sum*x + temp->data;
temp = temp->next;
}
return sum;
}
double LList::pdash(double x){
const double h=1.0e-3;
return (eval(x+h)- eval(x-h))/(2*h);
}
double LList::pdashdash(double x){
const double h=1.0e-3;
return (eval(x+h)-(2*eval(x)) + eval(x-h))/(h*h);
}
double LList::root(int x){
return(x - (eval(x)/(pdash(x))));
}
//Driver Main Program
int main(){
LList a(3);
a.set(2, 1);
a.set(1, -7);
a.set(0, -120);
cout<<"First Polynomial is:"<<" ";
a.printP();
cout<<endl;
cout<<endl;
cout<<"Evaluation of First Polynomial is:"<<" ";
cout<<a.eval(2.0)<<endl;
cout<<endl;
cout<<"Derivative of First Polynomial is:"<<" ";
cout<<a.pdash(2.0)<<endl;
cout<<endl;
cout<<"Second Derivative of First Polynomial is:"<<" ";
cout<<a.pdashdash(2.0)<<endl;
cout<<endl;
cout<<a.root(-1)<<endl;
cout<<a.root(10)<<endl;
}
thanks in advance