"<p>Copia e incolla all'inizio della pagina (al posto di quanto c'è ora) le righe seguenti. Qui apparirà <b>10</b> . . . . . . |
Copia e incolla all'inizio della pagina (al posto di quanto c'è ora) le righe seguenti. Qui apparirà 10<pre><script> with(Math) { document.write(2+4*2) } </script></pre> |
<pre><script> with(Math) { document.write(2+4*2) } </script></pre> |
10 |
<pre><script> with(Math) { document.write( (3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3) ) } </script></pre>Uscita: 36
<pre><script> with(Math) { document.write( "calcolo di (3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3)<br>" ) document.write( (3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3) ) } </script></pre>Uscita:
<pre><script> with(Math) { document.write( "calcolo di (3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3)\n" ) document.write( (3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3) ) } </script></pre>(2) Alcune delle operazioni inseribili: + - * /
<pre><script> with(Math) { N=10 for(i=0; i<N; i=i+1) document.write(floor(random()*6+1)," ") } </script></pre>
6 3 5 4 3 6 2 5 2 1
<pre><script> with(Math) { x = 36 y = 10 document.writeln( x+y ) document.writeln( x-y ) document.writeln( x*y ) document.writeln( x/y ) document.writeln( round(x/y) ) document.writeln( min(x,y) ) document.writeln( max(x,y) ) document.writeln( pow(x,y) ) } </script></pre>
Uscite: 46 26 360 3.6 4 10 36 3656158440062976(3) La tabulazione di una funzione. Possiamo introdurre la funzione con function. Vediamo come:
<pre><script> with(Math) { function F(x) { return pow(x,5)+6*pow(x,3)+x/7 } document.writeln("F(x) = pow(x,5)+6*pow(x,3)+x/7") x = 1.5; document.write( "x=",x, " -> ",F(x), "\n" ) x = 2; document.write( "x=",x, " -> ",F(x), "\n" ) x = 3.5; document.write( "x=",x, " -> ",F(x), "\n" ) x = 3; document.write( "x=",x, " -> ",F(x), "\n" ) } </script></pre>
F(x) = pow(x,5)+6*pow(x,3)+x/7 x=1.5 -> 28.058035714285715 x=2 -> 80.28571428571429 x=2.5 -> 191.76339285714286 x=3 -> 405.42857142857144
<pre><script> with(Math) { for(i=1; i<10; i=i+1) document.write( i*2," " ) } </script></pre>Le uscite: 2 4 6 8 10 12 14 16 18
i = 1 | scrivi i*2 < | | i = i + 1 | | | i < 10 ? SI' | | NO | STOP |
<pre><script> with(Math) { A=[3,5,7,0,3,8,0,5,3,2,1,6,5,4,3,2,1,8,9,0,1,7,3,0,6,8,".",7,1,2,5] B=[0,0,0,0,8,7,4,0,5,1,0,0,3,4,8,1,9,2,1,5,2,5,1,6,0,3,".",7,5,0,0] n=A.length; R=0; C="" for(i=n-1; i>=0; i=i-1) {if(A[i]==".") {C="."+C} else {S=A[i]+B[i]+R; R=0; if(S>9) {S=S-10; R=1}; C=S+C} } if(R==1) {C="1"+C} document.writeln(A," +") document.writeln(B, " =") document.write(C) } </script></pre>
3,5,7,0,3,8,0,5,3,2,1,6,5,4,3,2,1,8,9,0,1,7,3,0,6,8,.,7,1,2,5 + 0,0,0,0,8,7,4,0,5,1,0,0,3,4,8,1,9,2,1,5,2,5,1,6,0,3,.,7,5,0,0 = 35712545831689141105424672.4625Abbiamo usato anche un'istruzione if ("se").
<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 ("la somma di ",A," e' ", S, "<br>") document.write ("la media e' ",S,"/",n, " = ", S/n,"<br>") document.write ("min = ",min," max = ",max ) } </script></pre>Uscite:
la somma di 172,169,183,181,167 e' 872 la media e' 872/5 = 174.4 min = 167 max = 183Posso ordinare numeri:
<pre><script> with(Math) { A = [23, 4.1, PI, 3, 5, 12, -3, 1e3, 1e-4] document.writeln(A) B = A n = A.length for(k=0; k<n; k=k+1) { for(i=k+1; i<n; i=i+1) { if(B[i]<B[k]) {C=B[k]; B[k]=B[i]; B[i]=C} } } document.write(B) } </script></pre>Uscita:
23,4.1,3.141592653589793,3,5,12,-3,1000,0.0001 -3,0.0001,3,3.141592653589793,4.1,5,12,23,1000Nota. 1000 e 0.0001 sono stati scritti 1e3 e 1e-4, ossia in notazione esponenziale: AeB sta per A*10B
<pre><script> with(Math) {
dat = [1.35, 1.65, 1.80, 1.40, 1.65, 1.80, 1.40, 1.65, 1.85, 1.40, 1.65, 1.85, 1.50, 1.65, 1.90, 1.50, 1.65, 1.90,
1.50, 1.65, 1.90, 1.50, 1.70, 1.90, 1.50, 1.70, 1.90, 1.50, 1.70, 2.25, 1.55, 1.70, 1.55, 1.70, 1.55, 1.70, 1.60,
1.70, 1.60, 1.75, 1.60, 1.75, 1.60, 1.80, 1.60, 1.80, 1.60, 1.80, 1.60, 1.80, 1.00, 1.55, 1.70, 1.75, 1.30, 1.55,
1.70, 1.75, 1.40, 1.60, 1.70, 1.75, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40,
1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.45, 1.60, 1.70, 1.80,
1.50, 1.60, 1.70, 1.80, 1.50, 1.60, 1.70, 1.85, 1.50, 1.60, 1.70, 1.85, 1.50, 1.60, 1.75, 1.90, 1.50, 1.60, 1.75,
1.90, 1.50, 1.65, 1.75, 1.90, 1.55, 1.65, 1.75, 1.95, 1.55, 1.65, 1.75, 2.00, 1.55, 1.65, 1.75, 2.30, 1.35, 1.65,
1.80, 1.40, 1.65, 1.80, 1.40, 1.65, 1.85, 1.40, 1.65, 1.85, 1.50, 1.65, 1.90, 1.50, 1.65, 1.90, 1.50, 1.65, 1.90,
1.50, 1.70, 1.90, 1.50, 1.70, 1.90, 1.50, 1.70, 2.25, 1.55, 1.70, 1.55, 1.70, 1.55, 1.70, 1.60, 1.70, 1.60, 1.75,
1.60, 1.75, 1.60, 1.80, 1.60, 1.80, 1.60, 1.80, 1.60, 1.80, 1.00, 1.55, 1.70, 1.75, 1.30, 1.55, 1.70, 1.75, 1.40,
1.60, 1.70, 1.75, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80,
1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.40, 1.60, 1.70, 1.80, 1.45, 1.60, 1.70, 1.80, 1.50, 1.60, 1.70,
1.80, 1.50, 1.60, 1.70, 1.85, 1.50, 1.60, 1.70, 1.85, 1.50, 1.60, 1.75, 1.90, 1.50, 1.60, 1.75, 1.90, 1.50, 1.65,
1.75, 1.90, 1.55, 1.65, 1.75, 1.95, 1.55, 1.65, 1.75, 2.00, 1.55, 1.65, 1.75, 2.30]
A = dat; n = A.length
for(k=0; k<n; k=k+1) { for(i=k+1; i<n; i=i+1) { if(A[i] < A[k]) {C = A[k]; A[k]=A[i]; A[i]=C} } }
document.writeln ("n = ", n, " min = ", A[0], " max = ", A[n-1])
document.writeln ("mediana = ", A[round(n/2)], " 1^ quarto = ", A[round(n/4)], " 3^ quarto = ", A[round(n*3/4)])
S = 0; for(i=0; i<n; i=i+1) {S = S + A[i]}; media = S/n
document.writeln ("media = ", media)
} </script></pre>
n = 260 min = 1 max = 2.3 mediana = 1.65 1^ quarto = 1.55 3^ quarto = 1.75 media = 1.6592307692307673 Il 50% centrale dei dati ordinati cade nell'intervallo [1.55 cm, 1.75 cm]
<pre><script> with(Math) { n=632 k=2; while(k <= n) {if(n/k == round(n/k)) {n = n/k; document.write(k," ")} else k=k+1 } } </script></pre>
2 2 2 79else (= "altrimenti") si può usare per indicare che cosa fare se la condizione di un if è falsa.
<pre><script> with(Math) { n=13068 k=2; while(k <= n) {if(n/k == round(n/k)) {n = n/k; document.write(k," ")} else k=k+1 } document.write("<br>") n=90882 k=2; while(k <= n) {if(n/k == round(n/k)) {n = n/k; document.write(k," ")} else k=k+1 } document.write("<br>") n=29376 k=2; while(k <= n) {if(n/k == round(n/k)) {n = n/k; document.write(k," ")} else k=k+1 } } </script></pre>
2 2 3 3 3 11 11 2 3 3 3 3 3 11 17 2 2 2 2 2 2 3 3 3 17m.c.d.(13068,90882,29376) = 2*3*3*3 = 54, m.c.m.(13068,90882,29376) = 2*2*2*2*2*2*3*3*3*3*3*11*11*17 = 31990464
<pre><script> with(Math) { x1 = 1; y1 = 2; x2 = 6; y2 = 7 document.write( "distanza tra (",x1,",",y1,") e (",x2,",",y2,") <br>" ) document.write( sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) ) } </script></pre> distanza tra (1,2) e (6,7) 7.0710678118654755Puoi disegnare il triangolo e calcolarne area, lati, angoli con questo script ottenendo questa immagine.
<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( "perimetro del triangolo P1,P2,P3 = ", L1+L2+L3, "<br>" ) document.write( "area del triangolo 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 perimetro del triangolo P1,P2,P3 = 13.688507523218888 area del triangolo P1,P2,P3 = 5.5
<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.8613531161467862Prendo come arrotondamento della soluzione 2.8613531161468. (con software come WolframAlpha troveremmo 2.861353116146786101340213...)
<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) = -6374993La cosa può essere utile per avere una rapida valutazione dell'andamento della funzione. In questo caso prendendo a e b man mano più vicini ci convinceremmo che la funzione ha un andamento /\ tra -10 e 10 e potremmo cercare dove assume il valore massimo. Vediamo come.
<pre><script> with(Math) { function F(x) {return -pow(x,4)-pow(x,3)+7} a = -10; b = 10 for(n=10; n<3000; n=n*2) { for(i=0; i<n; i=i+1) { h = (b-a)/3; y1 = F(a); y2 = F(a+h); y3 = F(a+h+h) if( (y1 < y2) & (y2 < y3) ) {a = a+h} else {b = b-h} } document.writeln("n=",n , " max tra ", a,", ",b, " val= ", y2) } } </script></pre>
n=10 max tra -0.8421819167132386, -0.49535131839658636 val= 7.094270215667542 n=20 max tra -0.7500533542630592, -0.7499490523620422 val= 7.105468749998371 n=40 max tra -0.750000032934137, -0.7500000329247042 val= 7.105468749999999 n=80 max tra -0.750000032934137, -0.7500000329341369 val= 7.105468749999999 n=160 max tra -0.750000032934137, -0.7500000329341369 val= 7.105468749999999 n=320 max tra -0.750000032934137, -0.7500000329341369 val= 7.105468749999999 n=640 max tra -0.750000032934137, -0.7500000329341369 val= 7.105468749999999 n=1280 max tra -0.750000032934137, -0.7500000329341369 val= 7.105468749999999 n=2560 max tra -0.750000032934137, -0.7500000329341369 val= 7.105468749999999Intuiamo che il valore esatto del punto in cui si ha il massimo è -0.75 = -3/4 e che qui F vale 7.10546875. I valori "strani" sulle ultime cifre sono dovuti alle approssimazioni fatte dal programma. In ogni caso, in ogni problema reale, si deve arrotondare ad un numero relativamente piccolo di cifre (con WolframAlpha troveremmo esattamente -3/4 e 1819/256).
<pre><script> with(Math) { function F(x) {return 7*x*x - x/3 + 5} a = -10; b = 10 for(n=10; n<3000; n=n*2) { for(i=0; i<n; i=i+1) { h = (b-a)/3; y1 = F(a); y2 = F(a+h); y3 = F(a+h+h) if( (y1 > y2) & (y2 > y3) ) {a = a+h} else {b = b-h} } document.writeln("n=",n , " min tra ", a,", ",b, " val= ", y2) } } </script></pre>
n=10 min tra -0.21998679063150905, 0.12684380768514317 val= 5.030706157217319 n=20 min tra 0.0237545988945348, 0.02385890079555173 val= 4.9960317671489705 n=40 min tra 0.023809512484711453, 0.02380951249414428 val= 4.996031746031747 n=80 min tra 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747 n=160 min tra 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747 n=320 min tra 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747 n=640 min tra 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747 n=1280 min tra 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747 n=2560 min tra 0.023809512484711453, 0.023809512484711456 val= 4.996031746031747Il minimo è in 0.0238095124847 dove F vale 4.99603174603 (con WolframAlpha troveremmo 1/42 e 1259/252)
<pre><script> with(Math) { function F(x) {return pow(x*sin(x)+sqrt(x)-x*x,3)-pow(x,4) } a = 0; b = 0.8 for(n=10; n<3000; n=n*2) { for(i=0; i<n; i=i+1) { h = (b-a)/3; y1 = F(a); y2 = F(a+h); y3 = F(a+h+h) if( (y1 < y2) & (y2 <y3) ) {a = a+h} else {b = b-h} } document.writeln("n=",n , " max tra ", a,", ",b, " val= ", y2) } } </script></pre>
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.29800934618124975Potrei prendere x = 0.5993273, y = 0.29800934618125, ma, se il problema è "pratico", è sufficiente prendere x = 0.5993, y = 0.2980 (con Geogebra avrei invece ottenuto x = 0.6, y = 0.3!!! Con WolframAlpha avrei ottenuto correttamente x ≈ 0.599327, y ≈ 0.298009).
<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)Ho una stima del punto in cui la funzione assume il valore minimo e una stima di questo valore: 0.023809523809523725 invece di 0.0238095124847, 4.996031746031746 invece di 4.99603174603.
<pre><script> with(Math) { n=25; k=15 c=1; for(i=0; i<k; i=i+1) { c=c*(n-i)/(k-i) } document.write("C("+n+","+k+") = "+round(c)) } </script></pre> C(25,15) = 3268760 Più di 3 milioni!
<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, " rettangoli, ∫[a,b]F = ", s*h); n=n*2 } } </script></pre>
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5 10 rettangoli, ∫[a,b]F = 122.56000000000004 20 rettangoli, ∫[a,b]F = 122.64000000000001 40 rettangoli, ∫[a,b]F = 122.66000000000003 80 rettangoli, ∫[a,b]F = 122.66500000000003 160 rettangoli, ∫[a,b]F = 122.66624999999999Aumentiamo n, mettendo n=10000 al posto di n=10:
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5 10000 rettangoli, ∫[a,b]F = 122.66666655999998 20000 rettangoli, ∫[a,b]F = 122.66666663999993 40000 rettangoli, ∫[a,b]F = 122.66666665999948 80000 rettangoli, ∫[a,b]F = 122.6666666649996 160000 rettangoli, ∫[a,b]F = 122.66666666625022Deduco che ∫[a,b]F = 122.666 = 122+2/3 = 368/3
F(x) = abs(x*(x-2)); a = 0; b = 3 10 rettangoli, ∫[a,b]F = 2.6550000000000002 20 rettangoli, ∫[a,b]F = 2.6634374999999997 ... 81920 rettangoli, ∫[a,b]F = 2.6666666664804004 163840 rettangoli, ∫[a,b]F = 2.6666666666201007Deduco che ∫[0,3] |x·(x−2)| dx = 2+2/3 = 8/3 (con Geogebra avrei ottenuto 2.67!!! Per integrazione, ricerca di massimi/minimi, flessi, ... non è certo un software affidabile ...)
<pre><script> with(Math) { P = [5,10]; n = 4 x = [12, 17, 24, 33]; dx = [1, 1, 1, 1.5] y = [18, 27, 31, 45]; dy = [2, 2, 2.5, 3] pend1 = (y[0]-dy[0]-P[1])/(x[0]+dx[0]-P[0]) for(i=1; i<n; i=i+1) {p = (y[i]-dy[i]-P[1])/(x[i]+dx[i]-P[0]); if(p > pend1) pend1 = p} pend2 = (y[0]+dy[0]-P[1])/(x[0]-dx[0]-P[0]) for(i=1; i<n; i=i+1) {p = (y[i]+dy[i]-P[1])/(x[i]-dx[i]-P[0]); if(p < pend2) pend2 = p} document.write( P[1] + " + (x-" + P[0] + ") * " +pend1 + " <= y <<= " + P[1] + " + (x-" + P[0] + ") * " +pend2 ) } </script></pre>
10 + (x-5) * 1.1538461538461537 <= y <= 10 + (x-5) * 1.3055555555555556 che posso arrotondare a 10 + (x-5) * 1.154 <= y <= 10 + (x-5) * 1.306