# Sesión 4

Miguel, 22/09/2011

## Bucle for

for k:1 thru 10 do print(k);

for k:1 step 0.7 thru 10 do print(k);

for k:1 thru 10  step 0.7 do print(k);

for k:1 /*thru 10*/ while k<=10 step 0.7 do print(k);

k:1$ while k<=10 do 
     (
      k:k+1,
      print(k)      
     );

k:1$ unless k>10 do 
     (
      print(k),
      k:k+1      
     );

mod(8,3);

is(mod(8,3)=0);

primep(5);

primop(n):=block([],
                 primo:true,
                 for k:2 thru n/2 do if mod(n,k)=0 then primo:false,  
                 primo
                 );

primop(8);

primop(7);

primop(2);

primop(n):=block([primo,k],
                 primo:true,
                 for k:2 thru n/2 do 
                     if mod(n,k)=0 
                        then (primo:false,return()),  
                 primo
                 );

## Escritura y lectura secuencial de archivos (de datos)

s:openw("fibonacci.txt");

for  k:1 thru 20 do printf(s,"~d~%",fib(k));

close(s);

s:openr("fibonacci.txt");

readline(s);

readline(s);

readline(s);

while stringp(tmp:readline(s)) do print(tmp);

close(s);

s:openr("fibonacci.txt");

flength(s);

fposition(s);

readline(s);

fposition(s);

fposition(s,20);

readline(s);

readline(s);

close(s);

s:openr("fibonacci.txt");

fposition(s,1);

f:[] $ while stringp(tmp:readline(s)) do f:endcons(tmp,f);

f;

close(s);

s:openr("fibonacci.txt");

read_list(s);

fposition(s,1);

read_matrix(s);

m:zeromatrix(5,4);

fposition(s,1);

read_matrix(s,m);

m;

close(s);

# El método de bisección

solve(x+%e^x=0,x);

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

load(mnewton);

mnewton(x+%e^x=0,x,0);

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

f(x):=x+%e^x;

plot2d([f(x)], [x,-2,0],
 [plot_format, gnuplot],
 [gnuplot_preamble, "set zeroaxis;"])$

is (f(-1)*f(0)<0);

is (f(-1)*f(-0.5)<0);

is (f(-1)*f(-0.75)<0);

a:-1.0$
b:0.0$
prec:10^(-16)$
tol:10^(-16)$
if abs(b-a)<tol then return
if f(a)*f(b)<0 then
for k:1 thru 50 do
   (c:(a+b)/2,
    if abs(f(c))<prec then return(c),
    if f(a)*f(c)<0 then b:c else a:c
    )$
    c;

mnewton(x+%e^x,x,0);

a:-1.0$
b:0.0$
prec:10^(-16)$
tol:10^(-16)$
if f(a)*f(b)<0 then
for k:1 thru 25 do
   (c:(a+b)/2,print(c),
    if abs(f(c))<prec then return(c),
    if f(a)*f(c)<0 then b:c else a:c
    )$
    c;

a;

b;

c;

biseccion(f,extremo1,extremo2,
   tolerancia,precision,nmaxiter):=
    block(
        [a,b,c,k,i],
        a:extremo1,
        b:extremo2,
        i:0,
        if abs(f(a))<precision then return([i,a]),
        if abs(f(b))<precision then return([i,b]),
        if f(a)*f(b)>0 then 
        error("Intervalo no adecuado"),
        i:nmaxiter,
        for k:1 thru nmaxiter do
            (
            c:(a+b)/2,print(c),
            if abs(b-a)<tolerancia then (i:k,return()),          
            if abs(f(c))<precision then (i:k,return()),
            if f(a)*f(c)<0 then b:c else a:c
            ),
[i,c]
)$

biseccion(f,-1.0,0.0,10^(-8),10^(-8),100);

biseccion(f,0.0,-1.0,10^(-8),10^(-8),100);

biseccion(f,0.0,10.0,10^(-8),10^(-8),100);

biseccion(x^2-2,1.0,2.0,10^(-8),10^(-8),100);

g(x):=x^2-2;

kill(f);

f(x):=x^2-2;

biseccion(f,1.0,2.0,10^(-8),10^(-8),100);