How to compute a definite Integral in C++? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Needs more focus Update the question so it focuses on one problem only by editing this post.
Improve this question
How can you define a function to calculate the value of a definite integral in C++? For example to solve the integral of the function x^2 * cos(x)?

Interestingly enough, I ran across this article a little while ago explaining one method for calculating numerical integrals using function pointers.
https://helloacm.com/c-function-to-compute-numerical-integral-using-function-pointers/
For something like x^2 * cos(x):
You would need an overloaded integral function:
double integral(double(*f)(double x), double(*g)(double x, double y), double a, double b, int n)
{
double step = (b - a)/n; // width of rectangle
double area = 0.0;
double y = 0; // height of rectangle
for(int i = 0; i < n; ++i)
{
y = f(a + (i + 0.5) * step) * g(a + (i + 0.5) * step, y);
area += y * step // find the area of the rectangle and add it to the previous area. Effectively summing up the area under the curve.
}
return area;
}
To call:
int main()
{
int x = 3;
int low_end = 0;
int high_end = 2 * M_PI;
int steps = 100;
cout << integral(std::powf, std::cosf, low_end, high_end, steps);
return 0;
}

Related

How to write a log base 10 function in c++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know that it may seem a duplicate question, but I could not find my answer in previous questions.
I mean how to write a log base 10 function by simple loops and not using built in log function in c++.
The easiest way is to calculate the natural logarithm (ln) with a Taylor series. Once you have found the natural logarithm, just divide it by ln(10) and you get the base-10 log.
The Taylor series is quite simple to implement in C. If z is the number for which you are seeking the log, you just have to loop a few iterations multiplying an accumulator by (z-1) each time. To a limit, the more iterations you run, the more accurate your result will be. Check it a few times against the libC log10() version until you are happy with the precision.
This is a "numeric approach". There are other numeric solutions to finding the logarithm of a number which can give more accurate results. Some of them can be found in that Wikipedia link I gave you.
Assuming by "log base 10" you mean "the number of times n can be divided by 10 before resulting in a value < 10":
log = 0;
// Assume n has initial value N
while ( n >= 10 ) {
// Invariant: N = n * 10^log
n /= 10;
log += 1;
}
You'll get faster convergence with Newton's Method. Use something like this (hand written not compiled or tested uses f(r) = 2**r - x to compute log2(x) ):
double next(double r, double x) {
static double one_over_ln2 = 1.4426950408889634;
return r - one_over_ln2 * (1 - x / (1 << static_cast<int>(r)));
double log2(double x) {
static double epsilon = 0.000000001; // change this to change accuracy
double r = x / 2;. // better first guesses converge faster
double r2 = next(r, x);
double delta = r - r2;
while (delta * delta > epsilon) {
r = r2;
r2 = next(r, x);
delta = r - r2
}
return r2;
}
double log10(double x) {
static double log2_10 = log2(10);
return log2(x) / log2_10;
}

How to solve this ordinary differential equation using C++ Boost odeint library [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Using boost c++ odeint library, how to solve the following ordinary differential equations of motion,
z'' = -n²·z.
The analytical solution of the aforementioned ODE is
z(t) = (z0'/n)·sin(n·t) + z0·cos(n·t).
Solution attempt as per comment (Jan 17 at 23:01) by the OP in answer
My program is:
void ode( const state_type &z , state_type &dzdt , double t ) {
dzdt[0] = z[1];
dzdt[1] = -1 * z[0] * w * w;
}
void write_ode( const state_type &z , const double t ) {
cout << t << '\t' << z[0] << '\t' << z[1] << endl;
}
int main { ...
integrate( ode , z , t , 1000 , 0.1 , write_ode );
}
But the integrate function return only the values of z0, and z0'. I need to find the values of z(t).
You can use the standard way of transforming an N th-order differential equation of a first order ODE of dimension N. In your case this is:
x = z
y = dz/dt
dx/dt = y
dy/dt = n^2 x
You can easily put this into any solver of odeint. But, your ODE seems to be Hamiltonian - you might also think about a symplectic solver. They conserve the phase space volume and the energy "only" oscillates about the initial energy. Odeint has implemented two symplectic solvers.

How to find the centroid of an object in the point cloud? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anyone tell me how to find the centroid of an object in point cloud?.
I haven't tried any code yet because I have no slight idea as to how to go about it.
If you have the point locations, you should be able to just average the x and y positions.
int x = 0; y = 0;
for ( i = 0; i < num_pts; i++ )
{
x += pt[i].x;
y += pt[i].y;
}
centroid.x = x / num_pts;
centroid.y = y / num_pts;

How to print R, G and B matrices [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having the following code and the number of my channels are 3
IplImage* img_crop_mat = cvLoadImage("....", 1);
...
int b = 0;
uchar* rgb = (uchar*) img_crop_mat->imageData;
I would like to have R, G and B matrices in a loop, skimming the entire image:
for (int y = b; y < height - b; y++)
{
???
for (int x = b; x < width - b; x++)
{
????
}
}
The previous forums regarding my question deal with CvMat but not with pointers as my code.
What are the indexes that I must take into account?
You can use the following macro to access an arbitrary pixel of a 3-channel, 8U-image:
CV_IMAGE_ELEM(myImage, unsigned char, y, x*3 + ChannelOfInterest)
This is an lvalue so you can take and use its value, or you can change the pixel's value.
By default,
ChannelOfInterest = 0, blue
ChannelOfInterest = 1, green
ChannelOfInterest = 2, red
The actual data structure is pretty straightforward, look up the definition of CV_IMAGE_ELEM.

gaussian filter kernel values [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
can anyone tell me how to generate a 2d gaussian filter kernel using the gaussian filter equation? how does the x and y value vary?
ref: http://en.wikipedia.org/wiki/Gaussian_function
To generate the kernel is quite simple. If your problem is in applying the kernel, you need to update the question.
The kernel is simply a square matrix of values, generally an odd number size so that there's a clearly defined center. To fill it, the x and y values go from -(n-1)/2 to (n-1)/2 where n is the size of the matrix.
double half_n = (n - 1) / 2.0;
for (i = 0; i < n; ++i)
{
double x = i - half_n;
for (j = 0; j < n; ++j)
{
double y = j - half_n;
kernel[i][j] = // use formula with x and y here
}
}