Sign in to follow this  
L. Spiro

Learning Calculus Online

Recommended Posts

L. Spiro    25638
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
Waterlimon    4398

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
L. Spiro    25638

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
georger.araujo    1282

 

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
alvaro    21263
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
alvaro    21263

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
georger.araujo    1282

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
L. Spiro    25638

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
alvaro    21263
Your function integrates from 0 to x. If the value your are interested in is the integral from -infinity to x, you can break it up as the sum of the integral from -infinity to 0 plus the integral from 0 to x. The integral from -infinity to 0 is 0.5, which is why you are adding that at the end of the code.

Here you can look at the integral from 0 to x, or from -infinity to x, or from x to +infinity: http://www.mathsisfun.com/data/standard-normal-distribution-table.html

Share this post


Link to post
Share on other sites
alvaro    21263
Oh, and one nitpick about your code: You really shouldn't use macros where functions are more appropriate.

For instance, this would work for the sin(x) case, but not for the exp(-x^2) case:
  int j = 1;
  while (j <= total)
    sum0 += Func(a + 2 * j++ * h); // This modifies j twice, and it's actually undefined behavior!

No problems if you defined Func like this:
double Func(double x) {
  return std::exp(-0.5 * x * x);
}

Share this post


Link to post
Share on other sites
L. Spiro    25638
I'm aware of when macros are evil.
I'd not have shown that to someone in an interview. It's hack just for my learning process. In fact I didn't even write this code in C or C++. I'm using my script language which looks a lot like C.


L. Spiro

Share this post


Link to post
Share on other sites
JohnnyCode    1046


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.

 

If you feel like you need to gain more theoretical knowledge of math, I would give you advice I have made myself.

I divide math into two subsets: "facts" (axioms, definitions), "findings" (statements, implications, methods..). Findings is such a rich subset of math, that you possibly cannot consume it, but facts are something that if you collect completely, you will be quite sufficient with theory.

 

The fact is that integration of a function relates to derivation of a function like this:

 

S(f'(x))=f(x) , where S stands for integral and ' for derivative

 

In other words, if someone puts integral in front of a function, he seeks a function that the function behind integral is the derivative of.

For example f(x)=2x , then S(f(x))=x^2 , because derivation of x^2 is 2x .

 

Then there is this interesting "finding" about integrals, that

S f(x)=F(x) , then F(b)-F(a) equals signed surface between x axis and function f(x) on the interval of x in [a,b], if f(x) has a limit on entire interval (is continious).

Also it has been agreed, that F(b)-F(a)=Sa,b  what is called definite integral.

 

To prove this finding, you would need to know as well the definition of derivation of a function, and thus also limit of a function, what is quite too gigantic to even get a preview sight into, but I will try to elaborate further still:

 

Derivation of f(x) function at point a, exists if there is the limit of the function at point a, then f'(a) can be evaluated as lim (h->0) (f(a+h)-f(a))/h.

 

This would return a derivation at point a, what if you look closer is a tangential(tan()) of the function tangent at point a with x axis. But in general you want to find the derivation function- the function that will return the derivative of f(x) for any provided point x, thus finding f'(x), not only finding derivation at a single point.

 

So the last thing of this insight, is to define limit at point of a function, that is, if I will recall my memories from university, should be something not contradicting to this (a both sided limit at least)

 

lim x->a f(x)=v, if there exists s from R /{0},  for which two numbers L,K from R-{0} exist, so that [all f(x),x in (a-s,a+s), interval] is inclusive to ((v-L),(v+K)) interval.

 

This means that a limit of a function at a point is most of the time just the value of the function at that point, that is why you are generaly asked for limits rather like:

lim x->2 1/(x-2) where a limit does not exist

Share this post


Link to post
Share on other sites
alvaro    21263
There is a lot wrong or confused with what JohnnyCode said. I am sure he is trying to help, and it is very possible that his imprecise knowledge of the subject is enough for his needs; but you should probably learn this from a more rigorous source.

Share this post


Link to post
Share on other sites
JohnnyCode    1046


There is a lot wrong or confused with what JohnnyCode said. I am sure he is trying to help, and it is very possible that his imprecise knowledge of the subject is enough for his needs; but you should probably learn this from a more rigorous source.

I left too many things open and didn't detail them. I would not state "something is wrong" about my post though. Appointing wrong stuff would not be off-topic I believe Alvaro, but I am aware that a tremendous pile of stuff can be nit-picked, since my syntax has been so obscure. I am always happy to learn up as well  smile.png

Share this post


Link to post
Share on other sites
alvaro    21263
I'll go a little bit into it.

The term "integral", the way you are describing it, is what is normally known as "primitive function", "indefinite integral" or "antiderivative".

Your post seems to imply that the definite integral is defined through the fundamental theorem of calculus, which is not true. The definite integral is a concept that has multiple definitions. If a function happens to have a primitive, you can compute its integral using the fundamental theorem of calculus, but there are many situations where you want to compute an integral of a function for which there is no primitive.


Derivation of f(x) function at point a, exists if there is the limit of the function at point a, then f'(a) can be evaluated as lim (h->0) (f(a+h)-f(a))/h.


No, that's not right: f(x) = abs(x) has a limit at x=0, and yet it doesn't have a derivative at that point. Your use of the word "derivation" here is also objectionable.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this