You are on page 1of 21

Introduzione alluso di Mathematica

10
Prima
Sqrt[x ^ 2]
x2

100 !
93 326 215 443 944 152 681 699 238 856 266 700 490 715 968 264 381 621 468 592 963 895 217 599
993 229 915 608 941 463 976 156 518 286 253 697 920 827 223 758 251 185 210 916 864 000 000
000 000 000 000 000 000

N[%]
9.33262 10157

Gamma[4]
6

- 1 2 !

Gamma1 2

Integrate[Exp[- t] / Sqrt[t], {t, 0, + }]


Sqrt[2]
2

N[%, 500]
1.4142135623730950488016887242096980785696718753769480731766797379907324784621
07038850387534327641572735013846230912297024924836055850737212644121497099935
83141322266592750559275579995050115278206057147010955997160597027453459686201
47285174186408891986095523292304843087143214508397626036279952514079896872533
96546331808829640620615258352395054745750287759961729835575220337531857011354
37460340849884716038689997069900481503054402779031645424782306849293691862158
05784631115966687130130156185689872372
2 PrimaStud.nb

Integrate[Sin[x], {x, 0, }]
2

A questo punto stato spiegato luso dei quattro tipi di parentesi { }, ( ), [ ], [[ ]], e dei tre tipi di
uguale =, :=, ==,

x + 10 x + 1 x - 3 (x + 7)
(1 + x) (10 + x)
(- 3 + x) (7 + x)

x + 1 x + 2 x + 3 (x + 4)
(1 + x) (2 + x)
(3 + x) (4 + x)

Expand[%]
2 3x x2
+ +
(3 + x) (4 + x) (3 + x) (4 + x) (3 + x) (4 + x)

Together[%]
2 + 3 x + x2
(3 + x) (4 + x)

Denominator[%]
(3 + x) (4 + x)

Expand[%]
12 + 7 x + x2

Apart[%]
2 6
1+ -
3+x 4+x

Cenni di Algebra Lineare

v = {1, 2, 3}
{1, 2, 3}

In[1]:= M = {{4, 1, 2}, {- 1, 6, 3}, {2, 1, 8}}


Out[1]= {{4, 1, 2}, {- 1, 6, 3}, {2, 1, 8}}

In[2]:= MatrixForm[%]
Out[2]//MatrixForm=
4 1 2
-1 6 3
2 1 8

In[3]:= Det[%]
Out[3]= 168

M.v
{12, 20, 28}
PrimaStud.nb 3

v[[3]]
3

M[[3]]
{2, 1, 8}

M[[2, 3]]
3

M = {{5, 1, - 2}, {2, 4, - 3}, {1, 2, 6}}


{{5, 1, - 2}, {2, 4, - 3}, {1, 2, 6}}

MatrixForm[%]
5 1 -2
2 4 -3
1 2 6

Det[%]
135

M.{x, y, z}

{5 x + y - 2 z, 2 x + 4 y - 3 z, x + 2 y + 6 z}

b = {1, 2, 3}
{1, 2, 3}

LinearSolve[M, b]
5 82 4
, ,
27 135 15

s=%
5 82 4
, ,
27 135 15

M.s
{1, 2, 3}

s.s
1729
3645

b.b
14

s.M
65 85 16
, ,-
27 27 27

M
{{5, 1, - 2}, {2, 4, - 3}, {1, 2, 6}}
4 PrimaStud.nb

M[[{1, 3}]]
{{5, 1, - 2}, {1, 2, 6}}

Solve[a x ^ 2 + b x + c 0, x]
-b - b2 - 4 a c -b + b2 - 4 a c
x , x
2a 2a

Solve[x ^ 3 + 2 x + 10 0, x]
1/3
- 45 + 2049 2
x - ,
32/3 3 - 45 + 2049
1/3

1/3
1 + 3 - 45 + 2049 1- 3
x - + ,
2 32/3 3 - 45 + 2049
1/3

