How to determine a point on a sine wave

M

MathNewbie

Hi,

I'm trying to do get my head around some, probably basic, math
calculations. I checked some .net math libs, but the only thing I learn
from studying those is humility ;-).

Anyway, i have an array of points on a basic 2 dimensional axis. They
are all points of a sine shaped line. (not a perfect sine though, as
it's actually tide movement data that is roughly looking as a sine).

I would like to calculate the y dimension for a point with a known x value.

Can anyone help me solve this issue or give a pointer in the right
direction by providing some of the math slang for this problem?

Regards,

MathNewbie
 
S

ssamuel

MathNewbie,

If I understand your question, you're trying to find the y value of a
point on a sinusoidal curve at a given x. That's simple: y = sin x. Use
Math.Sin().

If you know the y value already and you're trying to find the x value,
you need an arcsin: x = arcsin y. Use Math.Asin(). Bear in mind that
sinusiods are repeating, so you're going to get the first x value at
which the y value exists. Add 2*pi to get the second, 4*pi to get the
third, and so on.

I should note that everything from Math.[any trig function] will give
you radians, not degrees. If you're working with a high school
education, the information that 2*pi radians = 360 degrees will bridge
the gap.

HTH!

Stephan
 
P

Peter Duniho

MathNewbie said:
[...]
Anyway, i have an array of points on a basic 2 dimensional axis. They are
all points of a sine shaped line. (not a perfect sine though, as it's
actually tide movement data that is roughly looking as a sine).

I would like to calculate the y dimension for a point with a known x
value.

If the points are sorted on the x coordinate, this is not hard at all. For
x where x is actually in your data set, it's simply a matter of looping
through the array to find x. With a sorted array, you can use a binary
search to more quickly find the point of interest.

If the x value does not actually correspond to one of the points within the
array, then you'll have to interpolate the value from one of the known
values. You'll still have to search, but rather than finding the exact
match, you'll find a pair of points in the array, between which that x value
would be found. Once you've done that, you apply the ratio of the desired x
to the interval between the x values of the two known points, to the
interval between the y values of the two known points, to find the desired y
value.

For example:

Suppose you have the set describing f(x) as { {0, 0}, {2, 3}, {4, 5}, {6,
8} }
(yes, I'm aware that's not a sine wave :) )

Suppose also that you want y = f(x) where x is 3. Searching through your
array, you fail to find an entry where x is 3, but you do find the entries
{2, 3} and {4, 5}. Call these {x0, y0} and {x1, y1}. Then...

y = y0 + (y1 - y0) * (x - x0) / (x1 - x0);

In the specific example:

y = 3 + (5 - 3) * (3 - 2) / (4 - 2)
= 3 + 2 * 1 / 2 = 4;
Can anyone help me solve this issue or give a pointer in the right
direction by providing some of the math slang for this problem?

"Interpolation" is the math buzz word that applies to your question. But
hopefully the above gives you the details you need.

Note that x needs to be within the range of known x values (or you have to
"extrapolate" off each end), and this does not guarantee a perfect match to
the curve that may represent your data. This is a linear interpolation, so
between each known point, the interpolated points fall on a straight line.
Since the actual data is curved, this means that the calculated value will
deviate from the correct value by some (usually small) amount. The error is
greatest when the desired value is farthest from either end of the
interpolation range.

If you want more accurate results, you will have to use more complex forms
of interpolation. Another common method is "polynomial approximation",
where you solve a large matrix of equations (one for each data point you
have) to establish constants and exponents to whatever degree you want. Yet
another method would involve fitting a sin function to subsets of your data,
allowing you to use an interpolated sin approximation of the function.

Of course, in all cases you are not guaranteed to have an exactly correct
answer. Any time you are filling in data that is absent from actual
empirical recordings, you have no way to know for sure that you have
interpolated the correct data. All you can say is that you have
interpolated a relatively useful simile of the correct data.

Pete
 
B

Ben Newsam

Hi,

I'm trying to do get my head around some, probably basic, math
calculations. I checked some .net math libs, but the only thing I learn
from studying those is humility ;-).

Anyway, i have an array of points on a basic 2 dimensional axis. They
are all points of a sine shaped line. (not a perfect sine though, as
it's actually tide movement data that is roughly looking as a sine).

I would like to calculate the y dimension for a point with a known x value.

Can anyone help me solve this issue or give a pointer in the right
direction by providing some of the math slang for this problem?

If I understand correctly, you want to be able to extrapolate the Y
position on a graph, based on a notional X position between two
existing X points. Assuming that the "dots" on the graph are joined by
straight lines, you can use something like the following (apologies
for the rather silly variable names):

public static double Extrapolate_Linear(double Value, double InMin,
double InMax, double OutMin, double OutMax)
{
double InRange, OutRange;
double Val;
double Result;

InRange = InMax - InMin;
OutRange = OutMax - OutMin;
Val = Value - InMin;

Result = Val / InRange * OutRange + OutMin;

return (Result);
}

So, for instance, if you have two XY points (4,10) and (7,15), and you
wanted to know the Y value corresponding to an X value of 5, calling

Whatever = Extrapolate_Linear (5, 4, 7, 10, 15);

would give you 11.667
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top