-5

What is the fastest way of calculation x in below equation in c++?

sin (a x) + b sin (c x)== d * x + e

I don't need a very exact value of x. the approximation with 0.001 is acceptable. I also know an interwal for the solution [x_0,x_1].

I know Newton method but as I am going to simulate a system and I need to solve if thousand of times, I don't know how to give the first solution

  • Are you asking us to do your algebra h/w? Well.... no :-) – George Nov 03 '17 at 07:29
  • No, it is not my homework. I'm simulating a system and I need a solution for a more general form of above equation. I didn't find any useful thing by searching, so I asked it here. @George – Oliver Range Nov 03 '17 at 07:32
  • You might want to try and solve the equation for x, then you'll have a simple expression that doesn't depend on x. – Rakete1111 Nov 03 '17 at 08:46

2 Answers2

0

I assume you are looking for a numerical solution, given the known parameters a,b,c,d,e. An approximate solution can be found by this dirty iteration. But it is not warrantied to converge for all values of the parameters. The only way to do that is to give an analytic upper bound and lower bound for the solution and iterate with bisection root finding.

#include<numeric>
#include<iostream>
#include<cmath>

using std::cout;

int main(){
    auto a = 1.1;
    auto b = 1.2;
    auto c = 0.9;
    auto d = 0.1;
    auto e = 0.1;

    auto N = 1000;

    auto x = 0.;
    for(int n = 0; n != N; ++n)
        x = 0.999*x + 0.001*(sin(a*x) + b*sin(c*x) - e)/d;

    cout << sin(a*x) + b*sin(c*x) << " == " << d*x + e << '\n';
    cout << "solution is x = " << x << '\n';
}

(for simplicity this is C++11)

alfC
  • 14,261
  • 4
  • 67
  • 118
0

You can reformulate your equation as

sin (a x) + b sin (c x) - d * x - e == 0

Now, this is a root finding problem. Here is a list for root finding algorithms.

Newton's method is very fast and easy to implement, since the derivative of your equation can be calculated analytically.

#include <array>
#include <iostream>
#include <cmath>

template <typename T> double func(const T &parameter, const double x) {
  const auto a = parameter[0];
  const auto b = parameter[1];
  const auto c = parameter[2];
  const auto d = parameter[3];
  const auto e = parameter[4];
  return sin(a * x) + b * sin(c * x) - (d * x + e);
}

template <typename T> double derivative(const T &parameter, const double x) {
  const auto a = parameter[0];
  const auto b = parameter[1];
  const auto c = parameter[2];
  const auto d = parameter[3];

  return a * cos(a * x) + b * c * cos(c * x) - d;
}

template <typename T> double solve(const T &parameter) {
  double x = 0.0;
  const double eps = 1e-9;
  while (fabs(func(parameter, x)) > eps) {
    x = x - func(parameter, x) / derivative(parameter, x);
  }
  return x;
}

int main() {
  const std::array<double, 5> parameter{1.1, 1.2, 0.9, 0.1, 0.1};
  const auto x = solve(parameter);
  std::cout << "solution is x=" << x << " f(x)=" << func(parameter, x) << '\n';
}

Go from double to float to speed it up, if your desired accuracy allows that.

schorsch312
  • 5,553
  • 5
  • 28
  • 57