1/3
1 - 3 - 45 + 2049 1+ 3
x - +
2 32/3 3 - 45 + 2049
1/3

N[%]
{{x - 1.84742}, {x 0.92371 - 2.13535 }, {x 0.92371 + 2.13535 }}

Esempio di definizione di una funzione: uso di x_

lungh[_, a_, b_] := Integrate[Norm[D[, t]], {t, a, b}]

= {t, t ^ 2}
t, t2
PrimaStud.nb 5

ParametricPlot[, {t, 0, 3}]

0.5 1.0 1.5 2.0 2.5 3.0

lungh[, 0, 3]
1
6 37 + ArcSinh[6]
4

N[%]
9.74709

= {Cos[t], Sin[t], t / 4}
t
Cos[t], Sin[t],
4

lungh[, 0, 2 ]
17
2
6 PrimaStud.nb

ParametricPlot3D[, {t, 0, 2 }]

Plot[x ^ 2, {x, 0, 3}, AspectRatio Automatic]

0.5 1.0 1.5 2.0 2.5 3.0


PrimaStud.nb 7

Plot[{Sin[x], Sin[2 x], Sin[3 x], Sin[4 x]}, {x, 0, 2 }, AspectRatio Automatic]
1.0

0.5

1 2 3 4 5 6

-0.5

-1.0

Seconda
Il comando Table[ ] per generare vettori, matrici e molto altro !

Table[2 ^ h, {h, 0, 10}]


{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}

(* Tavola pitagorica *)

Table[h k, {h, 1, 10}, {k, 1, 10}] // MatrixForm


1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

(* Matrice di Hilbert *)

Table1 h + k - 1, {h, 1, 10}, {k, 1, 10} // MatrixForm


1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 1 1
2 3 4 5 6 7 8 9 10 11
1 1 1 1 1 1 1 1 1 1
3 4 5 6 7 8 9 10 11 12
1 1 1 1 1 1 1 1 1 1
4 5 6 7 8 9 10 11 12 13
1 1 1 1 1 1 1 1 1 1
5 6 7 8 9 10 11 12 13 14
1 1 1 1 1 1 1 1 1 1
6 7 8 9 10 11 12 13 14 15
1 1 1 1 1 1 1 1 1 1
7 8 9 10 11 12 13 14 15 16
1 1 1 1 1 1 1 1 1 1
8 9 10 11 12 13 14 15 16 17
1 1 1 1 1 1 1 1 1 1
9 10 11 12 13 14 15 16 17 18
1 1 1 1 1 1 1 1 1 1
10 11 12 13 14 15 16 17 18 19

Det[%]
1 / 46 206 893 947 914 691 316 295 628 839 036 278 726 983 680 000 000 000

N[%]
2.16418 10-53
8 PrimaStud.nb

Definizione e studio di una FUNZIONE di una variabile.

In[4]:= f[x_] = x ^ 3 x ^ 2 - 1
x3
Out[4]=
- 1 + x2

T verr definito solo dopo, si tratta del Polinomio di Taylor.

In[5]:= Plot[{f[x], x, T}, {x, - 2, 2}, AspectRatio Automatic, PlotRange {- 3, 3}]


3

Out[5]=
-2 -1 1 2

-1

-2

-3

Limit[f[x], x 1, Direction 1]
-

Limit[f[x] / x, x + ]
1

Limit[f[x] - x, x + ]
0

fp = D[f[x], x]
2 x4 3 x2
- +
- 1 + x2 2 - 1 + x2

Together[%]
- 3 x2 + x4
- 1 + x2 2

Solve[fp == 0, x]
{x 0}, {x 0}, x - 3 , x 3
PrimaStud.nb 9

f[x] /. %
3 3 3 3
0, 0, - ,
2 2

Reduce[fp 0, x]
- 3 x < - 1 || - 1 < x < 1 || 1 < x 3

