TRABAJANDO CON  MATHEMATICA

Humberto Reyes
Cuaderno de trabajo

Ejemplos

Aritmética

Sumas

4 + 5

Los comandos se ejecutan con shift-enter

(4 + 5) + (4 - 2)

Otras operaciones

Sqrt[16]

?Sq*

4/6 // N

N[4/6, 1000]

Pi // N

N[Pi, 500]

Log[10] // N

Log[10, 5] // N

Log[2, 4]

2*2

2 2

(4 + 3) (2 + 5)

(4 + 3) (2 + 5)/(2 + 15) // N

Exp[2] // N

4^5

?Tan*

Tanh[2] // N

x = 15^4

x

10  x

x^2

Clear[x]

x

(Sqrt[2]) // N

Sqrt[x]

Álgebra

((x^2 + 2  y + 3  y + Sqrt[x + y])/
          x) + (x + y)^2

Simplify[%]

Solve[x + 10 == 1, x]

Solve[x + y == 1, x + 2  y == 3, {x, y}]

Solve[{x + y == 1, x + 2 y == 3}, {x, y}]

?Solve

Cálculo

Diferencial

D[2  x^2 + 2  x + 2, x]

D[D[2  x^2 + 2  x + 2, x], x]

D[Log[x], x]

Integral

Integrate[2 + 4  x, x]

Integral definida

Integrate[2 + 4  x, {x, 0, 1}]

expresion = Sin[((Log[x])^(x + 4))/Exp[x]]

Integrate[expresion, x]

Integrate[expresion, {x, 0, 1}]

NIntegrate[expresion, {x, 0, 1}, AccuracyGoal -> 1]

?Acc*

<< Statistics`ContinuousDistributions`

?Nor*

NormalDistribution[u, s]

PDF[NormalDistribution[0, 1], x]

NIntegrate[
      PDF[NormalDistribution[0, 1], x], {x, - .5,  .5}]

CDF[NormalDistribution[0, 1],  .5] - 
      CDF[NormalDistribution[0, 1], - .5]

Matrices

¿Cómo se escribe una matriz?

mat = {{1, 2, 3}, {4, 5, 6}}

MatrixForm[mat]

Operaciones con matrices

?Tra*

Tr[mat]

?Matr*

PseudoInverse[mat]

mat = {{1, 2}, {7, 1}}

Inverse[mat]

mat2 = {{1, 2}, {3, 4}}

mat .  mat2

Transpose[mat]

Transpose[mat] . mat2

Gráficas

Plot[2  x^2, {x, -1, 1}]

Plot[PDF[NormalDistribution[0, 1], x], {x, -3, 3}]

Plot3D[x + y^2, {x, 0, 1}, {y, 0, 2}, 
      ColorFunction -> Hue]

PlotStyle -> {Hue[ .3], Thickness[ .01]}

plot1 = 
      Plot[PDF[NormalDistribution[0, 1], x], {x, -3, 3}, 
        PlotStyle -> {Hue[ .3], Thickness[ .02]}]

plot2 = Plot[2  x^2, {x, -1, 1}]

Show[plot1, plot2]

ListPlot[{1, 1, 2, 3, 5, 8, 13}, PlotJoined -> True]

ListPlot[Transpose[{{0, 2, 3, 6, 7, 9}, {1, 1, 2, 3, 5, 8}}], 
      PlotJoined -> True]

?Cont*

Plot3D[x + y^2, {x, 0, 1}, {y, 0, 2}, 
      ColorFunction -> Hue]

ContourPlot[x + y^2, {x, 0, 1}, {y, 0, 2}, 
      ColorFunction -> Hue]

S = 100 Log[p] + 300 Log[1 - p]

Plot[S, {p, 0, 1}]

Estadística

<< Statistics`DescriptiveStatistics`

?Statistics`DescriptiveStatistics`*

lista = {2, 3, 5, 6.5, 7, 8, 9, 0, 1.2, 3, 4, 5, 6}

Mean[lista]

Variance[lista]

