Integration question

My last math question here for the day,I just have one more and I think it's more a scientific notation question but not too sure,

any how,

I am trying to find the area under a curve using simpsons rule

we need to find the area between 0 and pie or 3.14

y = sin theta

but what doe sin theta mean how can I sub my values (in 6 equal strips) into this equation ( y = sin theta) to get the y value?

for example what if I want to get y value for 3.14?

thanks


this looks like a good article about Simpson's rule with an example https://www.intmath.com/integration/6-simpsons-rule.php

Pi , not pie, and are you going to post code...?
Hi Icy,

cheers for the link :)

no code just a theory question as it relates to programming more so screen manipulation etc

but it is important to know.
but also

still do you know what sin theta means and how I would solve for y?

I know how to use simpsons rule but don't understand getting the x value for y = sin theta or how I would even plug it in to simpsons rule?



Last edited on
as per that link, your problem would be integral of f(x) = sin(x), from 0 to π (in radians), with n=6

theta is a greek letter that implies angle, often as a parameter to sin/cos/tan etc. functions.
sin is... how do you not know what sin is? sin curve? that button on your calculator? Trig class?

Δx = (b-a)/n = π/6

y0 = f(0)

y1 = f(0 + π/6)
y2 = f(0 + 2 * π/6)
y3 = f(0 + 3 * π/6)
y4 = f(0 + 4 * π/6)
y5 = f(0 + 5 * π/6)
y6 = f(π)

etc.
Last edited on
Theta (θ) is just the variable name, just treat it like you would x.

If you wanted to achieve more, you could write a program that performed simpson's integration on a function based on the formula given in https://en.wikipedia.org/wiki/Simpson%27s_rule

PS: I think I did Simpson's rule correct, someone tell if me if I'm wrong.
EDIT: Fixed some logic (thanks icy1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <functional>
#include <cmath>
#include <cassert>

using Function = std::function<double(double)>;
const double Pi = 3.141592653589793238462643383279502884;

/// Simpson's rule
/// Approximates an integral with N equally spaced subdivisions on [a, b]
/// ASSUMPTION: a < b
/// ASSUMPTION: N >= 3
/// ASSUMPTION: N is even (required for proper Simpson's rule)
double simpsons_integrate(Function f, double a, double b, int N)
{
	double delta_x = (b - a) / N;
	
	double sum = 0.0;
	sum += f(a); // i == 0
	
	for (int i = 1; i < N-1; i++)
	{
		double x_i = a + i * delta_x;
		
		if (i % 2 == 1)
		{
			sum += 4 * f(x_i);
		}
		else
		{
			sum += 2 * f(x_i);
		}

	}
	sum += 4.0 * f(a + (N-1) * delta_x); // i == N - 1
	sum += f(b); // i == N
	
	return (delta_x / 3.0) * sum;
}

double sin_wrapper(double x)
{
	return std::sin(x);
}

int main()
{
	Function f = sin_wrapper;
	
	double approx_area = simpsons_integrate(f, 0.0, Pi, 6);
	
	std::cout << approx_area << std::endl;
}


adam, do you know what the correct answer to your problem is (for 6 subdivisions)?
Last edited on
thanks Ganado much appreciated :)

I will try to implement this :p
also for your particular case, since 30 degrees is equal to π/6, this is something I got shown and had to memorize in trig, the 30-60-90 triangle: https://qph.fs.quoracdn.net/main-qimg-c7979fa834b089bf2065db2bee5ac7a1

using SOHCAHTOA , an acronym to help remember,
- sin is "opposite"/hypotenuse
- cos is "adjacent"/hypotenuse
- tan is "opposite"/"adjacent"

(in degrees)

sin(0) = 0
sin(30) = 1/2
sin(60) = √3/2
sin(90) = 1

sin(120) = √3/2
sin(150) = 1/2
sin(180) = 0

If you can imagine a cartesian plane, look at top-right quadrant (quadrant I) and draw an angle of 30 degrees and form a triangle exactly like linked. To visualize higher angles like 120, from the positive x axis you'd continue counter-clockwise over into quadrant II https://www.onlinemathlearning.com/image-files/quadrants.png . iirc you'd draw a triangle touching the negative x axis, so really you'd see sin(120) is same as sin(60), but in quadrant II. The sides of that 30-60-90 triangle are -1, √3, and 2, but for sin(60) purposes, the -1 is not involved, so it's still √3/2

Similarly, sin(150) is like sin(30) in quadrant II. You'd draw a triangle like this https://qph.fs.quoracdn.net/main-qimg-27511e6d248c21e1c2d68f39a61d13d1 and visually you can see it's still positive 1/2.
Last edited on
@Ganado I think you've got your odds and evens reversed or so. If going to assert, might as well assert n is even and b>=a ;D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <iomanip>
#include <cmath>

double f(double x)
{
    return std::sin(x);
}

int main()
{
    std::cout << std::fixed << std::setprecision(12);
    constexpr double pi = 3.14159265358979323846;

    int n = 6;
    double a = 0;
    double b = pi;

    double dx = (b-a)/n;
    double sum = 0.0;
    sum += f(a);
    for (int i=1; i<n; ++i)
    {
        sum += (i&1) ? 4*f(a + i*dx) :
                       2*f(a + i*dx);
    }
    sum += f(b);
    std::cout << (dx/3.0)*sum << '\n';

    return 0;
}

2.000863189674


confirmation from
https://www.emathhelp.net/calculators/calculus-2/simpsons-rule-calculator/?f=sin+%28x%29&a=0&b=PI&n=6&steps=on
Thanks for checking my work, you're right I confused the odds and evens.

My line 35 is also wrong, I did (a + (N-1) * delta_x) instead of f(a + (N-1) * delta_x).

Fixed in my previous post so that I don't confuse future readers.
Last edited on
what does sin theta mean
...
its really sin(theta). think of it as a function in math, eg y = f(x) where f is the sin function.
sin is a lot of things, but the easy explain is its the value of opposite/hypotenuse in a right triangle where opposite is determined by the angle you are looking at (its the side across from that angle).

sin is built into c++, but it expects radians for the angles, not degrees. pi radians is 180 degrees exactly.

with that knowledge you should be able to make sense of what they gave you already or try to do it yourself.
Last edited on
thanks guys much appreciated,

I've been programming for approx 2 years and now only starting to see how crucial math is to programming.
Topic archived. No new replies allowed.