fs = D[fp, x]
8 x5 14 x3 6x
- +
- 1 + x 2 3 - 1 + x 2 2 - 1 + x2

Together[%]
2 3 x + x3
- 1 + x2 3

Reduce[fs > 0, x]
- 1 < x < 0 || x > 1

Reduce[fs < 0, x]
x < - 1 || 0 < x < 1

In[6]:= Series[f[x], {x, 0, 8}]


Out[6]= - x3 - x5 - x7 + O[x]9

In[7]:= T = Normal[%]
Out[7]= - x3 - x5 - x7

Ritornare al grafico della funzione, per rappresentare anche T

In[8]:= Sum1 k ^ 2, {k, 1, }


2
Out[8]=
6

In[9]:= Sum1 n ^ 2, {n, 1, , 2}


2
Out[9]=
8

In[10]:= Sum1 n ^ 2, {n, 2, , 2}


2
Out[10]=
24

In[11]:= %%% %% + %
Out[11]= True

Sum1 k ^ 3, {k, 1, }
Zeta[3]

N[%]
1.08232
10 PrimaStud.nb

(* Grafico del modulo della funzione Zeta di Riemann *)

Plot3D[Abs[Zeta[x + I y]], {x, - 16, 16}, {y, - 16, 16}, PlotRange {0, 16}]

(* Grafico del modulo della funzione Zeta lungo la retta critica Re[z]12 *)

In[12]:= Plot[Abs[Zeta[0.5 + I y]], {y, - 125, 125}, PlotRange {- 1, 6}, PlotPoints 200]
6

3
Out[12]=

-100 -50 50 100

-1

Terza
Table[Series[Sin[x], {x, 0, n}], {n, 1, 11, 2}]
x3 x3 x5
x + O[x]2 , x - + O[x]4 , x - + + O[x]6 ,
6 6 120
x3 x5 x7 x3 x5 x7 x9
x- + - + O[x]8 , x - + - + + O[x]10 ,
6 120 5040 6 120 5040 362 880
x3 x5 x7 x9 x11
x- + - + - + O[x]12
6 120 5040 362 880 39 916 800

Normal[%]
x3 x3 x5 x3 x5 x7
x, x - , x- + , , x- + -
6 6 120
120 5040 6
x3 x5 x7 x9 x3 x5 x7 x9 x11
x- + - + , x- + - + -
6 120 5040 362 880 6 120 5040 362 880 39 916 800

T=%
x3 x3 x5 x3 x5 x7
x, x - , x- + , , x- + -
6 6 120
120 5040 6
x3 x5 x7 x9 x3 x5 x7 x9 x11
x- + - + , x- + - + -
6 120 5040 362 880 6 120 5040 362 880 39 916 800
PrimaStud.nb 11

Plot[{Sin[x], T}, {x, - 2 , 2 }]

-6 -4 -2 2 4 6

-2

-4

Integratex + 2 x ^ 3 + 2 x ^ 2 + 5 x, x
3 1+x 2 Log[x] 1
ArcTan + - Log5 + 2 x + x2
10 2 5 5

Apartx + 2 x ^ 3 + 2 x ^ 2 + 5 x
2 1-2x
+
5x 5 5 + 2 x + x2

Integratex + 2 x ^ 3 + 2 x ^ 2 + 5 x, {x, 1, 3}
1 324
- 3 + 4 3 ArcTan[2] + Log
40 25

N[%]
0.352712

Integratex + 2 x ^ 3 + 2 x ^ 2 + 5 x, {x, 1, }
3
( + Log[256])
40

N[%]
0.651508

Plot[Sin[x], {x, 0, 4 }, Filling Axis]


1.0

0.5

2 4 6 8 10 12

-0.5

-1.0
12 PrimaStud.nb

D[Sin[x], {x, 4}]


Sin[x]

Definizione e studio di una ESPRESSIONE di due variabili.

In[13]:= f = x^2 - y^2


Out[13]= x2 - y2