VarianceMLE[lista]

GeometricMean[lista]

Mean[lista] 10

<< Statistics`ContinuousDistributions`

?Statistics`ContinuousDistributions`*

?Norm*

Simulaciones

nor = Table[Random[NormalDistribution[0, 1]], {100}]

Mean[nor]

Variance[nor]

norBig = 
        Table[Random[NormalDistribution[0, 1]], {10000}];

Mean[norBig]

Variance[norBig]

Áreas bajo la curva normal

CDF[NormalDistribution[0, 1], 0]

CDF[NormalDistribution[0, 1], 1.95]

CDF[NormalDistribution[0, 1], 3] // N

1 - CDF[ChiDistribution[1], 8] // N

Plot[PDF[ChiDistribution[1], x], {x, 0, 8}, 
      PlotRange -> All]

Plot[PDF[ChiDistribution[3], x], {x, 0, 8}, 
      PlotRange -> All]

MathGenome

\(<< c:\dres\math\mathgenome.m\)

?Mathgenome`*

Hwmean[ .2, 10, 15, 6]

Plot[Hwmean[x, 10, 15, 6], {x, 0, 1}]

FindMinimum[-Hwmean[x, 10, 15, 6], {x,  .7}]

HWMultAllele[{ .1,  .2,  .3,  .1,  .1,  .2}]

Gametes[{{"A", "a"}, {"B", "b"}, {"c", "c"}, {"\
D", "D"}, {"Bar", "bar"}}]

\(Crossing[{{"A", "a"}, {"B", "b"}, {"c", "c"}, {"D", "D"}, {"Bar", "bar"}}, {{"A", "a"}, {"B", "b"}, {"c", "c"}, {"D", "D"}, {"Bar", "bar"}}]\)

\(Crossing[{{"A", "a"}, {"B", "b"}}, {{"A", "a"}, {"B", "b"}}]\)

ChiTest[{28, 50, 22}, {25, 50, 25}, 2, "n"] // N

IHFun[ .1] // N

Plot[IHFun[x], {x, 0, 3}]

Manejo de archivos

General

x = Table[1, {20}]

Save["c:\courses\dec-547\var.txt", x]

\(\(<< c:\courses\dec-547\var.txt;\)\)

x

x2 = {{1, 2, 3, 4, 5, 6, 7, 8}, {9, 10, 11, 12, 13, 14}}

Save["c:\courses\dec-547\varx.txt", x2]

Export["c:\courses\dec-547\vartab.txt", x2, "Table"]

x2 = {{1, 2, 3, 4, 5, 6, 7, 8}, {9, 10, 11, 12, 13, 14, 15, 
          16}}

variable = 
      Import["c:\courses\dec-547\vartab.txt", "Table"]

variable

?Save

Gráficas

pt1 = Plot[x^2 + 2  x + 1, {x, -1, 1}]

Export["c:\courses\dec-547\grafo.jpg", pt1]

Export["c:\courses\dec-547\grafo.pdf", pt1]

in = Integrate[Log[x - y]/x, x]

Export["c:\courses\dec-547\eq.pdf", in]

Nota: La mejor opción es exportar la página como html, y automáticamente salvará las ecuaciones con formato gig.

Introducción a la programación

HW[AA_, Aa_, 
        aa_] := (A = (2  AA + 
                Aa)/(2 (AA + Aa + aa)); 
a = 
          1 - A; 
{A, a})

HW[20, 30, 10] // N

Save["c:\courses\dec-547\hardy.m", HW]

\(<< c:\courses\dec-547\hardy.m\)

HW[30, 60, 10]

Clear[HW]

\(<< c:\courses\dec-547\hardy.m\)

HW2[60, 10] // N

I // N

Pi

Paréntesis

\(<< c:\dres\math\mathgenome.m\)

?Mathgenome`*

ChiTest[{10, 11, 20, 29}, {9, 12, 21, 28}, 1, "n"] // N

Sqrt[ .28/70]

EQUILIBRIO DE LIGAMIENTO

