Introduction to Matlab in Euler

>p=[-1,-1,1]
[ -1 -1 1 ]
>r=polysolve(p)
[ -0.61803398875+0i 1.61803398875+0i ]
>&solve(1/x=x-1)
1 - sqrt(5) sqrt(5) + 1
[x = -----------, x = -----------]
2 2

>real(r[2])
1.61803398875
>phi=(sqrt(5)+1)/2
1.61803398875
>&bfloat((sqrt(5)+1)/2)
1.6180339887498948482045868343656b0
>function f(x) := 1/x-(x-1)
>plot2d("f",0,4);
>solve("f",1)
1.61803398875
>plot2d(phi,0,>points,>add); insimg;

>function plotgr ...
phi = (1+sqrt(5))/2;
x = [0,phi,phi,0,0];
y = [0,0,1,1,0];
u = [1,1];
v = [0,1];
plot2d(x,y,a=0,b=1.7,c=-0.7,d=1,<frame,<axis,<grid);
plot2d(u,v,style="--",>add);
ctext("phi",toscreen([phi/2,1.1]));
ctext("phi-1",toscreen([(1+phi)/2,-.05]));
ctext("1",toscreen([-.05,.5]));
ctext("1",toscreen([.5,-.05]));
endfunction
>plotgr; insimg(crop=0.7);


>p="1"; loop 1 to 6; p="1+1/("+p+")"; end; p,
1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1))))))
>p()
1.61538461538
>phi-p()
0.00264937336528
>function test(n) ...
p = 1;
q = 1;
for k = 1:n
s = p;
p = p + q;
q = s;
end
return{p,q}
endfunction
>{p,q}=test(6);
>p+"/"+q
21/13
>contfrac((1+sqrt(5))/2,6), contfracval(%), fracprint(%)
[ 1 1 1 1 1 1 1 ]
1.61538461538
21/13
>contfrac(pi,3), api=contfracbest(pi,3), fracprint(api), api-pi
[ 3 7 15 1 ]
3.14159292035
355/113
2.66764189405e-007
>function fibonacci (n) ...
f=zeros(1,n);
f[1] = 1;
f[2] = 2;
for k = 3:n
f[k] = f[k-1] + f[k-2];
end
return f
endfunction
>fibonacci(12)
[ 1 2 3 5 8 13 21 34 55 89 144 233 ]
>sequence("x[n-2]+x[n-1]",[1,2],12)
[ 1 2 3 5 8 13 21 34 55 89 144 233 ]
>function fibnum (n) ...
if n<=1 then return 1
else return fibnum(n-1)+fibnum(n-2)
endfunction
>tic; fibnum(24), toc;
75025
Used 0.858 seconds
>n=40; f=fibonacci(n);
>longestformat; (f[2:n]/f[1:n-1]-phi)', longformat;
0.3819660112501051
-0.1180339887498949
0.04863267791677184
-0.01803398874989481
0.006966011250105098
-0.002649373365279484
0.001013630297724166
-0.0003869299263654646
0.0001478294319232631
-5.646066000730698e-005
2.15668056606777e-005
-8.237676933475768e-006
3.146528619657474e-006
-1.201864648914253e-006
4.590717870289751e-007
-1.753497695933248e-007
6.697765919660981e-008
-2.558318845657936e-008
9.771908393574336e-009
-3.732536946188247e-009
1.425702222945802e-009
-5.445699446937624e-010
2.080071670462758e-010
-7.945177848966978e-011
3.034772433352373e-011
-1.159183860011126e-011
4.427569422205124e-012
-1.691313755713964e-012
6.459277557269161e-013
-2.466915560717098e-013
9.414691248821328e-014
-3.597122599785507e-014
1.376676550535194e-014
-5.329070518200751e-015
1.998401444325282e-015
-8.881784197001252e-016
2.220446049250313e-016
-2.220446049250313e-016
0

