I have a school problem but I do not understand what it actually asks. Any of you have an idea what it's really asking for? I don't need code, I just need to understand it.
This is the problem:
Construct a computer program that uses the Secant method to solve the problem:
f(x) = (1+x) cos( sin(x)3 ) - 1.4 = 0
Starting with the initial guesses of x=2.0 and x=2.1, obtain an approximation to x such that |f(x)| < 0.0000001.
This is my code from what I understand, but I think I'm not understanding the question correctly.
#include <iostream>
#include <cmath>
double secant(double x);
using namespace std;
int main()
{
double x = 2.0;
double r = 0.0;
int counter = 0;
while( r < 0 && counter <= 40)
{
r =secant(x);
cout << "x: " << x << ", f(x): " << r << endl;
counter++;
x += 0.1;
}
return 0;
}
double secant(double x)
{
double r;
r = (1+x) * cos(pow(sin(x), 3.0)) - 1.4;
return r;
}
You are supposed to use the Secant Method: http://en.wikipedia.org/wiki/Secant_method
Follow the method as described in the article. It is an iterative method much like Netwon's method. You'll need to make a function to evaluate x(n+1) given x(n) and iterate it until your margin of error is less than specified.
The coding side of this may prove fairly straightforward as long as you know what the secant method is. Also, that page has a code example. That should prove pretty useful. :)
Related
Hi I am trying to calculate the results of the Taylor series expansion for sine to the specified number of terms.
I am running into some problems
Your task is to implement makeSineToOrder(k)
This is templated by the type of values used in the calculation.
It must yield a function that takes a value of the specified type and
returns the sine of that value (in the specified type again)
double factorial(double long order){
#include <iostream>
#include <iomanip>
#include <cmath>
double fact = 1;
for(int i = 1; i <= num; i++){
fact *= i;
}
return fact;
}
void makeSineToOrder(long double order,long double precision = 15){
double value = 0;
for(int n = 0; n < precision; n++){
value += pow(-1.0, n) * pow(num, 2*n+1) / factorial(2*n + 1);
}
return value;
int main()
{
using namespace std;
long double pi = 3.14159265358979323846264338327950288419716939937510L;
for(int order = 1;order < 20; order++) {
auto sine = makeSineToOrder<long double>(order);
cout << "order(" << order << ") -> sine(pi) = " << setprecision(15) << sine(pi) << endl;
}
return 0;
}
I tried debugging
here is a version that at least compiles and gives some output
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double factorial(double long num) {
double fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
double makeSineToOrder(double num, double precision = 15) {
double value = 0;
for (int n = 0; n < precision; n++) {
value += pow(-1.0, n) * pow(num, 2 * n + 1) / factorial(2 * n + 1);
}
return value;
}
int main(){
long double pi = 3.14159265358979323846264338327950288419716939937510L;
for (int order = 1; order < 20; order++) {
auto sine = makeSineToOrder(order);
cout << "order(" << order << ") -> sine(pi) = " << setprecision(15) << sine << endl;
}
return 0;
}
not sure what that odd sine(pi) was supposed to be doing
Apart the obvious syntax errors (the includes should be before your factorial header) in your code:
I see no templates in your code which your assignment clearly states to use
so I would expect template like:
<class T> T mysin(T x,int n=15){ ... }
using pow for generic datatype is not safe
because inbuild pow will use float or double instead of your generic type so you might expect rounding/casting problems or even unresolved function in case of incompatible type.
To remedy that you can rewrite the code to not use pow as its just consequent multiplication in loop so why computing pow again and again?
using factorial function is waste
you can compute it similar to pow in the same loop no need to compute the already computed multiplications again and again. Also not using template for your factorial makes the same problems as using pow
so putting all together using this formula:
along with templates and exchanging pow,factorial functions with consequent iteration I got this:
template <class T> T mysin(T x,int n=15)
{
int i;
T y=0; // result
T x2=x*x; // x^2
T xi=x; // x^i
T ii=1; // i!
if (n>0) for(i=1;;)
{
y+=xi/ii; xi*=x2; i++; ii*=i; i++; ii*=i; n--; if (!n) break;
y-=xi/ii; xi*=x2; i++; ii*=i; i++; ii*=i; n--; if (!n) break;
}
return y;
}
so factorial ii is multiplied by i+1 and i+2 every iteration and power xi is multiplied by x^2 every iteration ... the sign change is hard coded so for loop does 2 iterations per one run (that is the reason for the break;)
As you can see this does not use anything funny so you do not need any includes for this not even math ...
You might want to add x=fmod(x,6.283185307179586476925286766559) at the start of mysin in order to use more than just first period however in that case you have to ensure fmod implementation uses T or compatible type to it ... Also the 2*pi constant should be in target precision or higher
beware too big n will overflow both int and generic type T (so you might want to limit n based on used type somehow or just use it wisely).
Also note on 32bit floats you can not get better than 5 decimal places no matter what n is with this kind of computation.
Btw. there are faster and more accurate methods of computing goniometrics like Chebyshev and CORDIC
I need to find some way to deal with infinitesimial double values.
For example:
exp(-0.00000000000000000000000000000100000000000000000003)= 0.99999999999999999999999999999899999999999999999997
But exp function produce result = 1.000000000000000000000000000000
So my first thought was to make my own exp function. Unfortunately I am getting same output.
double my_exp(double x)
{
bool minus = x < 0;
x = abs(x);
double exp = (double)1 + x;
double temp = x;
for (int i = 2; i < 100000; i++)
{
temp *= x / (double)i;
exp = exp + temp;
}
return minus ? exp : (double)1 / exp;
}
I found that issue is when such small numbers like 1.00000000000000000003e-030 doesn't work well when we try to subtract it, neither both if we subtracting or adding such a small number the result always is equal to 1.
Have U any idea how to manage with this?
Try using std::expm1
Computes the e (Euler's number, 2.7182818) raised to the given power
arg, minus 1.0. This function is more accurate than the expression
std::exp(arg)-1.0 if arg is close to zero.
#include <iostream>
#include <cmath>
int main()
{
std::cout << "expm1(-0.00000000000000000000000000000100000000000000000003) = " << std::expm1(-0.00000000000000000000000000000100000000000000000003) << '\n';
}
Run the example in the below source by changing the arguments to your very small numbers.
Source: https://en.cppreference.com/w/cpp/numeric/math/expm1
I think the best way of dealing with such small numbers is to use existing libraries. You could try GMP starting with their example to calculate billions of digits of pi. Another library, MPFR which is based on GMP, seems to be a good choice. I don't know when to choose one over the other.
I'm trying to implement a function to calculate pi using the taylor series, here is my code to do that
#include <iostream>
#include <math.h>
using namespace std;
double pi(int n)
{
double sum = 1.0;
int sign = -1;
for (int i = 1; i < n; i++)
{
sum += sign / (2.0 * i + 1.0);
sign = -sign;
}
return 4.0 * sum;
}
int main()
{
cout << "the value for pi is" << pi ;
}
For some reason my code keeps returning 1, but I cant see why
as a side note I want to make it so the code doing the taylor series stops running if the absolute value of the latest term of the series is less than the error in the latest estimate for pi
I was thinking of doing this by using a similar for loop to calculate the error, and a do... while loop across the whole function that stops the calculation of pi once this condition is met, but I'm not sure if there's an easier way to do this or where to start.
I'm fairly new to this forum and c++, any help I can get is really appreciated
You should evaluate the function with some value, for example 10:
cout << "the value for pi is " << pi(2000)<<endl;
Output:
the value for pi is 3.14109
pi in your example is a function that takes an argument. Hence you have to add parenthesis and an argument like this pi(50). 50 is just an example use whatever you want.
In the latest Boost, there is a function to compute the Bernoulli number, but I miss what it does exactly.
For example, Mathematica, Python mpmath and www.bernoulli.org say that:
BernoulliB[1] == -1/2
but the boost version
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/math/special_functions/bernoulli.hpp>
boost::multiprecision::cpp_dec_float_100 x = bernoulli_b2n<boost::multiprecision::cpp_dec_float_100>(1);
returns 0.166667
Why this difference? Am I missing something?
All odd Bernoulli numbers are zero, apart of B1, which you know is -1/2. So,
boost::math::bernoulli_b2n returns the only even (2nth) Bernoulli numbers.
For example, to get B4 you need to actually pass 2:
std::cout
<< std::setprecision(std::numeric_limits<double>::digits10)
<< boost::math::bernoulli_b2n<double>(2) << std::endl;
and if you pass 1, you get B2.
See docs: http://www.boost.org/doc/libs/1_56_0/libs/math/doc/html/math_toolkit/number_series/bernoulli_numbers.html
Of course, you can make a simple wrapper, to imitate preferred syntax1:
double bernoulli(int n)
{
if (n == 1) return -1.0 / 2.0; // one
if (n % 2) return 0; // odd
return boost::math::bernoulli_b2n<double>(n / 2);
}
int main()
{
std::cout << std::setprecision(std::numeric_limits<double>::digits10);
for (int i = 0; i < 10; ++i)
{
std::cout << "B" << i << "\t" << bernoulli(i) << "\n";
}
}
or even a class with overloaded operator[] (for demanding persons ;) ):
class Bernoulli
{
public:
double operator[](int n)
{
return bernoulli(n);
}
};
or even make use of template magic and do all this checks at compile time (I will left it as an exercise for a reader ;) ).
1Please note, that this exact function body is not well verified and can contains mistakes. But I hope you've got the idea of how you can make a wrapper.
In the program below, I am trying to calculate the distance between two points. For this, I have made two Point objects. In the method that returns the distance, I have used the distance formula to calculate distance between two points in space. However, every time I run the program, I get a not a number value, which shouldn't be there. Please help.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int a, int b);
~Point();
double getDistance(Point& P2);
void setPoints(int a, int b);
int getX();
int getY();
private:
int x;
int y;
};
Point::Point(int a, int b)
{
setPoints(a,b);
}
Point::~Point()
{
//Nothing much to do
}
void Point::setPoints(int a, int b)
{
x = a;
y = b;
}
double Point::getDistance(Point& P2)
{
int xdiff = P2.getX()-this->getX();
int ydiff = P2.getY()-this->getY();
xdiff = xdiff*xdiff;
ydiff = ydiff*ydiff;
double retval = sqrt((xdiff) - (ydiff));
return retval;
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
int main(int argc, char* argv[])
{
Point P1(0,0);
Point P2(0,1);
Point& pr = P2;
cout<<P1.getDistance(pr)<<endl;
return 0;
}
Your formula is wrong. It's not
sqrt(xdiff - ydiff)
but
sqrt(xdiff + ydiff)
You're trying to get the sqrt(-1) which is indeed not a number (or not a real number).
Here's how to figure this sort of thing out for yourself, or at least get a lot closer to a good StackOverflow question:
You know the problem is in the sqrt() call. So, what is it being called with? In this case, you could trace through the computation manually:
int xdiff = P2.getX()-this->getX(); // this is 0 - 0, which is 0.
int ydiff = P2.getY()-this->getY(); // this is 1 - 0, which is 1.
xdiff = xdiff*xdiff; // this is still 0.
ydiff = ydiff*ydiff; // this is still 1.
double retval = sqrt((xdiff) - (ydiff)); // this is sqrt(0 - 1), or sqrt(-1).
Alternately, in more complicated cases -- and to check your work, you could either use a debugger to print out the values of the arguments, or you could insert print statements:
xdiff = xdiff*xdiff;
ydiff = ydiff*ydiff;
cout << 'xdiff: ' << xdiff << ' ydiff: ' << ydiff << endl
cout << 'computing sqrt(' << xdiff - ydiff << ')' << endl
double retval = sqrt((xdiff) - (ydiff));
Either way, you now know that you're computing sqrt(-1), and you can try running that directly to confirm that it does indeed produce the same result. So either you have a question of "Why is sqrt(-1) returning NaN?" or a question of "Why is my distance calculation trying to compute the square root of a negative number?"
Hopefully you already know the answer to the first question, and the second question should indicate that you need to double-check your distance formula, which should have showed you the answer pretty quickly -- but even if you can't figure out why it's doing that, it at least makes a more useful question to ask here.
You should be adding here, not subtracting:
double retval = sqrt((xdiff) - (ydiff)); // correct is +
Subtracting causes you to take the square root of -1 due to the input data, which is not a (real) number.
As craigmj said, the formula for distance is sqrt ((x1-x2) + (y1-y2)). It's addition not subtraction. What your doing is generating an imaginary number (sqrt (-1)) which will cause an error.
Just a tip of advice, but do not create the destructor if it doesn't do anything; a destructor will be provided for you. Adding a destructor that doesn't do anything just adds unneeded code and makes it look messier.
Also in the getDistance function, you do not need to use this ->getX() and this-> getY(). Since this is a member function it has access to private data, therefore you can directly access the variables through x and y.