trying to plot linear equation

Started by
2 comments, last by jefferytitan 11 years, 8 months ago
I believe this code is generating random data points and plotting based on a linear equation, but I'm not sure how to solve this equation manually to check how points are lining up.

Can anyone point me to something so I can understand how to plot points from this equation?


x = ((double)rand()/(double)RAND_MAX) * 2.0 - 1.0;
y = ((double)rand()/(double)RAND_MAX) * 2.0 - 1.0;

y1 = (-5 * x - 2) / -4;
Advertisement
Note that this code generates co-ordinates for a single point (or two points, depending upon whether you use y, y1, or both). But if you run it in a loop you'd get a more interesting graph.

It looks like the rand()/RAND_MAX expressions is meant to generate doubles in the range 0-1. If you call that number s in the x expression, and t in the y expression, it goes as below:

x = 2s - 1
y = 2t - 1

If you plot x vs y you'll get random points within the square from x=-1 to 1 and y=-1 to 1.

y1 on the other hand completely ignores y. You'd get random points along the line y1=1.25x + 0.5, with the range x=-1 to 1.

Plotting it by hand is very easy. Because it is just a line, all you need is two points to draw it. Substitute x=-1 into the equation to get a value for y1 at the start of the line. Substitute x=1 into the equation to get a value for y1 at the end of the line. Draw a line between those two points.

Plotting it on a computer... well, what programming languages do you know / have available?
You are correct, it is in a loop and generates random points from -1 to 1, and then this linear function is used to "categorize" the data.

To answer the "y1 ignore", I should have put this after the line "y1 = " (which I think tells me what side of the line the point is on).


if (y < y1)
output = 1;
else
output = -1;


I substitued x = -1 and x = 1 and got these results, but I don't think I'm doing this correctly.

y1 = (-5 * x - 2) / -4
y1 = (-5 * -1 - 2) / 4
y1 = (5 -2) / -4
y1 = 3/-4

y1 = (-5 * x - 2) / -4
y1 = (-5 * 1 -2) / -4
y1 = (-5 -2) / -4
y1 = -7/-4


I'm trying to learn simple neural nets. I'm just not good at math but I can see I have to start somewheres.
Okay, your solutions for y1 look correct, although it is traditional to divide both numerator and denominator by -1 if the denominator ends up negative, e.g. -3/4 and 7/4. So if you draw a line from (-1, -0.75) to (1, 1.75), that's your line. Anything below that line with have output 1, anything above it will have output -1. Whether the line is correct... I have no idea where that equation came from. It may be completely correct that the end point is outside the square, and you just ignore anything that doesn't intersect with the square.

This topic is closed to new replies.

Advertisement