Hey, I am a novice to this, and now have to build a program to read sequentially, line by line, the coefficients of a cubic polynomial, and the initial guess, approximate the root and display the number of iterations it took for it to be correct to within 5e-6, or if it fails to converge, to add 10 to the estimate and try again, if it the fails, display a fail message and move to the next line of the input file, do it all again and again until it reaches the end of input file and quits.
In short, I am more than a little screwed, and will barter my firstborn for help.
Hello~~ A cubic polynomial has at least one real root while your solution may sometimes fail to find one.
I wrote a toy program which solves this problem, but not completely.
Here's the code. This program runs correctly in Windows.
#include <iostream>
#include <assert.h>
using namespace std;
Thanks heaps, that was really helpful to see the way you strung together the functions, I am actually getting correct answers! now i just need to get the data from the .dat file and assign the values, then loop the calculator until the end of the file.
It is just a bit much for a first year assignment for me...
sorry to have made a mistake in my code.
the code segment if...else if....else if...else should be replaced by the following code.
if (curF > 0)
{
preF = curF;
step = -STEP;
preX = curX;
curX = curX + step;
curF = F(curX);
count++;
}
else
{
preF = curF;
preX = curX;
curX = curX + STEP;
curF = F(curX);
count++;
}
Calculating the differentiation of F(x) is of no help.