pt1 = Plot[GamDesT[ .5,  .6, t], {t, 0, 100}]

pt2 = Plot[GamDesT[ .2,  .6, t], {t, 0, 100}]

pt3 = Plot[GamDesT[ .01,  .6, t], {t, 0, 100}]

Show[{pt1, pt2, pt3}, PlotRange -> All]

MÁS DEL MATHEMATICA

Divisors[28]

Expand[(a + b)^2]

Simplify[%]

Números aleatorios

Random[]

Random[Real, {0, 2}]

Random[Integer, {1, 500}]

Table[Random[Integer, {1, 500}], {500}];

listanormal = 
        Table[Random[NormalDistribution[0, 1]], {100}];

Mean[listanormal]

boot1 = 
        Table[listanormal[[Random[
                Integer, {1, 100}]]], {100}];

bota := 
      Table[listanormal[[Random[Integer, {1, 100}]]], {100}]

mediadebota := Mean[bota]

milistaboot = Sort[Table[mediadebota, {100000}]];

milistaboot[[2500]]

milistaboot[[97500]]

CDF[NormalDistribution[0, 1], 1.95]

La desviación estándar teórica de la media estimada es s/Sqrt[n]

sdev = 1/10 // N

Los límites teóricos son: (-.195, .195)

Manejo de listas

Definiendo listas

lista = {1, 2, 3, 4, 5, 6}

lista2 = {"a", "b", "c"}

a = 2

{a, bc}

{"a"}

?Range

Range[20]

Range[3, 10]

Range[0, 1,  .1]

?Table

Table[1, {10}]

Table[2  i, {i, 1, 10}]

Table[{i , j}, {i, 1, 2}, {j, 1, 3}]

Dimensiones de listas

l1 = {1, 3, 5, 7, 8}

Length[l1]

mat = {{1, 2}, {1, 3}, {1, 4}}

Length[mat]

Length[Transpose[mat]]

Dimensions[mat]

Table[Random[], {10}]

\(<< c:\dres\math\mathgenome.m\)

?Random

Random[Integer, {1, 20}]

?RandomP*

RandomPermutation[20]

RandomPartition[20]

Trabajando con elementos de listas

milista = RandomPermutation[20]

Position[milista, 10]

Part[milista, 2]

?Take

Take[milista, 3]

Take[milista, -3]

Take[milista, {3, 4}]

?Sort

Sort[milista]

Sort[{"h", "o", "l", "a", 5}]

?Drop

Drop[milista, 1]

milista

Drop[milista, {5}]

Drop[milista, {5, 6}]

First[milista]

Last[milista]

?Select

Select[milista, EvenQ]

Reverse[milista]

RotateLeft[{"a", "b", "c"}, 2]

?Flatten

mat = {{1, 2}, {3, 4}}

Flatten[mat]

Partition[{"A", "a", "a", "a"}, 2]

milista

Append[milista, "fenotipo"]

Prepend[milista, "fenotipo"]

?Insert

Insert[milista, "fenotipo", 5]

Insert[milista, "mut", Random[Integer, {0, 20}]]

?Drop

mutante = Drop[milista, {Random[Integer, {1, 20}]}]

Length[mutante]

Sort[mutante]

insercion := 
      If[Random[] <  .1, Drop[milista, {Random[Integer, {1, 20}]}], 
        milista]

Table[Length[insercion], {20}]

?ReplacePart

ReplacePart[milista, "mut", 5]

N[Random[], 200000]

?Ber*

Table[Random[BernoulliDistribution[ .1]], {40}]

insercion := 
      If[Random[BernoulliDistribution[ .1]] == 1, 
        Drop[milista, {Random[Integer, {1, 20}]}], milista]

insercion

Table[Length[insercion], {20}]

Trabajando con varias listas

milista2 = {"a", "b"}

Join[milista, milista2]

milista3 = {"b", "c"}

Union[milista2, milista3]

Union[{1, 1, 2}]

Intersection[milista2, milista3]