"<p>Copy and paste the following lines at the top of the page (instead of what is now). Here it will appear <b>10</b> . . . . . . |
Copy and paste the following lines at the top of the page (instead of what is now). Here it will appear 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>Output: 36
<pre><script> with(Math) { document.write( "calculation of <br>(3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3)" ) } </script></pre>Output:
<pre><script> with(Math) { document.write( "calculation of\n(3/4 + 7.5 + 0.25 - 5/2)*(3/2 + 7/10 - 1/2 + 4.3)" ) } </script></pre>
<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>
Outputs: 46 26 360 3.6 4 10 36 3656158440062976(3) The tabulation of a function:
<pre><script> with(Math) { document.write( " x^5 + 6*x^3 + x/7<br><br>" ) x = 1.5 document.write( "x=",x, " -> ",pow(x,5)+6*pow(x,3)+x/7, "<br>" ) x = 2 document.write( "x=",x, " -> ",pow(x,5)+6*pow(x,3)+x/7, "<br>" ) x = 2.5 document.write( "x=",x, " -> ",pow(x,5)+6*pow(x,3)+x/7, "<br>" ) x = 3 document.write( "x=",x, " -> ",pow(x,5)+6*pow(x,3)+x/7, "<br>" ) } </script></pre>Outputs:
x^5 + 6*x^3 + x/7
x=1.5 -> 28.058035714285715
x=2 -> 80.28571428571429
x=2.5 -> 191.76339285714286
x=3 -> 405.42857142857144
(4) Let's see how loops can be performed, i.e. how calculations can be automatically repeated several times. Here is a first example:
<pre><script> with(Math) { for(i=1; i<10; i=i+1) document.write( i*2," " ) } </script></pre>Outputs: 2 4 6 8 10 12 14 16 18
i = 1 | write i*2 < | | i = i + 1 | | | i < 10 ? YES | | 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.4625
<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>Outputs:
the sum of 172,169,183,181,167 is 872 the mean is 872/5 = 174.4 min = 167 max = 183I can sort numbers:
<pre><script> with(Math) { A = [23, 4.1, PI, 3, 5, 12, -3, 1e3, 1e-4] document.write(A,"<br>") 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>Output:
23,4.1,3.141592653589793,3,5,12,-3,1000,0.0001 -3,0.0001,3,3.141592653589793,4.1,5,12,23,1000Note. 1000 and 0.0001 were written 1e3 and 1e-4, i.e. in exponential notation: AeB indicates 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 ("median = ", A[round(n/2)], " 1^ quarter = ", A[round(n/4)], " 3^ quarter = ", A[round(n*3/4)])
S = 0; for(i=0; i<n; i=i+1) {S = S + A[i]}; mean = S/n
document.writeln ("mean = ", mean)
} </script></pre>
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]
<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 can be used to indicate what to do if the condition of an if is false.
<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 17gcd(13068,90882,29376) = 2*3*3*3 = 54, lcm(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( "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.0710678118654755You can draw the triangle and calculate its area, sides, angles with this script getting this image.
<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
<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.8613531161467862I take as rounding of the solution 2.8613531161468. (with software like WolframAlpha we would find 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) = -6374993This 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 and we could look for where it assumes the maximum value. Let's see how.
<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 between ", a,", ",b, " val= ", y2) } } </script></pre>
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.105468749999999We 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).
<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 between ", a,", ",b, " val= ", y2) } } </script></pre>
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.996031746031747The minimum is 0.0238095124847 where F is 4.99603174603 (with WolframAlpha we would find 1/42 and 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.29800934618124975I 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).
<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.
<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 More than 3 million!
<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, ∫[a,b]F = ", s*h); n=n*2 } } </script></pre>
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5 10 rectangles, ∫[a,b]F = 122.56000000000004 20 rectangles, ∫[a,b]F = 122.64000000000001 40 rectangles, ∫[a,b]F = 122.66000000000003 80 rectangles, ∫[a,b]F = 122.66500000000003 160 rectangles, ∫[a,b]F = 122.66624999999999We increase n, putting n=10000 instead of n=10:
F(x) = 2*x*x + 3*x + 1; a = 1; b = 5 10000 rectangles, ∫[a,b]F = 122.66666655999998 20000 rectangles, ∫[a,b]F = 122.66666663999993 40000 rectangles, ∫[a,b]F = 122.66666665999948 80000 rectangles, ∫[a,b]F = 122.6666666649996 160000 rectangles, ∫[a,b]F = 122.66666666625022I assume that ∫[a,b]F = 122.666 = 122+2/3 = 368/3
F(x) = abs(x*(x-2)); a = 0; b = 3 10 rectangles, ∫[a,b]F = 2.6550000000000002 20 rectangles, ∫[a,b]F = 2.6634374999999997 ... 81920 rectangles, ∫[a,b]F = 2.6666666664804004 163840 rectangles, ∫[a,b]F = 2.6666666666201007I 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 )
<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 which I can round to 10 + (x-5) * 1.154 <= y <= 10 + (x-5) * 1.306