Quick! Is there a Mathematician in the house?!

I’ve always wanted to say that…

This is slightly off topic, but still part of my Java project work, so I thought there might be someone here that can help me.

I’m working on a neural network project, and what i need to do is take the following sin wave (in black) and create the red line, which is the ideal output of the network.

http://sphotos.ak.fbcdn.net/hphotos-ak-snc4/hs231.snc4/38913_417848026716_555931716_5326664_2482414_n.jpg

Currently I’ve been doing this manually in Excel, because i have no idea how to calculate this. Basically, when the angle is at the 90 degree position, the output needs to be 1, and at the 270 degree position, it needs to be -1, and anywhere else it needs to be as close to zero as possible.

I probably wouldn’t be the first to say I’m terrible with Trig’ :smiley:

Thanks all,
Matt

cant you just write a function that does exactly what you described ?


/**
* theta given in radians
*/
function myFunc(double theta)
{

double sin = Math.sin(theta);

if (sin==1 || sin==-1) return sin;

return 0 ; 
}

I have the felling I don’t completely understand what you want, but anyway …

Untested:


final float range = 15.0f;

float degrees = ...; // anything
degrees %= 360; // -359..359
degrees += 360; // 1..719
degrees %= 360; // 0..359

if(isBetween(degrees,  90-range,  90+range))  return (range-Math.abs( 90-degrees)) / +range;
if(isBetween(degrees, 270-range, 270+range))  return (range-Math.abs(270-degrees)) / -range;
return 0.0f;

Alternatively, if you don’t need the linear ramp:


double highNumber = 500; // seems to give good results
double oddNumber = highNumber | 1; // otherwise you might end up without negative values
Math.pow(Math.sin(angle), oddNumber);

I think this is what I need.

I actually need to do this bit of code as an Excel formula rather than java code, to them save out as a CSV file and use with my program. I can sort that out though, its just the math that gets me :slight_smile:

Thanks guys!

EDIT:

double oddNumber = highNumber | 1; // otherwise you might end up without negative values

what does this line actually do? I’ve not come across inclusive OR before. (and I’m not getting any negative values)

EDITEDIT:

http://support.microsoft.com/kb/q132686/ Microsoft has no need for your primitive BODMAS! Blah! :wink:

It will make sure the last bit is set.

ex.
101011011000 is OR’d with 0001 so it becomes 101011011001.

01101101 remains 01101101 because the last bit is already set.

[quote]what does this line actually do? I’ve not come across inclusive OR before. (and I’m not getting any negative values)
[/quote]
it return an odd number when input is even otherwise it return the same number

1 => 1
2 => 3
3 => 3
4 => 5
etc…

Ahh, cool, that’s actually a really useful thing to know :smiley:
Cheers.

I did want to go down the Excel rout with this (for the graphing functions), but I may just write a program that dumps out the CSV’s instead…

THis is getting further off topic, but has anyone found a really simple graphing package for java and swing? I cant remember which ones i’ve tried, but I’ve never found one that works, or is far too complicated and finiky to use for me to bother trying :wink:

http://www.jfree.org/jfreechart/

I think this one is pretty straightforward, and it is very popular (i.e. there are a lot of faqs and tutorials).

Hay, I got it working! Now I can get back to actual real programming ;D

I feel really stupid asking this, but can anyone explain to me why raising to a power of an even number always returns a positive, and raising to the power of a odd number will do both positive and negative?

thanks for the graphing tip, I’m giving jFreeChart a go.

As you might remember from highschool: +2*+2=+4 -2*+2=-4 +2*-2=-4 -2*-2=+4

In general: [positive] * [positive] = [positive] [positive] * [negative] = [negative] [negative] * [negative] = [positive]

Let’s write it out: -2^1 = -2 = -2 -2^2 = -2*-2 = +4 -2^3 = +4*-2 = -8 -2^4 = -8*-2 = +16 -2^5 = +16*-2 = -32 -2^6 = -32*-2 = +64

You can see that raising -2 to an odd number, the result is negative, and with an even number, the result is positive.