# Sesión 2

Miguel Piñar, 20/09/2011

## Listas (y 2)

makelist(k^2,k,1,10,1);

makelist(k^2,k,10,1,-1);

makelist(k^2,k,[1,2,3,5,7]);

a:makelist(k^2,k,1,10,1);

a[2];

a[5]:x;

a;

b:a;

b;

b[9]:y;

b;

a;

c:copylist(a);

f(x);

rango(n):=makelist(k,k,1,n);

rango(10);

f(rango(10));

map(f,rango(10));

m:[[1,2,3],[4,5,6]];

first(m);

map(first,m);

apply("+",rango(10));

apply("+",rango(10)^2);

fact(n):=apply("*",rango(n));

fact(10);

fact(3.7);

fact(-3);

(-3.7)!;

Teresa E. Pérez

## Matrices

m:[[1,2,3],[4,5,6],[1,0,-1]];

m[2,3];

m[2][3];

determinant(m);

mat:genmatrix(lambda([i,j], i-j), 3, 3);

determinant(mat);

mat2:genmatrix(s, 3, 3);

mat2[1,2]:6;

mat2;

mat3:matrix([2,-1,0],[9,7,6]);

mat3[2,3];

mat3[3,2];

row(mat3,2);

col(mat3,1);

v:matrix([2],[-1],[0]);

w:matrix([2,-1,0]);

mat3;

length(mat3);

matrix_size(mat3);

matrix_size(mat2);

mat3[1];

col(mat3,1);

lista:transpose(mat3)[1];

lista[1];

apply("+",lista);

lis2:[8,7];

lista-lis2;

listp(lista);

listp(col(mat3,1));

listp(row(mat3,1));

listp(mat3[1]);

matrixp(row(mat3,1));

lista;

row(mat3,1);

[1,2,3]-row(mat3,1);

Copia de matrices

mat3;

B:mat3;

B[1,2]:3;

B;

mat3;

c:copymatrix(mat3);

c[1,2]:1;

c;

mat3;

Multiplicación de matrices y/o vectores

### Listas

kill(all);

v:[1,2,3];

3*v;

v*3;

v-2*v;

A:matrix([2,-1,1],[9,7,6]);

3*A;

A*3;

A-2*A;

v^(-1);

A^(-1);

1/v;

1/A;

/*Se trata de "map"*/;

f(x):=sin(%pi*x/3);

f(v);

map(f,v);

f(A);

map(f,A);

v;

v*v;

A*A;

A;

A.v;

v.A;

w:[-1,2];

w.A;

z:matrix([-1],[0],[1/2]);

A.z;

y:matrix([8],[5]);

y.A;

v;

v.v;

y.y;

A;

A^2;

A.A;

v;

w;

y;

z;

v.v;

y.w;

y.y;

y.transpose(y);

B:transpose(v).v;

v;

listp(v);

listp(transpose(v));

determinant(B);

B[1,3]:-1;

B;

determinant(B);

B[3,1]:-1;

determinant(B);

B;

invert(%);

B^(-1)/*Aplica "map"*/;

B^^(-1)/*calcula la inversa*/;

M: 
genmatrix(lambda([i,j], if i=j then 2 else 1), 
4, 4);

genmatrix(lambda([i,j], if i=j then 2 
        else if i<j then 
        1 else 0), 3, 3);

L:genmatrix(lambda([i,j], if i=j then 2 
        else if i-j=1 then 
        -1 else if i-j=-1 then 1 else 0), 4, 4);

determinant(L);

matrix_size(L);

invert(L);

L.L;

L^^2;

L.L.L;

L^^3;

mat_trace(L);

adjoint(L);

ident(n):=genmatrix(
    lambda([i,j], if i=j 
    then 1 else 0), n, n);

ident(3);

diagmatrix(4,1);

diag_matrix(1,2,3,4);

L;

determinant(L-x*diagmatrix(4,1));

charpoly(L, x);

charpoly(L, x), expand;

L;

echelon(L);

triangularize(L);

eigenvalues(L);

eigenvectors(L);

p(x):=charpoly(L,x);

p(x);

solve(p(x)=0);

kill(y);

solve(y^2-1=0);

solve(y*x=1,[x,y]);

solve([y-x=1,x+2*y=0],[x,y]);

rank(L);

zeromatrix(5,4);

diagmatrix(5,0);

## Ecuaciones y sistemas de ecuaciones

kill(all);

solve(a*x^2+b*x+c=0,x);

solve(a*x^2+b*x+c=0,c);

solve(a*x^2+b*x+c=0);

solve(x+y=3,[x,y]);

solve(x+y-z=9,[x,y,z]);

solve([x+y=0,x-y=1],[x,y]);

solve([x+y=0,x+y=1],[x,y]);

eq1:x^2-1=0;

solve(eq1);

sis:[x+y=1,x-y=5]$
inc:[x,y]$
solve(sis,inc);

sis:[x+y=1,x-y=5];
inc:[x,y];
solve(sis,inc);

(sis:[x+y=1,x-y=5],
inc:[x,y],
solve(sis,inc));

eqn: (1+2*x)^3=13*(1+x^5);

allroots(eqn);

solve(eqn,x);

eqn1: (1+2*x)^3=13.5*(1+x^5);

allroots(eqn1);

solve(eqn1,x);

%,numer;

solve([x-y+z=1,
       2*x-y+0.5*z=2.5,
       3*x-0.7*y+z=6.4],[x,y,z]);

linsolve([x-y+z=1,
       2*x-y+0.5*z=2.5,
       3*x-0.7*y+z=6.4],[x,y,z]);

keepfloat:true;

sol:solve([x-y+z=1,
       2*x-y+0.5*z=2.5,
       3*x-0.7*y+z=6.4],[x,y,z]);

sol,numer;

linsolve([x-y+z=1,
       2*x-y+0.5*z=2.5,
       3*x-0.7*y+z=6.4],[x,y,z]);

Cálculo del polinomio de interpolación usando sistemas 
de ecuaciones

kill(all);

datos:[[-1,2],[0,1],[1,2],[2,5]];

q(x):=a[0]+a[1]*x+a[2]*x^2+a[3]*x^3;

abc:map(first,datos);

ord:map(second,datos);

map(last,datos);

q(abc)=ord;

ecuac:makelist(q(abc[k])=ord[k],k,1,length(datos));

incog:makelist(a[k],k,0,length(datos)-1);

solu:solve(ecuac,incog);

r(x):=subst(solu,q(x));

r(x);

plot2d([[discrete,datos],r(x)], [x,-2,5],
[style,points,lines],
 [plot_format, gnuplot])$

Una ecuación particular

solve(x+exp(x)=0,x);

plot2d([x+exp(x)], [x,-1,0],
 [plot_format, gnuplot])$

load("mnewton")$

mnewton(x+exp(x),x,0);

solve(x^2-2=0,x);

solve(x^2-2=0,x),numer;

float(solve(x^2-2=0,x));

mnewton(x^2-2=0,x,2);

mnewton(x^2-2=0,x,-2);

mnewton(x^2-2=0,x,20000);

allroots(x^2-2);