Plot3D[f, {x, - 1, 1}, {y, - 1, 1}]

fx = D[f, x]
1+2x

fy = D[f, y]
-2 - 2 y

Solve[{fx 0, fy 0}, {x, y}]


1
x - , y - 1
2

{{D[f, x, x], D[f, x, y]}, {D[f, x, y], D[f, y, y]}}


{{2, 0}, {0, - 2}}

MatrixForm[%]
2 0
0 -2

Maximize[{f, - 1 x 1, - 1 y 1}, {x, y}]


{3, {x 1, y - 1}}

Minimize[{f, - 1 x 1, - 1 y 1}, {x, y}]


13 1
- , x - , y 1
4 2

g = Plot3D[f, {x, - 2, 2}, {y, - 2, 2},


RegionFunction Function[{x, y, z}, x ^ 2 + y ^ 2 + z ^ 2 4], AspectRatio 1]

h = Graphics3D[{Opacity[0.5], Sphere[{0, 0, 0}, 2]}]

Show[{h, g}]

Quarta
Definizione e studio di una FUNZIONE di due variabili (dopo aver liberato il simbolo f ) .

In[15]:= f =.

In[16]:= f[x_, y_] = x ^ 2 - y ^ 2


Out[16]= x2 - y2

In[22]:= Plot3D[{f[x, y], p, q}, {x, - 2, 2}, {y, - 2, 2}, PlotStyle {Red, Blue, Green}]
PrimaStud.nb 13

In[18]:= fx[x_, y_] = D[f[x, y], x]


Out[18]= 2x

In[19]:= fy[x_, y_] = D[f[x, y], y]


Out[19]= -2 y

In[20]:= p = f[1, 0] + fx[1, 0] x - 1 + fy[1, 0] y


Out[20]= 1 + 2 (- 1 + x)

In[21]:= q = f[1, - 1] + fx[1, - 1] x - 1 + fy[1, - 1] y + 1


Out[21]= 2 (- 1 + x) + 2 (1 + y)

Ritornare al grafico precedente per vedere due piani tangenti

ContourPlot[f[x, y] 0, {x, - 2, 2}, {y, - 2, 2}]

DensityPlot[f[x, y], {x, - 2, 2}, {y, - 2, 2}]

DensityPlot[Sin[2 x] Sin[2 y], {x, 0, 6 }, {y, 0, 6 }]

DensityPlot[4 Sin[x] Sin[y], {x, 0, 20 }, {y, 0, 20 }]

Animazioni (si pu usare Animate[] per far partire subito lanimazione)

In[25]:= Manipulate[Series[Sin[x], {x, 0, n}], {n, 1, 11, 2}]

In[26]:= taylor[n_, x_] := Normal[Series[Sin[x], {x, 0, n}]]

