beginner wanting criticism
I'm teaching myself c++ and giving myself random stuff to program. This program works, mostly, but I know for a fact that it could have been done much much better. This program is basically Newton's Method http://en.wikipedia.org/wiki/Newtons_method for polynomials. I'm mainly seeking out criticism on this code because I know it isn't the best quality. I know I shouldn't use global variables and I should do comments, but ya...
Code:
#include <iostream>
#include "math.h"
using namespace std;
double a;
double b;
double c;
double d;
double e;
double f;
double linearsolver(double slope, double x_coordinate, double y_coordinate)
{
double solution = 0 - y_coordinate;
solution = solution/slope;
solution = solution + x_coordinate;
return solution;
}
double equation(double x)
{
double result;
result=a*x*x*x*x*x+b*x*x*x*x+c*x*x*x+d*x*x+e*x+f;
return result;
}
double differentiation(double number_diff_at)
{
double i=0.001;
double result,neg_result;
do
{
result=(equation(number_diff_at+i)-equation(number_diff_at)/i;
neg_result=(equation(number_diff_at-i)-equation(number_diff_at))/(-i);
if(floor(result-neg_result)!=0)
{
i=i*i;
}
}while(floor(result-neg_result)!=0);
double deriv_result=(result+neg_result)/2;
return deriv_result;
}
double newtons_method(double initialguess, int iterations)
{
double x=initialguess;
double y,slope,result;
int n;
for(n=iterations;n!=0;n--)
{
slope=differentiation(x);
y=equation(x);
result=linearsolver(slope,x,y);
x=result;
}
return result;
}
int main()
{
double initialguess, result;
int n;
cout<<"a: ";
cin>>a;
cout<<"b: ";
cin>>b;
cout<<"c: ";
cin>>c;
cout<<"d: ";
cin>>d;
cout<<"e: ";
cin>>e;
cout<<"f: ";
cin>>f;
cout<<"\ninitial guess: ";
cin>>initialguess;
cout<<"iterations: ";
cin>>n;
result=newtons_method(initialguess,n);
cout<<"\nsolution is: "<<result<<"\n";
cin>>n;
return 0;
}
Re: beginner wanting criticism
This program accepts any user input, without checking it. Does this algorithm have some input restrictions (for example, some variable must be non-negative)? In the case there are such restrictions, you need to check the values typed by user.
If you write in C++, why don't you use classes? Think about class that has a, b... f member variables, and linearsolver, equation.. methods. Main function creates such class instance, fills its members, and calls newtons_method function.
Local variables like initialguess, slope, result have pretty informative names. However, general parameters have short and useless names like x, y.
In some places indentation looks a bit strange. Visual Studio "Format Selected Text" function is a good way to fix this.
Generally, this program is readable and clear.
Re: beginner wanting criticism
Quote:
Originally Posted by
clintavian
Code:
double linearsolver(double slope, double x_coordinate, double y_coordinate)
{
double solution = 0 - y_coordinate;
solution = solution/slope;
solution = solution + x_coordinate;
return solution;
}
This is unacceptably verbose. At the least you should write.
Code:
double solution = -y_coordinate;
solution /= slope;
solution += x_coordinate;
return solution;
However, I would just write a single line.
Code:
return -y_coordinate / slope + x_coordinate;
Quote:
Originally Posted by
clintavian
Code:
double equation(double x)
{
double result;
result=a*x*x*x*x*x+b*x*x*x*x+c*x*x*x+d*x*x+e*x+f;
return result;
}
There's no need for a local variable if you write it like this. But you may be able to improve performance by changing it to
Code:
double x2 = x * x;
double x3 = x * x2;
double x4 = x * x3;
double x5 = x * x4;
return a * x5 + b * x4 + ... = f;
Note the use of spaces between operators to make the code easier to read.
Re: beginner wanting criticism
Quote:
Originally Posted by
clintavian
Perhaps you meant
or better yet:
Re: beginner wanting criticism
I don't think I'd write something like this.
result=a*x*x*x*x*x+b*x*x*x*x+c*x*x*x+d*x*x+e*x+f;
At least wrap some parentheses around the various parts of it. That will make it clearer to the reader exactly what order the calculations are being performed in, and probably help you avoid mistakes when you don't get the calculation order right.
Re: beginner wanting criticism
And for any mathematicians out there ...
Code:
double equation_horner(double x)
{
return f + x * (e + x * (d + x * (c + x * (b + x*a))));
}
sorry.