Jump to content
  • Advertisement
Sign in to follow this  
L. Spiro

Learning Calculus Online

This topic is 1157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to easily be able to read equations and turn them into code, and am looking for the best places to learn online.  I skipped 2 years in math in school, but when I reached Algebra 2 I got bored of school and stopped doing all homework in all classes (which led to me dropping out).
Prior to that I had taught myself a large amount of Trigonometry, so mainly these days I only struggle with Calculus, which is mostly a mystery to me.
 
I am looking at this example code here:
http://math60082.blogspot.jp/2013/02/question-write-function-to-calculate-nx.html
I get that integration is just the sum of the values from the start to the end in slices, each slice put through whatever equation is right of the integration bar, but I could never write the code to do that on my own, at least not in this example.  Here are example of things that confound me:
 
His first lines of code are:
// add in the first few terms
sum = sum + sin(a) + 4.*sin(a+h);
// and the last one
sum = sum + sin(b);
I know from some graphics equations that that squiggly bar with a and b has something to do with angles or hemispheres, so I guess the sin()’s came from that.  I still need proper teaching so I could have come up with this on my own.
 
Then there is h.  What is this magical number?
I can see from his code:
h=(b-a)/N;
Okay, it’s the range divided by the number of slices in our integration (the contribution of each slice). Why did he know that? Does h have some known constant meaning in integration?  Why is it divided by 3?  Normally I would have though it would be a normalization factor, but it appears to just be a part of the equation for no known reason?
 
 
After this my head just explodes.
I’m assuming his first pass with the sin() calls is basically part of a hemispherical integration template (when integrating over a hemisphere/curve, this is just how you do it) meant to test that he is integrating over an arbitrary circle correctly, but then he changes to exp() and by this point I just give up trying to follow how he took the formula and made the code.
I don’t see where he actually did the math in the formula, and instead at the end he pulled an atan() and sqrt() out of his ass and it’s all good.
 
 
 
So I would like 2 things:
#1: A break-down of this particular formula and conversion so I can have a good set of formulas whose conversions into code I can at least follow.
#B: I somewhat want to be spoon-fed on this particular example, but I definitely want to be able to do all of these on my own easily.  I am so tired of not being able to follow equations in research papers and even worse unable to make my own.  I want to know some good places, even if not free, online, where I can go through a course at a reasonable pace for a working person with 20 dates with supermodels every week.
A place that is structured like a high school class, possibly even with homework, but with the important point being that it teaches the information from start to finish step-by-step.
 
 
L. Spiro

Share this post


Link to post
Share on other sites
Advertisement

I know from some graphics equations that that squiggly bar with a and b has something to do with angles or hemispheres, so I guess the sin()’s came from that.

 

If you mean the integral symbol, not just angles or hemispheres. You integrate over a variable, which might be an angle. In the article it just uses a generic "f(x)", where f is integrated over the variable x. A bit unclear, but here f(x) = sin(x), so thats where the sine comes. It could be any function, integrated over any variable (specified by the dx thing).

 

 

 


Then there is h.  What is this magical number?

"step size h"

Integrals are not discrete, you break it into steps of size h along the variable being integrated over to make it computable. You approximate the integral, h controls the accuracy. Infinitesimally small value (infinite steps) is equivalent to the integral itself.

 

 

 


Why is it divided by 3?

If you look inside the [] you can see that its something like 2*(sum of f(x) over half range) + 4*(sum of f(x) over half range) + the edges.

Ignoring edges, thats something like 6*(sum of f(x) over half range) = 3*(sum of f(x))

That 3 needs to go poof since we only want sum of f(x) over the range.

 

Thats a horrible simplification of what seems like a complicated method though. Stare at the derivation on the wikipedia page for a few hours to know why it actually is there smile.png

 

 

Integrals are not dependent with trigonometry in any fundamental way. You can integrate over trigonometric functions using the angle as the variable of integration, but thats no different from any other arbitrary function. That probably got you confused.

 

The rest seems to be just examples of using this approximation with more complicated functions (instead of sin(x)), spawning all those sqrts and stuff.

 

 

You can probably calculate the area of some discrete function using a for loop (sum of rectangular slices).

An integral is what you get with infinite steps, but you know that.

What the article shows, is not just boxes. A flat-top box is a bad approximation of a curved line. So they come up with maths to use different shapes for the top of the box. A slanted top instead of flat. Or maybe some fancy polynomial (which seems to be what this "simpsons rule" used in the article is?). To minimize the "error" between the flat top of the box, and the not-flat curve of the function being integrated.

 

I know khan academy is one place to learn such things, dont know if pace is right or if you like videos (get a book if not, should be plenty to choose from).

edit: https://www.khanacademy.org/math/integral-calculus/indefinite-definite-integrals  (Not sure if they cover the approximation used in that article, but it has something about approximation at least)

Edited by Waterlimon

Share this post


Link to post
Share on other sites

When you say squiggly bar, are you referring to the integral symbol?

Yes.
 

A bit unclear, but here f(x) = sin(x), so thats where the sine comes.

That would have helped significantly in understanding this mess. Now tons of crap falls into place. Now I can follow his code completely (until he does the strange refactors).
But how would anyone know that other than by him saying so?  I don’t even see that on the page explaining Simpson’s Rule.

Same with the meaning of h.  If all you have is the equation, how would you know what to do with h?

 

 

Well I feel less lost than I did before, although there are still very confusing things.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

 

When you say squiggly bar, are you referring to the integral symbol?

Yes.
 

A bit unclear, but here f(x) = sin(x), so thats where the sine comes.

That would have helped significantly in understanding this mess. Now tons of crap falls into place. Now I can follow his code completely (until he does the strange refactors).
But how would anyone know that other than by him saying so?  I don’t even see that on the page explaining Simpson’s Rule.

