step size in c++ simulation

Dear all,

I have a simple question regarding the step size in c++ simulation.

For example, if I want to calculate a sine wave in a given interval, then I can use for (int k = 0; k < 10; ++k) to compute 10 points. But if I want to compute 100 points in the same interval, simply change to for (int k = 0; k < 100; ++k) will actually change the frequency (10 times than original). What I want is to "sample" more points in the same interval so that the results will be more accurate. How can I do that?

Thank you.
The value of the for loop index (k in your case) could be scaled?

Andy

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
#include <iostream>
#include <iomanip>
#include <cmath>

const double pi = 3.141592654;

inline double deg2rad(double deg) {
    return ((deg * pi) / 180.0);
}

int main() {
    std::cout << std::fixed << std::setprecision(4);

    int increments = 10;

    double x_min = 76.0;
    double x_max = 77.0;

    double x_range = x_max - x_min;
    double dx      = x_range / increments;

    for(int i = 0; i <= increments; ++i) {
        double x = x_min + i * dx;
        std::cout << x << "  " << sin(deg2rad(x)) << "\n";
    }
    
    return 0;
}


76.0000  0.9703
76.1000  0.9707
76.2000  0.9711
76.3000  0.9715
76.4000  0.9720
76.5000  0.9724
76.6000  0.9728
76.7000  0.9732
76.8000  0.9736
76.9000  0.9740
77.0000  0.9744

1
2
3
4
5
double step = 0.1;
for(double i = 0; i < 10; i += step)
{
    //compute sine
}


Change step to suit you.
I was 'brought up' to avoid using non-integral types in for-loops. Prob pendantic, but I'd code that as:

1
2
3
4
5
double step = 0.1;
for(int i = 0; i < 100; ++i)
{
    //compute sine using i * step
}


Using < it's no doubt safe, but with <=, see

For-loop in C++ using double breaking out one step early, boundary value not reached
http://stackoverflow.com/questions/1286394/for-loop-in-c-using-double-breaking-out-one-step-early-boundary-value-not-rea

Andy
Last edited on
@Andy, good point.
Thank you guys for the reply.

I understand the solution you've mentioned. Basically we increase the step size to increase the accuracy, like change sin(0.1) to sin(0.01), sin(0.02), ... to sample more points.

However my case is a little different. I am doing a control simulation, so the formula is like:

1
2
3
4
for (size_t k = 0; k < FINAL_STEP; ++k) {
    u[k] = K*x[k];
    x[k+1] = A*x[k] + B*u[k];
}

where K, A, B are constant matrix.

If I want to simulate 10 seconds, and sampling is 0.1s, then FINAL_STEP will be 100. Now I want to simulate still in 10 seconds, but more "smoothly", so I change sampling to 0.01s.
What I want to see is a more smooth curve. But if change FINAL_STEP to 1000, then the results will be "condensed". In other words, x, u will reach the same results in only 1 second, not 10 seconds with more smoothness.

Since the system is already discretized, the index k is an integer instead of float/double. Then how can I smoothen my simulation?

Again, thanks a lot for the suggestions.
Last edited on
Is it not 10s sampling at 0.01 from 0 to 1000?
I dont understand how this holds:
In other words, x, u will reach the same results in only 1 second,

You need to alter one or more of your coefficients -- K, A, and B They must include some sort of rate of change.

Andy
Hi shadowCODE,

What I mean is that, still take a sine wave for example, in 2*pi seconds, the original result is only one period. But if I increase my FINAL_STEP to 10 times of its previous value, then in 2*pi seconds, the result will be 10 period. It only increases the frequency. What I want is that in 2*pi seconds, there's still only one period of sine wave, but more smoothly, namely we sampled 10 times more points.

Maybe taking sine wave is not quite appropriate, since sin() can take a double as inputs. In my case, I do not have any function that takes an argument. Only the formula is listed as
1
2
3
4
for (size_t k = 0; k < FINAL_STEP; ++k) {
    u[k] = K*x[k];
    x[k+1] = A*x[k] + B*u[k];
}


I hope my explanation makes sense.
Andy,

A, B, K are all constant matrix and not supposed to change. How can I alter them with rate of change?
I think I should have said sample rate rather than rate of change.

Topic archived. No new replies allowed.