In Internet pages like this, calculation procedures ("programs") can be inserted which allow various types of processing to be carried out. These procedures are called "scripts". It is the most used way of making programs (coding). Let's see various examples, some accessible since basic school. Multiple sessions of macosa.dima.unige.it/js.com can be opened simultaneously. Once you have created a program, don't click "Update" (Ctrl+R) if you don't want to lose the document (possibly copy it first or open another session). For ready-made scripts, which you can use as a "black box", see here; in particular, for basic school, see: how to draw with a computer, a game and these.
Starting from this example, it is easy to insert modifications to create other programs.
You can also copy the following by trying to put the program lines in place of ... and removing "$" (or other symbol you used) to run it, and possibly putting back "$" to make new changes before running it again ( removing "$").
<pre><script> with(Math) {$...} </script></pre>
[The programma is bounded by <script> and </script>.
The <pre> and </pre> commands cause the text to be written in a predetermined format, which reserves the same space for each character.
"document.write" causes the following to be written, indicated in parentheses.
"with(Math)" allows mathematical operations to be inserted, such as those indicated later, in C, in this documentc]
A) Example: the calculation of
(3/4 + 7.5 + 0.25 - 5/2)·(3/2 + 7/10 - 1/2 + 4.3) Copy and Paste:
C) Some of the insertable operations: +-*/abs(K) absolute value of K,max(H,K) the maximum between H and K,min(H,K) the minimum between H and K,pow(H,K) H to the Kth,round(H) the integer closest to H,sqrt(H) the square root of H. PI denotes π (≈ 3.141592653589793).
E denotes Napier's number (e ≈ 2.718281828459045).
To round x = 12345.6789 to hundredths you can use round(x*100)/100, to thousands round(x*0.001)/0.001
An example of calculations, in which writelnis used, which after printing makes a "new line" ("ln" stands for "line").
i = 1
|
write i*2 <
| |
i = i + 1 |
| |
i < 10 ? YES
|
| NO
|
STOP
A for loop appears: the loop is executed starting from the value assigned to the "counter variable" (in this example "i") until the indicated condition is valid (i<10); "i=i+1" indicates how it is changed
the counter variable at each end of the cycle;
the instruction that is repeated isdocument.write( i*2," " ), which causes the value of i*2 to be written followed by " ", i.e. a white space.
If instead of document.write(i*2," ") we had written document.writeln(i*2) we would have obtained: 2 4 ... 18
What does the following program calculate if you put any number in A and any natural number in N? Experiment with some values of A and of N.
<pre><script> with(Math) {
A = 2; N = 3
P=1
for(i=0; i<=N; i=i+1) P = P*A
document.write( P )
} </script></pre>
read A
read N
set P = 1
set I = 0
| yes
----> I = N ? -------> write P
| | no
| set P = P*A
| set I = I+1
| |
--------
The sum of two natural numbers however long, having put the digits in two sequences (using a "decreasing" for loop and an instruction if, where "equal" is written "==""):
Output: It's morning If I put time = 22 I get: It's night
In a statement if (as we had already seen) "equals" is written "==", "and" is written "&&"; "not" is written "!";
"different" is written "!="; "or" is written "||".
Two for loops appear, one boxed within the other:
the "i" loop prints the lines; each row is printed by a loop "j";
the firstdocument.write(" ")writes a blank space before the number if it has only one digit;
the finaldocument.write(" ")writes the blank space that separates one number from the next;
when exiting the boxed loop,document.write("<br>")generates a "new line" between one line and the next.
With small modifications (which ones?) a multiplication table with more numbers is produced:
I can define a function that can be called from other functions. For example, a function to round to the nth digit after the ".": function APPR(x,n) {return round(x*pow(10,n))/pow(10,n)}
<pre><script> with(Math) {function APPR(x,n) {return round(x*pow(10,n))/pow(10,n)}
function F(x) { return pow(x,5)+6*pow(x,3)+x/7 }
x = 3; document.writeln( "x=",x, " -> ",F(x) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),10) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),5) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),0) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),-1) )
x = 3; document.writeln( "x=",x, " -> ",APPR(F(x),-2) )} </script></pre>
J) The mean (and sum, min, max) of asequenceof data
<pre><script> with(Math) {n=5; A = [172,169,183,181,167]
min=A[0]; max=A[0]
S = 0; for(i=0; i<n; i=i+1) {S = S+A[i]; if(A[i]>max) max=A[i]; if(A[i]<min) min=A[i]}
document.write ("the sum of ",A," is ", S, "<br>")
document.write ("the mean is ",S,"/",n, " = ", S/n,"<br>")
document.write ("min = ",min," max = ",max )
} </script></pre>
n = 5
|
A[0] = 172, A[1] = 169, A[2] = 183, A[3] = 181, A[4] = 167
|
min = A[0], max = A[0], SUM = 0
|
i = 0
|
SUM = SUM + A[i] <
| |
if A[i] > max put max = A[i] |
| |
if A[i] < min put min = A[i] |
| |
i = i + 1 |
| |
i < n ? YES
|
| NO
|
write the values of SUM, SUM/n, min, max
|
STOP
Outputs:
the sum of 172,169,183,181,167 is 872
the mean is 872/5 = 174.4
min = 167 max = 183
Alternative, using "length", with the same outputs:
<pre><script> with(Math) {A = [172,169,183,181,167]
n = A.length; min = A[0]; max = A[0]
S = 0; for(i=0; i<n; i=i+1) {S = S+A[i]; if(A[i]>max) max=A[i]; if(A[i]<min) min=A[i]}
document.write ("the sum of ",A," is ", S, "<br>")
document.write ("the mean is ",S,"/",n, " = ", S/n,"<br>")
document.write ("min = ",min," max = ",max )
} </script></pre>
I can also order the words:
<pre><script> with(Math) {A = ["casa", "pino", "cielo", "zio", "aria"]
n = A.length; min = A[0]; max = A[0]
for(i=0; i<n; i=i+1) {if(A[i]>max) max=A[i]; if(A[i]<min) min=A[i]}
document.write ("min = ",min ," max = ", max )
} </script></pre>
Output: min = aria max = zio
Not just "minimum" and "maximum", but the complete sort
n = 260 min = 1 max = 2.3
median = 1.65 1^ quarter = 1.55 3^ quarter = 1.75
mean = 1.6592307692307673
The middle 50% of the sorted data falls in the range [1.55 cm, 1.75 cm]
K) Bar charts / histograms:
<pre><script> with(Math) {data = [127,585,430,1256,148]
n = data.length
total = 0; for(i=0; i<n; i=i+1) total = total+data[i]
document.write(data); document.write(" HISTOGRAM:\n\n")
for(i=0; i<n; i=i+1) { p = round(data[i]/total*150);
for(k=1; k<=p; k=k+1) document.write("#");
document.write(" ",data[i],"\n") }
} </script></pre>
N)
In this example, the user only has to manage the assignment to "go", to command the movements, to the Right, Left, Up and Down of "0", like the movement of a snake.
As in other cases, the user can gradually modify the input, immediately seeing the changes in the outputs.
The following lines are understandable by older kids:
a 50×50 array is built which contains for each column and each row the characters to appear on the screen: "0" or " " (i.e. a blank space character).
<pre><script> with(Math) {R=1; L=2; U=3; D=4
go = [R,R,R,R,R,R,R,R,R,R,R,R,D,D,D,D,D,D,D,D,D,L,L,L,L,L,L,L,L,L,L,L,
U,U,U,U,U,U,U,R,R,R,R,R,R,R,R,D,D,D,D,D,L,L,L,L,L,L,U,U,U,R,R,R,R,D]
var T = new Array(50);
for (i=0; i<50; i=i+1) {T[i]=new Array(50)}
for (i=0; i<50; i=i+1) {for(j=0; j<50; j=j+1) T[i][j]=" "}
n = go.length; u=0; v=0
for(i=0; i<n; i=i+1) {
if(go[i] == R) {v=v+1; T[u][v]=0}
if(go[i] == L) {v=v-1; T[u][v]=0}
if(go[i] == U) {u=u-1; T[u][v]=0}
if(go[i] == D) {u=u+1; T[u][v]=0}
}
for (i=0; i<50;i=i+1) {for(j=0; j<50; j=j+1) {document.write(T[i][j]); if(j==49) document.write("<br>")} }
} </script></pre>
P) Various examples
First of all, let us recall the names of other functions (of a) that can be used in addition to those called in C): cbrt(a) cube root,
sign(a) sign (-1, 1 o 0),
ceil(a) intero vicino non < a,
floor(a) integer closest to a not > a,
cos(a) cosine,
sin(a) sine,
tan(a) tangent,
acos(a) arc cosine,
asin(a) arc sine,
atan(a) arc tangent,
log10(a) log base 10,
log2(a) log base 2,
log(a) log base e,
random() random number in (0,1).
p0)
You can use floor(random()*6+1) to generate the random roll of a dice. To generate the output of a generic number between 3 and 7 you can use random()*(7-3)+3. Example:
(theoretically I would find that the possible cases are 6*6 = 36 and the good ones are 10, and 10/36 = 5/18 = 0.2777 )
If I want to quantify the precision of the experimental estimate (by means of an interval in which the probability falls at 99.7%), I can add "±3·σ√n" (for theoretical justifications see the topic "central limit theorem"):
Stopping at 1 million experimental trials I have (27.74±0.25)%
p1)
<pre><script> with(Math) {
n = 20; S = 0
for(i=0; i<=n; i=i+1) S = S+i
document.write(S)
} </script></pre>
210 ( = 0+1+2+...+20 )
N = 20
|
S = 0, i = 0
|
S = S + i <
| |
i = i + 1 |
| |
i ≤ N ? YES
|
| NO
|
write S
|
STOP
S = 0, i = 0
S = S+i = 0, i = i+1 = 1
i ≤ 20? yes
S = S+i = 1, i = i+1 = 2
i ≤ 20? yes
S = S+i = 3, i = i+1 = 3
i ≤ 20? yes
S = S+i = 6, i = i+1 = 4
i ≤ 20? yes
...
S = S+i = 190, i = i+1 = 20
i ≤ 20? sì
S = S+i = 210, i = i+1 = 21
i ≤ 20? no
write 210
p2)
<pre><script> with(Math) {
n = 5; P = 1
for(i=1; i<=n; i=i+1) P = P*i
document.write(P)
} </script></pre>
120 ( = 1*2*3*4*5 )
N = 5
|
P = 1, i = 1
|
P = P * i <
| |
i = i + 1 |
| |
i ≤ N ? YES
|
| NO
|
write P
|
STOP
P = 1, i = 1
P = P*1 = 1, i = i+1 = 2
i ≤ 5? yes
P = P*2 = 2, i = i+1 = 3
i ≤ 5? yes
P = P*3 = 6, i = i+1 = 4
i ≤ 5? yes
P = P*4 = 24, i = i+1 = 5
i ≤ 5? yes
P = P*5 = 120, i = i+1 = 6
i ≤ 5? no
write 120
p3)
<pre><script> with(Math) {
x1 = 1; y1 = 2; x2 = 6; y2 = 7
document.write( "distance between (",x1,",",y1,") and (",x2,",",y2,") <br>" )
document.write( sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) )
} </script></pre>
distance between (1,2) and (6,7)
7.0710678118654755 p4)
<pre><script> with(Math) {
x1 = 1; y1 = 1
x2 = 4; y2 = 0
x3 = 0; y3 = 5
L1=sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) )
L2=sqrt( (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) )
L3=sqrt( (x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) )
document.write( "P1P2 P1P3 P2P3: ", L1," ", L2," ", L3,"<br>" )
document.write( "perimeter of the triangle P1,P2,P3 = ", L1+L2+L3, "<br>" )
document.write( "area of the triangle P1,P2,P3 = ", abs(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2 )
} </script></pre>
P1P2 P1P3 P2P3: 3.1622776601683795 4.123105625617661 6.4031242374328485
perimeter of the triangle P1,P2,P3 = 13.688507523218888
area of the triangle P1,P2,P3 = 5.5
You can draw the triangle and calculate its area, sides, angles with this script getting this image.
That is, I can generate a triangle with this script and then check the response with what we have just seen.
p5)
2nd degree equations:
<pre><script> with(Math) {
title = "Solutions of a*x^2 + b*x + c = 0"
a = 1
b = 2
c = -15
document.write(title+"\n a = ",a,", b = ",b, ", c = ",c+"<br>")
d = b*b-4*a*c
k1 = -b/(2*a) + sqrt(d)/(2*a); k2 = -b/(2*a) - sqrt(d)/(2*a)
if(d < 0) document.write("no solution")
if(d > 0) document.write(k1, " ", k2)
if(d == 0) document.write(k1)
} </script></pre>
Solutions of a*x^2 + b*x + c = 0
a = 1, b = 2, c = -15
3 -5
3rd degree equations
Let's put the solution procedure, without explaining it.
solutions of A*x^3 + B*x^2 + C*x + D = 0
A = 1, B = 3, C = -10, D = -24
3 sol: 3 -3.9999999999999996 -2.0000000000000013 (or 3 -4, -2)
solutions of A*x^3 + B*x^2 + C*x + D = 0
A = 1, B = 9, C = 15, D = -25
2 sol: 1 -5
solutions of A*x^3 + B*x^2 + C*x + D = 0
A = 1, B = 0, C = 0, D = 2
1 sol: -1.2599210498948732
p6)
How is the square root of a number X calculated? For example proceeding by bisection: I start from an interval that contains the square root ([0,X] if X<1, [0,1/X] otherwise); little by little I find half of it, M; if M*M < X I take the right one as the new interval, otherwise I take the left one; and so on.
<pre><script> with(Math) {
x = 1000
a = 0; if(x > 1) {b = x} else {b = 1/x}
for(n=1; n<15; n=n+1) {
m = (a+b)/2; y = m*m
if(y < x) {a = m} else {b = m}
document.writeln("n = ",n , " a = ", a," b = ",b)
}
} </script></pre>
n = 1 a = 0 b = 500
n = 2 a = 0 b = 250
n = 3 a = 0 b = 125
n = 4 a = 0 b = 62.5
n = 5 a = 31.25 b = 62.5
n = 6 a = 31.25 b = 46.875
...
n = 14 a = 31.6162109375 b = 31.67724609375
If x = 0.1 I get:
n = 1 a = 0 b = 5
n = 2 a = 0 b = 2.5
...
n = 14 a = 0.316162109375 b = 0.3167724609375
Analogously I solve the equations f(x) = 0 starting from an interval in which f changes sign (proceeding by bisection: if in the middle of the interval the function has the same sign as in the left end I assume that the solution lies in the right half, otherwise I assume which is in the left half).
Example: solve (with respect to x) 5x = 100, starting from [0,10] (since
50<100 and 510>100).
<pre><script> with(Math) {
function F(x) {return pow(5,x)-100}
a = 0; b = 10
for(n=10; n<3000; n=n*2) {
for(i=0; i<n; i=i+1) {
m = a+(b-a)/2; y1 = F(a); y2 = F(m); y3 = F(b)
if(y1*y2 > 0) {a = m} else {b = m}
}
document.writeln("n = ",n , " a = ", a," b = ",b)
}
} </script></pre>
n = 10 a = 2.861328125 b = 2.87109375
n = 20 a = 2.861353112384677 b = 2.8613531216979027
n = 40 a = 2.861353116146786 b = 2.8613531161467862
n = 80 a = 2.861353116146786 b = 2.8613531161467862
n = 160 a = 2.861353116146786 b = 2.8613531161467862
n = 320 a = 2.861353116146786 b = 2.8613531161467862
n = 640 a = 2.861353116146786 b = 2.8613531161467862
n = 1280 a = 2.861353116146786 b = 2.8613531161467862
n = 2560 a = 2.861353116146786 b = 2.8613531161467862
I take as rounding of the solution 2.8613531161468. (with software like WolframAlpha we would find 2.861353116146786101340213...)
p7)
It can be convenient to tabulate a function in a range. For example we tabulate x → -x4-x3+7 (with 11 values) between -50 and 50:
<pre><script> with(Math) {
function F(x) {return -pow(x,4)-pow(x,3)+7}
a = -50; b = 50
h=(b-a)/10
for(n=0; n<=10; n=n+1) {document.writeln("x = ",a+h*n," F(x) = ",F(a+h*n))}
} </script></pre>
x = -50 F(x) = -6124993
x = -40 F(x) = -2495993
x = -30 F(x) = -782993
x = -20 F(x) = -151993
x = -10 F(x) = -8993
x = 0 F(x) = 7
x = 10 F(x) = -10993
x = 20 F(x) = -167993
x = 30 F(x) = -836993
x = 40 F(x) = -2623993
x = 50 F(x) = -6374993
This can be useful for having a quick assessment of the trend of the function. In this case, taking a and b as they get closer, we will be convinced that the function has a trend /\ between -10 and 10 (using WolframAlpha we could easily draw the graph) and we could look for where it assumes the maximum value. Let's see how.
Given a function F, having identified an interval in which the trend is /\, we can find the maximum in that interval, or having identified one in which the trend is \/ we can find the minimum.
The idea is to gradually divide the interval into 3 parts and take the 2/3 where the value sought is.
Here is the program to find the maximum (in the case of the previous function):
n=10 max between -0.8421819167132386, -0.49535131839658636 val= 7.094270215667542
n=20 max between -0.7500533542630592, -0.7499490523620422 val= 7.105468749998371
n=40 max between -0.750000032934137, -0.7500000329247042 val= 7.105468749999999
n=80 max between -0.750000032934137, -0.7500000329341369 val= 7.105468749999999
n=160 max between -0.750000032934137, -0.7500000329341369 val= 7.105468749999999
n=320 max between -0.750000032934137, -0.7500000329341369 val= 7.105468749999999
n=640 max between -0.750000032934137, -0.7500000329341369 val= 7.105468749999999
n=1280 max between -0.750000032934137, -0.7500000329341369 val= 7.105468749999999
n=2560 max between -0.750000032934137, -0.7500000329341369 val= 7.105468749999999
We guess that the exact value of the point where the maximum is -0.75 = -3/4 and that here F is 7.10546875. The "strange" values on the last digits are due to the approximations made by the program. In any case, in any real problem, one must round to a relatively small number of digits (with WolframAlpha we would find exactly -3/4 and 1819/256).
The program to find the minimum applied to the case of x → 7*x2 - x/3 + 5:
n=10 min between -0.21998679063150905, 0.12684380768514317 val= 5.030706157217319
n=20 min between 0.0237545988945348, 0.02385890079555173 val= 4.9960317671489705
n=40 min between 0.023809512484711453, 0.02380951249414428 val= 4.996031746031747
n=80 min between 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747
n=160 min between 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747
n=320 min between 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747
n=640 min between 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747
n=1280 min between 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747
n=2560 min between 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747
The minimum is 0.0238095124847 where F is 4.99603174603 (with WolframAlpha we would find 1/42 and 1259/252)
Another example. We figured out (by tabulating the function) that (x*sin(x)+sqrt(x)-x*x)3-x4 has a maximum between 0 and 0.8. Let's find it more precisely.
n=10 max tra 0.5923893715388915, 0.6062625954715576 val= 0.2980093461765785
n=20 max tra 0.599325390462832, 0.5993295625388726 val= 0.2980093461811849
n=40 max tra 0.599327315089737, 0.5993273150901144 val= 0.2980093461812495
n=80 max tra 0.599327315089737, 0.5993273150897371 val= 0.29800934618124975
n=160 max tra 0.599327315089737, 0.5993273150897371 val= 0.29800934618124975
n=320 max tra 0.599327315089737, 0.5993273150897371 val= 0.29800934618124975
n=640 max tra 0.599327315089737, 0.5993273150897371 val= 0.29800934618124975
n=1280 max tra 0.599327315089737, 0.5993273150897371 val= 0.29800934618124975
n=2560 max tra 0.599327315089737, 0.5993273150897371 val= 0.29800934618124975
I could take x = 0.5993273, y = 0.29800934618125, but, if the problem is "practical", it is sufficient to take x = 0.5993, y = 0.2980 (with Geogebra I would instead get x = 0.6, y = 0.3!!! With WolframAlpha I would get correctly x ≈ 0.599327, y ≈ 0.298009).
p8)
Theminimumandmaximumof a function in an interval, if its trend is not known (\/ or/\ or ...), can be approximated with a dense tabulation.. Consider the same function studied above:
<pre><script> with(Math) {
function F(x) {return 7*x*x - x/3 + 5 }
a = -10; b = 10
min = 1e300; max = -1e300
n = 8*9*5*7*11*13
for(i=0; i<=n; i=i+1) {x = a+(b-a)/n*i; y = F(x); if(y<min) {min=y; x1=x}; if(y>max) {max=y; x2=x}}
document.write("min ≈ ", min, " (x=", x1, ") max ≈ ", max, " (x=", x2, ")")
} </script></pre>
min ≈ 4.996031746031746 (x=0.023809523809523725) max ≈ 708.3333333333334 (x=-10)
I have an estimate of the point where the function assumes the minimum value and an estimate of this value: 0.023809523809523725 instead of 0.0238095124847, 4.996031746031746 instead of 4.99603174603.
p9)
A biologist has to do an experiment on 2 mice, chosen from the 4 (A, B, C and D) at his disposal. In how many ways can he make the choice?
6: AB, AC, AD, BC, BD, CD. It is the number of subsets of 2 elements of a set of 4 elements, i.e. the combinations of 4 elements 2 to 2. A formula for counting them is 4/2·3/1 (= 6). If he had to choose 15 mice out of 25, the possible choices would be 25/15·24/14·...·11/1. How many are they?
<pre><script> with(Math) {
document.write(25/15*24/14*23/13*22/12*21/11*20/10*19/9*18/8*17/7*16/6*15/5*14/4*13/3*12/2*11/1)
} </script></pre>
3268760 More than 3 million!
The calculation can be done easily, and in general, with a for loop. The combinations C(25,15) of 25 elements 15 to 15 are:
The "round" instruction is used to not display the possible variation from the whole numbers deriving from the approximations.
An example of a calculation using binomial coefficients: a calculation related to the binomial distribution law. In a certain population of adults in a given region, it is known that a particular childhood disease has affected an average of 1 person out of 8; if 100 adults from that region are taken completely at random, what is the probability that no more than 10 have been affected by it?
To understand, let's see how to calculate the probability that 3 have been affected:
the probability that the top 3 (or bottom 3 or 1st, 3rd and 9th or ) have been affected: (1/8)3·(1-1/8)97
the ways i can choose the 3 people out of 100: C(100,3)
the probability that exactly 3 were affected: C(100,3)·(1/8)3·(1-1/8)97
The solution of the problem: C(100,0)·(1/8)0·(1-1/8)100+C(100,1)·(1/8)1·(1-1/8)99+
... + C(100,10)·(1/8)10·(1-1/8)90
The probability that no more than 10 people have been affected by the disease is 28.1%.
p10)
The integration. Consider the integral between 1 and 5 of x → 2*x*x + 3*x + 1.
We proceed by approximating the area with n small rectangles (I divide the interval into n small intervals which I take as the bases of the rectangles; as their heights I take the values of the function in their center; if the values are negative I take the small rectangles facing downwards). We start with n=10 and we gradually double n:
<pre><script> with(Math) {
function F(x) { return 2*x*x + 3*x + 1 }; a = 1; b = 5
document.writeln("F(x) = 2*x*x + 3*x + 1; a = 1; b = 5")
n=10
for(i = 0; i < 5; i = i+1) {
s=0; h=(b-a)/n; for (var j=0; j < n; j=j+1) {s = s + F(a+(j+1/2)*h)}
document.writeln(n, " rectangles, integral of F on [a,b] = ", s*h); n=n*2
}
} </script></pre>
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5
10 rectangles, integral of F on [a,b] = 122.56000000000004
20 rectangles, integral of F on [a,b] = 122.64000000000001
40 rectangles, integral of F on [a,b] = 122.66000000000003
80 rectangles, integral of F on [a,b] = 122.66500000000003
160 rectangles, integral of F on [a,b] = 122.66624999999999
We increase n, putting n=10000 instead of n=10:
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5
10000 rectangles, integral of F on [a,b] = 122.66666655999998
20000 rectangles, integral of F on [a,b] = 122.66666663999993
40000 rectangles, integral of F on [a,b] = 122.66666665999948
80000 rectangles, integral of F on [a,b] = 122.6666666649996
160000 rectangles, integral of F on [a,b] = 122.66666666625022
I assume that ∫[a,b]F = 122.666 = 122+2/3 = 368/3
Another example: calculation of ∫[0,3] |x·(x−2)| dx
F(x) = abs(x*(x-2)); a = 0; b = 3
10 rectangles, integral of F on [a,b] = 2.6550000000000002
20 rectangles, integral of F on [a,b] = 2.6634374999999997
...
81920 rectangles, integral of F on [a,b] = 2.6666666664804004
163840 rectangles, integral of F on [a,b] = 2.6666666666201007
I assume that ∫[0,3] |x·(x-2)| dx = 2+2/3 = 8/3 (with Geogebra I would have gotten 2.67!!! For integration, search for maximum/minimum, inflections, it is certainly not a reliable software )
Another example: ∫[0,π/2] cos(x) dx
function F(x) { return cos(x) }; a = 0; b = PI/2
5000 rectangles, integral of F on [a,b] = 1.0000000041123318
...
80000 rectangles, integral of F on [a,b] = 1.0000000000160585
Another example: 1 − ∫[0,35] 0.1·exp(-0.1·x) dx
function F(x) { return 0.1*exp(-0.1*x) }; a = 0; b = 35
[document.writeln(n, " rettangoli, 1 - integrale di F su [a,b] = ", 1-s*h); n=n*2]
5000 rettangoli, 1 - integrale di F su [a,b] = 0.030197403222453345
10000 rettangoli, 1 - integrale di F su [a,b] = 0.030197388372357636
20000 rettangoli, 1 - integrale di F su [a,b] = 0.03019738465983124
40000 rettangoli, 1 - integrale di F su [a,b] = 0.030197383731682903
80000 rettangoli, 1 - integrale di F su [a,b] = 0.03019738349965262
p11)
It is very easy to study sequences defined by recursion. Let's take just 3 examples:
x[0] = A, x[n+1] = sqrt(x[n])
x[0] = 1, x[n+1] = (x[n] + A/x[n]) / 2
x[0] = A, x[1]=B, x[n+2] = (3*x[n+1]-x[n]) / 2
<pre><script> with(Math) {
A = 13
document.writeln("x[0] = ", A, ", x[n+1] = sqrt(x[n])")
x = A; N = 1
for(i=0; i<N; i=i+1) x = sqrt(x)
document.writeln("x[", i, "] = ", x)
} </script></pre>
With A=13, changing N:
x[0] = 13, x[n+1] = sqrt(x[n])
x[1] = 3.605551275463989
x[20] = 1.0000024461293169
x[54] = 1
With A=0.17:
x[0] = 0.17, x[n+1] = sqrt(x[n])
x[1] = 0.41231056256176607
x[54] = 0.9999999999999999
x[n] -> 1 as n -> ∞ whatever A is
<pre><script> with(Math) {
A = 36
document.writeln("x[0] = ", 1, ", x[n+1] = (x[n] + A/x[n]) / 2")
x = A; N = 1
for(i=0; i<N; i=i+1) x = (x + A/x)/2
document.writeln("x[", i, "] = ", x)
} </script></pre>
With A=36 for increasing values of N:
x[0] = 1, x[n+1] = (x[n] + A/x[n]) / 2
x[1] = 18.5
x[2] = 10.222972972972974
x[5] = 6.0002529841194185
x[7] = 6
x[n] -> √A as n -> ∞ whatever A > 0 is
A recursive sequence with 2 initial values:
<pre><script> with(Math) {
A = 1; B = 2
document.writeln("x[0] = ", A, ", x[1] = ", B, ", x[n+2] = (3*x[n+1]-x[n]) / 2")
x = [A, B]
N = 2
for(i=2; i<=N; i=i+1) x[i] = (3*x[i-1]-x[i-2]) / 2
document.writeln("x[", N, "] = ", x[N])
} </script></pre>
With A = 1, B = 2 for increasing values of N:
x[0] = 1, x[1] = 2, x[n+2] = (3*x[n+1]-x[n]) / 2
x[0] = 1, x[1] = 2, x[n+2] = (3*x[n+1]-x[n]) / 2
x[55] = 2.999999999999999
Try different values of A and B (e.g.: A = -3; B = 9 → x[55] = 21) and verify that:
x[n] -> 2*B - A
p12)
The linear regression. What can I say about the linear function F such that F(5)=10 whose graph passes through the following points? (there are many such problems; a simple example: the elongation of a rubber band or spring detected by hanging objects of different weights on it)
x: 12±1 17 ±1 24±1 33±1.5
y: 18±2 27±2 31±2.5 45±3
Look at these figures. The first represents the straight line that best approximates the points without taking into account their precisions.
The second represents the lines of minimum and maximum slope that pass through all the points, or, better, through all the approximate points, which are therefore in reality small rectangles.
The first straight line is found with a non-trivial algorithm on which this script is based; from it we obtain that y = 1.225*x+3.875.
The straight lines of the second figure are instead easy to find. We can use this simple program:
NOTE.
Specific INPUT statements could be inserted into scripts, but this would not lead to improvements in the case of simple scripts like these, in which one can directly change the values assigned to variables and immediately see the changes in the outputs. Those interested see here.