Contents

The demos for our text book

we tried fern, floatgui, and walker.

ncmgui

Golden ratio phi (1)

notice 1) display format of number, 2) three methods to calculate phi.

phi= (1+sqrt(5))/2
format long
phi
1/phi

p= [1 -1 -1];
r= roots(p)

r= solve('x-1= 1/x')
phi= r(1)
vpa(phi, 50)           % too many digits
phi= double(phi)

f= @(x) x-1-1/x;
figure(2);
ezplot(f, [0, 4])
phi= fzero(f, 1)
hold on
plot(phi, 0, 'o')
plot([0, 4], [0, 0])
phi =

    1597/987   


phi =

   1.618033988749895


ans =

   0.618033988749895


r =

  -0.618033988749895
   1.618033988749895

 
r =
 
 5^(1/2)/2 + 1/2
 1/2 - 5^(1/2)/2
 
 
phi =
 
5^(1/2)/2 + 1/2
 
 
ans =
 
1.6180339887498948482045868343656381177203091798058
 

phi =

   1.618033988749895


phi =

   1.618033988749895

Golden ratio phi (2)

下面是显示黄金矩形的程序,它是黄金比例名字的由来。 注意看goldrect.m中的plot, text及axis等有关命令。

edit goldrect.m
figure(3)
goldrect

Golden ratio phi (3)

黄金比例也可以由连分数(取无穷项)得到。 goldfract.m是显示有限项连分数的程序,随着项数n增加误差越小吗?

edit goldrect.m
goldfract(38)
goldfract(39)
goldfract(91)  % 试试n=91看看,从goldfract.m源码中找原因
p =

1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1))))))))))))))))))))))))))))))))))))))


p =

102334155/63245986


p =

   1.618033988749895


err =

   2.2204e-16


p =

1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1)))))))))))))))))))))))))))))))))))))))


p =

165580141/102334155


p =

   1.618033988749895


err =

     0


p =

1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1+1/(1)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))


p =

1.220016e+19/7540113804746346496


p =

   1.618033933694774


err =

   5.5055e-08

斐波拉契数

看两个函数fibonacci.m和fibnum.m, 后者是递归程序。 递归程序简洁,但运行时间长。 最后,看看相邻斐波拉契数的比值

edit fibonacci.m
edit fibnum.m
tic, fibonacci(24), toc
tic, fibnum(24), toc
tic, fibnum(30), toc
f= fibonacci(40);
f(2:40)./f(1:39)     %注意./
ans =

           1
           2
           3
           5
           8
          13
          21
          34
          55
          89
         144
         233
         377
         610
         987
        1597
        2584
        4181
        6765
       10946
       17711
       28657
       46368
       75025

Elapsed time is 0.000195 seconds.

ans =

       75025

Elapsed time is 0.832392 seconds.

ans =

     1346269

Elapsed time is 11.187684 seconds.

ans =

    2.0000
    1.5000
    1.6667
    1.6000
    1.6250
    1.6154
    1.6190
    1.6176
    1.6182
    1.6180
    1.6181
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180
    1.6180

分形蕨

看看复杂的绘图程序,图片文件的读取与显示。

fern
edit fern.m
F= imread('fern.png');
figure;
image(F)

3阶幻方矩阵

熟悉矩阵的一些计算命令

A= magic(3)
sum(A)
sum(A')'
sum(diag(A))
sum(diag(flipud(A)))
det(A)
X= inv(A)
format rat; X
rot90(A,1)
norm(A)
eig(A)
A =

     8     1     6
     3     5     7
     4     9     2


ans =

    15    15    15


ans =

    15
    15
    15


ans =

    15


ans =

    15


ans =

  -360


X =

    0.1472   -0.1444    0.0639
   -0.0611    0.0222    0.1056
   -0.0194    0.1889   -0.1028


X =

      53/360        -13/90          23/360   
     -11/180          1/45          19/180   
      -7/360         17/90         -37/360   


ans =

       6              7              2       
       1              5              9       
       8              3              4       


ans =

      15       


ans =

      15       
    4801/980   
   -4801/980   

4阶幻方矩阵

figure
load durer
image(X)
colormap(map)
axis image
A= magic(4)
inv(A)
rank(A)        % 更高阶幻方矩阵的秩的规律,看课本
A =

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


ans =

       *              *              *              *       
       *              *              *              *       
       *              *              *              *       
       *              *              *              *       


ans =

       3