>n=1:40; 1/(2*phi-1)*(phi^(n+1)-(1-phi)^(n+1))
[ 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584
4181 6765 10946 17711 28657 46368 75025 121393 196418 317811
514229 832040 1346269 2178309 3524578 5702887 9227465 14930352
24157817 39088169 63245986 102334155 165580141 ]
>function fern (n) ...
p = [ .85, .92, .99, 1.00];
A1 = [ .85, .04; -.04, .85]; b1 = [0; 1.6];
A2 = [ .20, -.26; .23, .22]; b2 = [0; 1.6];
A3 = [-.15, .28; .26, .24]; b3 = [0; .44];
A4 = [ 0, 0 ; 0, .16];
x=[0.5;0.5];
X=zeros(n,2);
r=random(1,n);
loop 1 to n;
if r[#] < p[1]
x = A1.x + b1;
elseif r[#] < p[2]
x = A2.x + b2;
elseif r[#] < p[3]
x = A3.x + b3;
else
x = A4.x;
endif
X[#]=x';
end;
return X;
endfunction
>X=fern(100000)'; ...
>plot2d(X[1],X[2],a=-3,b=3,c=0,d=10, ...
> >points,<frame,<grid,style=".",color=green,>insimg);

>remvalue X;
>shortformat; A=magic(3)
8 1 6
3 5 7
4 9 2
>sum(A)'
[ 15 15 15 ]
>sum(A')'
[ 15 15 15 ]
>sum(diag(A,0))
15
>sum(diag(flipy(A),0))
15
>sum(1:9)/3
15
>for k=0:3; rot(A,k), "", end;
8 1 6
3 5 7
4 9 2
6 7 2
1 5 9
8 3 4
2 9 4
7 5 3
6 1 8
4 3 8
9 5 1
2 7 6
>for k=0:3; rot(A',k), "", end;
8 3 4
1 5 9
6 7 2
4 9 2
3 5 7
8 1 6
2 7 6
9 5 1
4 3 8
6 1 8
7 5 3
2 9 4
>det(A)
-360
>inv(A)
0.147222 -0.144444 0.0638889
-0.0611111 0.0222222 0.105556
-0.0194444 0.188889 -0.102778
>fracprint(inv(A))
53/360 -13/90 23/360
-11/180 1/45 19/180
-7/360 17/90 -37/360
>fracformat; inv(A), longformat;
53/360 -13/90 23/360
-11/180 1/45 19/180
-7/360 17/90 -37/360
>sqrt(max(abs(eigenvalues(A.A'))))
15
>real(eigenvalues(A))
[ 4.89897948557 -4.89897948557 15 ]
>{U,v,W}=svd(A); v,
[ 15 3.46410161514 6.92820323028 ]
>A ::= magic(3)
8 1 6
3 5 7
4 9 2
>&eigenvalues(A)[1]
[- 2 sqrt(6), 2 sqrt(6), 15]
>shortestformat; A=magic(4)
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>A=A[:,[1,3,2,4]]
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
>sum(A)', sum(A')', sum(diag(A,0)), sum(diag(flipy(A),0))
[ 34 34 34 34 ]
[ 34 34 34 34 ]
34
34
>det(A)
0
>rank(A)
3
>r=(3:24)'|0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
>for i=1 to rows(r); r[i,2]=rank(magic(r[i,1])); end;
>r
3 3
4 3
5 5
6 5
7 7
8 3
9 9
10 7
11 11
12 3
13 13
14 9
15 15
16 3
17 17
18 11
19 19
20 3
21 21
22 13
23 23
24 3
>plot2d(r'[1],r'[2],>bar,>insimg);

>n=9; A=magic(n); plot3d(A/totalmax(A)*n/2,>bar,<frame, ...
> height=50°,angle=20°,>insimg);

>function x2s (x:real vector) ...
s="";
for i=1 to length(x)
s=s+char(x[i]);
end;
return s;
endfunction
>x2s(48:57)
0123456789
>s=x2s(32:96)
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`
>chartostr(32:96)
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`
>function s2x (s:string) ...
n=strlen(s);
x=zeros(1,n);
for i=1 to n;
x[i]=ascii(s);
s=substring(s,2,n-i+1);
end;
return x;
endfunction
>s2x(s)
[ 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
86 87 88 89 90 91 92 93 94 95 96 ]
>strtochar(s)
[ 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
86 87 88 89 90 91 92 93 94 95 96 ]

>longformat; p=97
97
>x=[52;54]
52
54
>A=[71,2;2,26]
71 2
2 26
>y=mod(A.x,97)
17
53
>mod(A.y,97)
52
54
>mod(A.A,97)
1 0
0 1
>function crypto (s:string) ...
global A,p;
x=strtochar(s)-32;
X=redim(x,2,(length(x)+1)/2);
X=mod(A.X,p);
x=redim(X,1,length(x))+32;
return chartostr(x);
endfunction
>s=crypto("Hello World")
H-?36 WZU{s
>crypto(s)
Hello World
>crypto(crypto("Hello World!"))
Hello World!

>function threenplus1 (n) ...
N=n;
repeat
if mod(n,2)==0 then n=n/2; N=N|n;
else n=3*n+1; N=N|n;
endif
until n==1;
end;
return N
endfunction
>N=threenplus1(7)
[ 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 ]
>plot2d(N); insimg;

>plot2d(N,>logplot); insimg;

>function test ...
n=7;
repeat
N=threenplus1(n);
plot2d(N,>logplot,title="n="+ ...
n+". Press up, down or return");
k=key;
if k==1 then n=n+1;
elseif k==2 then n=max(n-1,1);
elseif k==13 then return;
endif;
end;
endfunction
>test;
>insimg;

>epsilon
2.22044604925e-012
>printhex(epsilon)
2.7100000000000*16^-10
>printhex(2^-52)
1.0000000000000*16^-13
>printdual(2^-52)
1.0000000000000000000000000000000000000000000000000000*2^-52
>printhex(0.1)
1.999999999999A*16^-1
>x=0; loop 1 to 10; x=x+0.1; end; printdual(x)
1.1111111111111111111111111111111111111111111111111111*2^-1
>0:0.1:1
[ 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 ]
>longestformat; 1-(4/3-1)*3
2.220446049250313e-016
>&1-(4/3-1)*3
0
>[17,5;1.7,0.5]\[22;2.2]
Determinant zero!
Error in :
[17,5;1.7,0.5]\[22;2.2]
^
>fit([17,5;1.7,0.5],[22;2.2])
1.294117647058824
0
>svdsolve([17,5;1.7,0.5],[22;2.2])
1.191082802547771
0.3503184713375797
>x = 0.988:.0001:1.012;
>plot2d(x,(x-1)^7,color=red,thickness=2);
>p &= expand((x-1)^7)
7 6 5 4 3 2
x - 7 x + 21 x - 35 x + 35 x - 21 x + 7 x - 1
>y = p(x);
>plot2d(x,y,color=blue,>add); insimg;

>longformat; p=polycons(ones(1,7))
[ -1 7 -21 35 -35 21 -7 1 ]
>plot2d(x,xpolyval(p,x,eps=1e-16),color=red,thickness=2); insimg;

>function bio (birth, now) ...
t=linspace(now-8,now+8,200);
plot2d(t-now,100*sin(2*pi/33*(t-birth)),color=red,);
plot2d(t-now,100*sin(2*pi/28*(t-birth)),color=green,>add);
plot2d(t-now,100*sin(2*pi/23*(t-birth)),color=blue,>add);
title("red=intellectual, green=emotional, blue=physical");
endfunction
>bio(day(1956,5,15),day(2012,14,7)); insimg;

Examples Homepage