[Math] How to fit a sinusoidal function through 2 points with known slopes

mathematicaregressionsystems of equations

I can define my sinusoidal function as $y(x) = A\sin(B x+c) + D$
or as $y(x) = A \sin(B x) + C \cos(B x) + D$

Now, I have two points with known slopes that I must fit this sine wave to, thus my constraints are:

  • $y(0) = y_0$
  • $y'(0) = y_0'$
  • $y(w) = y_w$
  • $y'(w) = y_w'$

    where $w$ is the x coordinate of the second known point.

These give rise to the following system of equations (using $y(x) = A\sin(B x) + C\cos(B x) + D$):

  • $y_0 = A \sin(B*0) + C \cos(B*0) + D$, simplifiable to $y_0 = C+D$
  • $y_0' = A B \cos(B*0) – C B \sin(B*0)$, simplifiable to $y_0' = A B$
  • $y_w = A \sin(B w) + C \cos(B w) + D$
  • $y_w' = A B \cos(B w) – C B \sin(B w)$

Using Mathematica, I tried to solve the equation for A, B, C and D (using y0p and ywp as symbols for $y_0'$ and $y_w'$ respectively):

Solve[y0 == C+D && y0p == A*B && yw == A*Sin[B*w] + C*Cos[B*w] + D && ywp == A*B*Cos[B*w] - C*B*sin[B*w], {A,B,C,D}]

But I get the message: Solve::nsmet: This system cannot be solved with the methods available to Solve.

Maybe Mathematica can solve the equation if I represent y as $y(x) = A \sin(B x+c) + D$ ?

  • $y_0 = A \sin(B*0+c) + D = A \sin(c) + D$
  • $y_0' = A B \cos(B*0+c) = A B \cos(c)$
  • $y_w = A \sin(B w+c) + D$
  • $y_w' = A B \cos(B w+c)$

Thus the command is:

Solve[y0 == A*Sin[c] + D && y0p == A*B*Cos[c] && yw == A*Sin[B*w+c] + D && ywp == A*B*Cos[B*w+c], {A,B,c,D}]

But this just gives the same message.

I've tried solving it by hand a few times, but I just can't figure it out. So that's why I've come here. How can I solve the sinusoidal function, as defined above, that passes through two points with known slopes?


As an example, if $w=1$, $y_0=0$, $y_w=0.1$, $y_0'=2.8$, $y_w'=-0.5$, then the resulting function (by numerical approximation) is $y(x) = 0.407975 \sin(6.863160 x) + 0.755679 \cos(6.863160 x) – 0.755679$

enter image description here

We see that indeed, $y(0)=0$, $y(1)=0.1$, $y'(0)=2.8$, and $y'(1)=-0.5$.

Note that there are multiple solutions by varying B. For example, $y(x)=0.205328 \sin(13.636737 x) + 0.154076 \cos(13.636737 x) – 0.154076$ also solves the above constraints. However, I only need the one with longest period, which means solving for the smallest non-negative value of $B$ possible.

Best Answer

Beside all the answers and comments done before this answer, let me clarify some points. You set for equations for four unknowns in the case where

$y(x) = A \sin(B x) + C \cos(B x) + D$

You can eliminate D from your first condition, A from the second condition and C from the fourth condition. Then, you are left with a single equation in B which can simply be reduced to

Tan[z] = K z, with z = B w / 2 and K = 2 (y(w) - y(0)) / [w (y'(w) + y'(0))].

In the particular case you consider, you then have K = 2/23 and z = B / 2. The last equation has an infinite number of solutions; if looking only at positive values of B, we find successively 6.8632, 13.6367, 20.2956, 26.8580, 33.3500, 39.7925 (and so on) which are almost successive multiples of the first value. Corresponding negative values are also to take into account.

For sure, the remaining problem is that all other coefficients A, B, C and D will depend on the value you will select for coefficient B.