Re: Finding a sine wave

*To*: mathgroup at smc.vnet.net*Subject*: [mg95512] Re: Finding a sine wave*From*: dh <dh at metrohm.com>*Date*: Tue, 20 Jan 2009 05:48:56 -0500 (EST)*References*: <gkppq1$dmn$1@smc.vnet.net>

Hi Hug, I think a direct method is much simpler than numerical iterations. Your model is: c0 Sin[c1 t+c2] You have 3 data points for 3 times. Therefore we can get rid of c0 by division e.g. the first 2 data points by the last. This leaves 2 data points and the model: Sin[c1 t+c2]. The data values must be between -1..1, this shows that there is a constrain on the original values. Now we take the ArcSin of the data values, this gives a model: c1 t + c2. Now the fit is trivial. c0 can be obtained from the original data values. Here is an example: d0 = {0.45, 0.86}; t = {0.5, 1.2} d = ArcSin /@ d0; cs = {c1, c2} /. Solve[{c1 t[[1]] + c2 == d[[1]], c1 t[[2]] + c2 == d[[2]]}, {c1, c2}][[1]]; Plot[Sin[cs.{t, 1}], {t, 0, 2 Pi/cs[[1]]}, Prolog -> {Red, Point[Transpose[{t, d0}]]}] hope this helps, Daniel Hugh Goyder wrote: > An experiment will give me three coordinates which lie on a sine wave. > I have to find the sine wave efficiently. There are three unknowns the > sine wave amplitude, A, the wavelength of the sine wave, L, and the > phase, ph. I also know that the wavelength is larger than the interval > containing my measurement points. I think this condition removes > possible multiple solutions. > > Below I give two methods which need improving. The first method uses > FindRoot. This methods works about 60% of the time. I give six > examples where it fails. The failure may be due to poor initial > guesses. In the second method I use FindFit. This is not quite the > correct method because I have an equal number of equations and > unknowns. Some of the failures here, I think, are due to there being > no error for the algorithm to work with. I give examples of failures. > I have also tried FindInstance, Reduce and NSolve but I don't think > these are appropriate. > > Here are some questions > > 1. I would really like a symbolic solution rather than an iterative > one. Is such a solution possible? > 2. Can anyone improve on the methods below to make them more robust? > 3. I have some control over my x-locations. How can I work out best x > locations given an estimate of the wavelength L? > > Many thanks for all answers. > > > xx = Sort[Join[{0}, RandomReal[{0, 10}, 2]]] > > yy = RandomReal[{-25, 25}, 3] > > sol = FindRoot[{yy[[1]] - A*Sin[ph], > yy[[2]] - A*Sin[2*Pi*(xx[[2]]/L) + ph], > yy[[3]] - A*Sin[2*Pi*(xx[[3]]/L) + ph]}, {{A, > Max[Abs[yy]]}, {L, Max[xx]}, {ph, 0}}] > > Plot[Evaluate[A*Sin[2*Pi*(x/L) + ph] /. sol], {x, 0, 10}, > Epilog -> {PointSize[0.02], (Point[#1] & ) /@ > Transpose[{xx, yy}]}, PlotRange -> {{0, 10}, {-25, 25}}] > > FindRootFailures = {{{0, 3.781264462608982, > 3.797055100562352}, {-22.948348087068737, 1.4581744078038472, > -14.242676740704574}}, {{0, 4.424069570670131, > 8.861716396743098}, {-6.444538773775843, -10.787309362608688, > 15.579334094330942}}, {{0, 4.424069570670131, > 8.861716396743098}, {-5.944536471563289, 10.627536107497393, > -21.294232497202316}}, {{0, 1.3680556047878967, > 8.546115267250002}, {4.128528863849845, 2.9017293848933923, > -22.51610539815371}}, {{0, 1.3738869718371616, > 5.689309423462079}, {20.553993773523437, -16.972841620064592, > 16.61185061676568}}, {{0, 1.0408828831133632, > 8.484645515699821}, {-3.7267589861478045, 4.1016610850387, > -24.600635061804443}}}; > > (({xx, yy} = #1; > sol = FindRoot[{yy[[1]] - A*Sin[ph], > yy[[2]] - A*Sin[2*Pi*(xx[[2]]/L) + ph], > > yy[[3]] - A*Sin[2*Pi*(xx[[3]]/L) + ph]}, {{A, > Max[Abs[yy]]}, {L, Max[xx]}, {ph, 0}}]; > Plot[Evaluate[A*Sin[2*Pi*(x/L) + ph] /. sol], {x, 0, 10}, > > Epilog -> {PointSize[0.02], (Point[#1] & ) /@ > Transpose[{xx, yy}]}, > PlotRange -> {{0, 10}, {-25, 25}}]) & ) /@ > FindRootFailures > > xx = Sort[Join[{0}, RandomReal[{0, 10}, 2]]] > > yy = RandomReal[{-25, 25}, 3] > > sol = FindFit[ > Transpose[{xx, yy}], {A*Sin[2*Pi*(x/L) + ph], > Max[Abs[xx]] < L}, {A, L, ph}, x] > > Plot[Evaluate[A*Sin[2*Pi*(x/L) + ph] /. sol], {x, 0, 10}, > Epilog -> {PointSize[0.02], (Point[#1] & ) /@ > Transpose[{xx, yy}]}, PlotRange -> {{0, 10}, {-25, 25}}] > > FindFitFailures = {{{0, 2.463263668380835, > 4.3190892163093615}, {-1.8407827676541144, -8.736574079785198, > 12.661520984622932}}, {{0, 3.894521446091823, > 9.937403619870642}, {12.381369822165155, 15.840399165432128, > -6.914634137727327}}, {{0, 8.087369725271945, > 8.343899312282815}, {24.73795103895976, -13.396248713970305, > 7.150470311065216}}, {{0, 1.5480031866178834, > 9.575255260205617}, {-8.163720246784278, 13.373468882892958, > -18.018462091502098}}, {{0, 0.6152784485896601, > 0.818296772602134}, {-1.858698140836046, 3.695113783904491, > -1.3186989232026658}}, {{0, 4.743093154057316, > 6.028314583406327}, {-16.60893277597446, -17.413392198343093, > -18.54081837965986}}}; > > (({xx, yy} = #1; > sol = FindFit[ > Transpose[{xx, yy}], {A*Sin[2*Pi*(x/L) + ph], > Max[Abs[xx]] < L}, {A, L, ph}, x]; > Plot[Evaluate[A*Sin[2*Pi*(x/L) + ph] /. sol], {x, 0, 10}, > > Epilog -> {PointSize[0.02], (Point[#1] & ) /@ > Transpose[{xx, yy}]}, > PlotRange -> {{0, 10}, {-25, 25}}]) & ) /@ > FindFitFailures > > > >