In[27]:= Plot[{Sin[x], taylor[5, x] // Evaluate}, {x, 0, }]

In[28]:= a = Plot[t, {x, 0, 2 }, PlotRange {{0, 2 }, {- 4, 4}}, AspectRatio Automatic]

In[30]:= Animate[
Show[a, Plot[{Sin[x], taylor[n, x] // Evaluate}, {x, 0, 2 }]], {n, 1, 13, 2}]

In[37]:= Animate[Plot[{Sin[4 2 x - 2 t]}, {x, 0, 2}, PlotRange {- 1.2, 1.2}], {t, 1, 10}]

In[38]:= a = ParametricPlot[{Cos[], Sin[]}, {, 0, 2 }]

In[39]:= Animate[Show[{a, Graphics[{Red, PointSize[0.02], Point[{Cos[t], Sin[t]}],


Line[{{0, 0}, {Cos[t], Sin[t]}}], Blue, Point[{Cos[t], 0}],
Dashed, Line[{{Cos[t], 0}, {Cos[t], Sin[t]}}]}]}], {t, 0, 2 }]

In[40]:= a = Plot3D[t, {x, 0, }, {y, 0, }, PlotRange {- 1.2, 1.2}]

In[42]:= Animate[Show[{a, Plot3D[Sin[x] Sin[y] Sin[t], {x, 0, }, {y, 0, },


PerformanceGoal "Quality", ColorFunction Hue]}], {t, 0, 2 }]

In[43]:= Animate[Show[{a, Plot3D[Sin[2 x] Sin[2 y] Sin[t], {x, 0, }, {y, 0, },


PerformanceGoal "Quality", ColorFunction Hue]}], {t, 0, 2 }]
14 PrimaStud.nb

Quinta
In[48]:= sol = DSolve[{y ''[t] - y[t], y[0] 2, y '[0] 0}, y[t], t]
Out[48]= {{y[t] 2 Cos[t]}}

In[49]:= r[t_] = y[t] /. sol[[1]]


Out[49]= 2 Cos[t]

In[50]:= Plot[r[t], {t, 0, 40}, PlotRange All]

In[51]:= sol = DSolvey ''[t] - y '[t] 10 - y[t], y[0] 2, y '[0] 0, y[t], t


2 399 t 399 t
Out[51]= y[t] -t/20 399 Cos + 399 Sin
399 20 20

In[52]:= s[t_] = y[t] /. sol[[1]]


2 399 t 399 t
Out[52]= -t/20 399 Cos + 399 Sin
399 20 20

In[53]:= Plot[s[t], {t, 0, 40}, PlotRange All]


2

Out[53]=

10 20 30 40

-1

In[55]:= a = Plot[t, {x, - 2.2, 2.2},


PlotRange {{- 2.2, 2.2}, {- 0.4, 0.4}}, AspectRatio Automatic]

In[58]:= Animate[
Show[{a, Graphics[{Red, PointSize[0.02], Point[{r[t], 0}]}]}], {t, 0.01, 80}]

In[57]:= Animate[
Show[{a, Graphics[{Red, PointSize[0.02], Point[{s[t], 0}]}]}], {t, 0.01, 80}]

Sesta
In[59]:= fib[0] = 1
Out[59]= 1

In[60]:= fib[1] = 1
Out[60]= 1
PrimaStud.nb 15

In[61]:= fib[n_] := fib[n] = fib[n - 1] + fib[n - 2]

In[62]:= Table[fib[n], {n, 0, 100}]

In[73]:= fib[4000] // Timing


Out[73]= {0.,
64 574 884 490 948 173 531 376 949 015 369 595 644 413 900 640 151 342 708 407 577 598 177
210 359 034 088 914 449 477 807 287 241 743 760 741 523 783 818 897 499 227 009 742 183 152
482 019 062 763 550 798 743 704 275 106 856 470 216 307 593 623 057 388 506 776 767 202 069
670 477 506 088 895 294 300 509 291 166 023 947 866 841 763 853 953 813 982 281 703 936 665
369 922 709 095 308 006 821 399 524 780 721 049 955 829 191 407 029 943 622 087 779 296 459
174 012 610 148 659 520 381 170 452 591 141 331 949 336 080 577 141 708 645 783 606 636 081
941 915 217 355 115 810 993 973 945 783 493 983 844 592 749 672 661 361 548 061 615 756 595
818 944 317 619 922 097 369 917 676 974 058 206 341 892 088 144 549 337 974 422 952 140 132
621 568 340 701 016 273 422 727 827 762 726 153 066 303 093 052 982 051 757 444 742 428 033
107 522 419 466 219 655 780 413 101 759 505 231 617 222 578 292 486 081 002 391 218 785 189
299 675 757 766 920 269 402 348 733 644 662 725 774 717 740 924 068 828 300 186 439 425 921
761 082 545 463 164 628 807 702 653 752 619 616 157 324 434 040 342 057 336 683 279 284 098
590 801 501}

In[74]:= N[%]
Out[74]= 0., 6.457488449094817 10835

In[75]:= a=1
Out[75]= 1

In[76]:= b=1
Out[76]= 1

In[77]:= fibo[n_] := {Do[{c = a + b, a = b, b = c}, {j, n - 1}], a = 1, b = 1, c}[[4]]

In[83]:= fib[500]
Out[83]= 225 591 516 161 936 330 872 512 695 036 072 072 046 011 324 913 758 190 588 638 866 418 474
627 738 686 883 405 015 987 052 796 968 498 626

In[84]:= fibo[500]
Out[84]= 225 591 516 161 936 330 872 512 695 036 072 072 046 011 324 913 758 190 588 638 866 418 474
627 738 686 883 405 015 987 052 796 968 498 626

In[87]:= Fibonacci[501]
Out[87]= 225 591 516 161 936 330 872 512 695 036 072 072 046 011 324 913 758 190 588 638 866 418 474
627 738 686 883 405 015 987 052 796 968 498 626

In[88]:= FactorInteger[%] // Timing


Out[88]= {67.203125, {{2, 1}, {18 104 700 793, 1},
{9 024 686 010 889 754 273, 1}, {1 966 344 318 693 345 608 565 721, 1},
{351 082 956 611 639 069 560 238 479 712 041 302 626 479 716 454 577, 1}}}
16 PrimaStud.nb

Settima
sol = DSolvey '[t] 1 2 y[t], y[0] 1, y[t], t
y[t] t/2

Plot[y[t] /. sol, {t, - 13, 13}]

sol = DSolve[{y ''[t] + 4 y[t] 0, y[0] 2, y '[0] 0}, y[t], t]


{{y[t] 2 Cos[2 t]}}

Plot[{Sin[2 t], Sin[2 2 t], Sin[2 4 t]}, {t, 0, 1}]

DSolve[{y '[t] - 4 y[t] + y[t] ^ 2 0, y[0] 1}, y[t], t]


Solve::ifun : Inverse functions are being used by Solve, so
some solutions may not be found; use Reduce for complete solution information.
4 4 t
y[t]
3 + 4 t

Expand[%]
4 4 t
y[t]
3 + 4 t

f = y[t] /. %[[1]]
4 4 t
3 + 4 t

Plot[f, {t, - 4, 4}]


4

-4 -2 2 4

DSolve[z[t] z '[t] - 4 z[t] / t + 3 0, z[t], t]


4 z[t]
DSolve3 - + z[t] z [t] 0, z[t], t
t

s = NDSolve[{z[t] z '[t] - 4 z[t] / t + 3 0, z[1] 1}, z, {t, - 1, 3}]

z InterpolatingFunction Domain: {{-1., 3.}}


Output: scalar
PrimaStud.nb 17

Plot[Evaluate[z[t] /. s], {t, - 1, 3}, AxesOrigin {0, 0}]


1.5

1.0

0.5

-1 1 2 3

-0.5

TraditionalFormy '[x] x ^ 2 + y[x] ^ 2 - 1 x ^ 2 + y[x] ^ 2 + 1


x2 + y(x)2 - 1
y (x)
x2 + y(x)2 + 1

In[89]:= s=
NDSolvey '[x] x ^ 2 + y[x] ^ 2 - 1 x ^ 2 + y[x] ^ 2 + 1, y[0] 0.8, y, {x, - 3, 3}

Out[89]= y InterpolatingFunction Domain: {{-3., 3.}}


Output: scalar

In[90]:= Plot[Evaluate[y[x] /. s], {x, - 3, 3}, PlotRange All, AspectRatio Automatic]


2.0

1.5

1.0
Out[90]=

0.5

-3 -2 -1 1 2 3

In[91]:= s =.

In[92]:= s[k_] :=
NDSolvey '[x] x ^ 2 + y[x] ^ 2 - 1 x ^ 2 + y[x] ^ 2 + 1, y[0] k, y, {x, - 3, 3}

In[93]:= Table[s[- 1.0 + h], {h, 0, 2, 0.2}];

In[94]:= g[h_] := Plot[Evaluate[y[x] /. s[h]],


{x, - 3, 3}, PlotRange All, AspectRatio Automatic]

In[95]:= gr = Graphics[{Red, Circle[{0, 0}, 1]}];

In[96]:= Show[Table[g[- 1.4 + h], {h, 0, 2.8, 0.2}], gr]

In[98]:= s[k_] := DSolve[{y ''[x] 2 y '[x] - 5 y[x], y[0] k, y '[0] 0}, y[x], x]

In[99]:= Tables- 12 10 + h, h, 0, 2, 2 10

In[100]:= g[h_] := PlotEvaluate[y[x] /. s[h]], x, - 3, 5 2, AspectRatio Automatic


18 PrimaStud.nb

In[101]:= ShowTableg- 12 10 + h, h, 0, 2, 2 10

-3 -2 -1 1 2
Out[101]=

-2

-4

-6

In[102]:= s := NDSolve[{y[x] y ''[x] - 4 y '[x] + 3 y[x] 0, y[0] 1, y '[0] 1}, y, {x, - 3, 3}]

In[103]:= Plot[Evaluate[y[x] /. s], {x, - 3, 2.8}, PlotRange All, AspectRatio Automatic]

3.0

2.5

2.0

Out[103]=
1.5

1.0

0.5

-3 -2 -1 1 2
PrimaStud.nb 19

Grafica 2-D e 3-D


In[106]:= GraphicsRed, Thickness[0.005], Line[{{0, 0}, {2, 0}, {1, 1}, {0, 0}}],
{RGBColor[0.8, 0.8, 0.1], Polygon[{{2, 2}, {4, 2}, {3, 3}, {2, 2}}]},
Disk[{1, 2}, {0.5, 0.3}], Blue, Thickness[0.01],
Arrow4, 3 2, 0, 3 2, {0, 0}, {1.8, 0.7},
Text["Prova di grafica", {2, 1}]

In[107]:= Graphics3DRed, Thickness[0.005],


Line[{{0, 0, 0}, {2, 0, 1}, {3, 1, 2}, {1, 3, 2}, {0, 0, 0}}],
{Opacity[.3], RGBColor[0.2, 1, 0.2],
Polygon[{{2, 2, 4}, {4, 2, 4}, {3, 3, 6}, {6, 5, 4}, {2, 2, 4}}]},
Opacity[.5], Sphere[{1, 2, 3}, {1}], Blue, Thickness[0.01],
Dashed, Arrow4, 3 2, 1, 0, 3 2, 2, {0, 0, 5},
Text["Prova di grafica", {3, 0.5, 3}]

Un triangolo speciale (Sierpinski)


In[108]:= {L, R, T} = {{0, 0}, {1, 0}, {0.5, 1}}
Out[108]= {{0, 0}, {1, 0}, {0.5, 1}}

In[109]:= sierp[1, L_, T_, R_] := Line[{L, T, R, L}]

In[110]:= sierp[n_, L_, T_, R_] := sierpn - 1, L, (L + T) 2, (L + R) 2,


sierpn - 1, (L + R) 2, R, (T + R) 2, sierpn - 1, (L + T) 2, T, (R + T) 2

In[112]:= Graphics[{Red, sierp[9, L, T, R]}]

Tappeto di Sierpinski
In[113]:= {a, b, c, d} = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};

In[117]:= carpet[1, a_, b_, c_, d_] := Polygon[{a, b, c, d, a}];


20 PrimaStud.nb

In[118]:= carpet[n_, a_, b_, c_, d_] :=


{carpet[n - 1, a, a + (b - a) / 3., a + (c - a) / 3, a + (d - a) / 3],
carpet[n - 1, b + (a - b) / 3., b, b + (c - b) / 3, b + (d - b) / 3],
carpet[n - 1, c + (a - c) / 3., c + (b - c) / 3, c, c + (d - c) / 3],
carpet[n - 1, d + (a - d) / 3., d + (b - d) / 3, d + (c - d) / 3, d],
carpet[n - 1, a + (b - a) / 3., a + 2 (b - a) / 3,
a + (b - a) / 3 + (c - a) / 3, a + (b - a) / 3 + (d - a) / 3],
carpet[n - 1, b + (c - b) / 3., b + 2 (c - b) / 3,
b + 2 (c - b) / 3 + (d - c) / 3, b + (c - b) / 3 + (d - c) / 3],
carpet[n - 1, c + (d - c) / 3., c + 2 (d - c) / 3,
c + 2 (d - c) / 3 + (b - c) / 3, c + (d - c) / 3 + (b - c) / 3],
carpet[n - 1, d + (a - d) / 3., d + 2 (a - d) / 3,
d + 2 (a - d) / 3 + (b - a) / 3, d + (a - d) / 3 + (b - a) / 3]};
In[122]:= Graphics[{Red, carpet[6, a, b, c, d]}]

La scala di Cantor
In[123]:= sC[x_, 1] = Whichx < 1 3, 3 x 2, 1 3 < x < 2 3, 1 2, 2 3 < x, 3 x - 2 3 2 + 1 2;

In[124]:= sC[x_, n_] := Whichx < 1 3, sC[3 x, n - 1] 2,


1 3 < x < 2 3, 1 2, 2 3 < x, sC3 x - 2 3, n - 1 2 + 1 2;

In[125]:= Plot[sC[x, 7], {x, 0, 1}, PlotRange {0, 1}, PlotPoints 3 ^ 6,


AspectRatio Automatic, PlotStyle {Thickness[0.005], Red}]

In[126]:= g[x_] := sC[x, 7];

In[127]:= Plot3D[g[x] g[y], {x, 0, 1}, {y, 0, 1}, PlotRange {0, 1},
PlotPoints 3 ^ 4, AspectRatio Automatic, ColorFunction Hue]

In[128]:= ContourPlot[g[x] g[y], {x, 0, 1}, {y, 0, 1}]

La Cicloide
In[129]:= a = ParametricPlot[{t - Sin[t], 1 - Cos[t]}, {t, - 2, 2 + 2}]
2.0

1.5

Out[129]= 1.0

0.5

2 4 6

In[130]:= Animate[Show[a, Graphics[{Line[{{- 2, 1}, {8, 1}}], Red,


PointSize[0.02], Point[{s, 1}], Point[{s - Sin[s], 1 - Cos[s]}], Black,
Circle[{s, 1}, 1], Line[{{s, 1}, {s - Sin[s], 1 - Cos[s]}}]}]], {s, 0, 2 }]
PrimaStud.nb 21

Ottava
Suoni
In[131]:= Play[Sin[2 440 t], {t, 0, 1}]

In[132]:= Play[Sin[2 880 t], {t, 0, 1}]

In[133]:= Sound[{Play[Sin[2 440 t], {t, 0, 1}], Play[Sin[2 880 t], {t, 0, 1}]}]

In[136]:= PlaySin2 440 1 + t ^ 3, {t, 0, 2}

Scala cromatica temperata


In[137]:= q = 2 ^ 1.0 12
Out[137]= 1.05946

In[140]:= SoundTablePlaySin2 440 q ^ - 9 + h t // Evaluate, {t, 0, 0.5}, {h, 0, 11}

Le sette note
In[141]:= settenote = {0, 2, 4, 5, 7, 9, 11}
Out[141]= {0, 2, 4, 5, 7, 9, 11}

In[143]:= Sound
TablePlaySin2 440 q ^ - 9 + h t // Evaluate, {t, 0, 0.5}, {h, settenote}

Le sette note al pianoforte


In[144]:= Sound[{SoundNote["C"], SoundNote["D"], SoundNote["E"],
SoundNote["F"], SoundNote["G"], SoundNote["A"], SoundNote["B"]}]

You might also like