Given this 4th order polynomial, x^4 - 3x^3 -15x^2 +10x +30, use lab 17 to construct a plotting routine from x = -3.5 to x = +5.5.
increment 0.1, by replacing the return value of the function with a0 * pow(x,4) + a1 * pow(x,3) + a2 * pow(x,2) + a3*x +a4; (drop
the if, no worries about x=0) and define, a0, a1, a2, a3 ,a4 in the function with the values from the latter equation, be careful of
signs. The output to a file ( you name in its filename) needed for plotting in excel to plot this polynomial. Plot both axis with
appropriate values from the imported values.
How would I edit this code (Lab 17) to be able to plot for this new 4th degree polynomial ? I understand I have to define the coefficients of the polynomial, but to I define them at int, or as double on line 10 in parentheses?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include<iostream> //Required for cin, cout
#include<cmath>
#include<fstream> // to build data file to be used by Excel to plot
#include <string> // for using filename and
using namespace std;
double ROBBINS (double x); //Function Prototype
int
main ()
{
double a, b, x_incr, new_x; // Declare objects in main()
int Number, loop;
string filename;
ofstream fout; // declare object for sending data to the output file
cout <<
"enter the name of the data file to send data used for plotting, from this program\n";
cin >> filename;
fout.open (filename); // open file for inputing data from this program
cout << "Enter endpoint range a and b (a<b): of the function to be plotted \n";
cin >> a >> b;
cout <<
"Enter the number of points you want over the range of 'a' to 'b'\n";
cin >> Number;
x_incr = (b - a) / Number; // increment between the points based on number wanted
loop = int (b - a) / x_incr; // loop gives the for loop repetitions equal to number points wanted
cout << loop; // check that shows loop and Number are the same
cout.setf (ios::fixed);
cout.precision (6); // Set Formats
cout << "x and ROBBINSX \n";
for (int k = 0; k <= loop; k++)
{
new_x = a + k * x_incr;
cout << new_x << " " << ROBBINS (new_x) << endl; //echo vales to screen
fout << new_x << " " << ROBBINS (new_x) << endl; // building data file for excel plotting
}
return 0; // Exit program.
fout.close ();
} // end main unction used on the next slide
double ROBBINS (double x)
if (x == 0)
{
return 1;
}
else
{
return -exp(fabs(x)/10) /fabs(x);
}
} // end of function
|