Same with the meaning of h.  If all you have is the equation, how would you know what to do with h?

 

 

Well I feel less lost than I did before, although there are still very confusing things.

 

 

L. Spiro

 

 

He wants to compute N(x), which requires a definite integral. With pencil and paper he could/would use classical integration techniques; on a computer, however, he's probably better off using numerical integration. So he implements Simpson's rule (where 2510c39011c5be704182423e3a695e91.png is the "step length", given by e5a8d405bf46f7c522d473eb0ffb7a70.png - see Wikipedia) and uses f(x)=sin(x) as a test case for accuracy, then finally computes N(x).

 

You've probably seen many methods for numerical integration: Euler, Runge-Kutta and Verlet.

 

I'm not qualified to help with #A. As for #B:

Share this post


Link to post
Share on other sites
When talking about numerical methods, the word "integration" means two different things:
(1) computation of the value of a definite integral (this is sometimes called "quadrature", which is less ambiguous)
(2) solving a differential equation

Simpson's rule is of type (1), while Euler, Runge-Kutta and verlet are of type (2).

I don't have any good pointers to resources that would help you learn about these things, but I'll be happy to answer questions or discuss specific problems in detail. Just PM me.

Share this post


Link to post
Share on other sites

We all would probably benefit if he didn't PM you and you answered here. :)


Oh, if the questions are appropriate for this forum, that's much better. But for some more continued mentorship, particularly if he takes on a long-term project, it might be better to do it privately.

Share this post


Link to post
Share on other sites

When talking about numerical methods, the word "integration" means two different things:
(1) computation of the value of a definite integral (this is sometimes called "quadrature", which is less ambiguous)
(2) solving a differential equation

Simpson's rule is of type (1), while Euler, Runge-Kutta and verlet are of type (2).

 

Thanks for pointing this out. After university I never solved another integral by hand, and had never paid attention to the the conceptual difference.

Share this post


Link to post
Share on other sites

I have written my own version of that equation.

//#define Func( X ) sin( X )
#define Func( X ) exp( -(X) * (X) / 2.0 )


double N( double x )
{
	int N = 2000;
	double a = 0.0;
	double b = x;
	double h = (b - a) / N;
	
	double sum0 = 0.0;
	int total = N / 2 - 1;
	for ( int j = 1; j <= total; ++j )
	{
		double s = a + 2 * j * h;
		sum0 += Func( s );
	}
	
	double sum1 = 0.0;
	total = N / 2;
	for ( int j = 1; j <= total; ++j )
	{
		double s = a + (2 * j - 1) * h;
		sum1 += Func( s );
	}
	
	double finalSum = Func( a ) + Func( b ) +
		2.0 * sum0 + 4.0 * sum1;
	finalSum = finalSum * h;

	// 0.39894228040143267793994605993438 = 1 / sqrt( 2.0 * PI )
	return (0.39894228040143267793994605993438 / 3.0) * finalSum + 0.5;
}

I wrote my version entirely on my own without looking at his code. The only error I made in my original version was omitting the + 0.5 at the end, but I will get to that later.

I can now explain everything about his code except for the + 0.5.
His code, for the lazy:

double normalDistribution(double x)
{
  if(x<-10.)return 0.;
  if(x>10.)return 1.;
  // number of steps
  int N=2000;
  // range of integration
  double a=0,b=x;
  // local variables
  double s,h,sum=0.;
  // inialise the variables
  h=(b-a)/N;
  // add in the first few terms
  sum = sum + exp(-a*a/2.) + 4.*exp(-(a+h)*(a+h)/2.);
  // and the last one
  sum = sum + exp(-b*b/2.);
  // loop over terms 2 up to N-1
  for(int i=1;i<N/2;i++)
  {
    s = a + 2*i*h;
    sum = sum + 2.*exp(-s*s/2.);
    s = s + h;
    sum = sum + 4.*exp(-s*s/2.);
  }
  // complete the integral
  sum = 0.5 + h*sum/3./sqrt(8.*atan(1.));
  // return result
  return sum;
}

#1: He was using sin() originally because if you implement Simpon’s Rule using sin() as your function, you can verify the accuracy of your implementation by checking against (1.0-cos(b)).

#2: We have to switch to exp() because the actual equation our integration routine should be evaluating calls for it. See top of the article.
I made this switch easy via a macro.

#3: He wants to iterate only once because we can reuse intermediate values that way, so he rearranged the loops. The 2nd sigma set has 1 more iteration than the 1st so he put the j=1 result (of the 2nd sigma set) into the opening sum equations and inside the loop he works on indices j and j+1 (rather than j and j-1). This is why the “4.*exp(-(a+h)*(a+h)/2.)” and why in the loop he has “s = s + h” (the difference between indices is just h, and + rather than - because he is working 1 index up rather than down).

#4: Now we are going back to the original question which calls for 1 / sqrt( 2.0 * PI ).
atan( 1.0 ) is ¼ of PI. 8.0 * atan( 1.0 ) == 2.0 * PI.
Since it is a constant, I just worked it out and put the value there, and moved h/3.0 into that constant as well.


My implementation is a naïve implementation of the actual equation without all the shuffling.
It is slower, but more accurate (accuracy is actually more important for what I am trying to learn) under most situations.

 

 

The only thing about which I had no clue was that adding 0.5 would handle infinity.

He explained that it is doing that, but assumes we knew that already.

 

For as much as I can figure out on my own, I simply would never have known to do that.  That failure would have cost me an interview.

This is why I need courses.  I will look into what has been suggested and am still open to more suggestions.

 

 

L. Spiro

Edited by L. Spiro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!