Thursday, February 23, 2006

 

Random Generator

>> help spdiags

 SPDIAGS Sparse matrix formed from diagonals.
    SPDIAGS, which generalizes the function "diag", deals with three
    matrices, in various combinations, as both input and output.

    [B,d] = SPDIAGS(A) extracts all nonzero diagonals from the m-by-n
    matrix A.  B is a min(m,n)-by-p matrix whose columns are the p
    nonzero diagonals of A.  d is a vector of length p whose integer
    components specify the diagonals in A.

    B = SPDIAGS(A,d) extracts the diagonals specified by d.
    A = SPDIAGS(B,d,A) replaces the diagonals of A specified by d with
        the columns of B.  The output is sparse.
    A = SPDIAGS(B,d,m,n) creates an m-by-n sparse matrix from the
        columns of B and places them along the diagonals specified by d.

    Roughly, A, B and d are related by
        for k = 1:p
            B(:,k) = diag(A,d(k))
        end

    Example: These commands generate a sparse tridiagonal representation
    of the classic second difference operator on n points.
        e = ones(n,1);
        A = spdiags([e -2*e e], -1:1, n, n)

    Some elements of B, corresponding to positions "outside" of A, are
    not actually used.  They are not referenced when B is an input and
    are set to zero when B is an output.  If a column of B is longer than
    the diagonal it's representing, elements of super-diagonals of A
    correspond to the lower part of the column of B, while elements of
    sub-diagonals of A correspond to the upper part of the column of B.

    Example: This uses the top of the first column of B for the second
    sub-diagonal and the bottom of the third column of B for the first
    super-diagonal.
        B = repmat((1:n)',1,3);
        S = spdiags(B,[-2 0 1],n,n);

    See also DIAG.
ode文件 ode文件是一种函数M文件,即:在m-file编辑器中,文件必须以function开头,后面跟的函数名必须与将来保存的M文件名一致。函数文件第一句的具体形式为:function y=average(x) 其中为y输出变量名,x为变量,average为函数名,保存时文件名必须是average.m。当函数有一个以上的输出函数时,输出参数包含在方括号里,变参包含在圆括号里,即函数文件的第一行的具体形式为: function [out1,out2,…]=average(in1,in2,…) 例:计算数组元素的平均值 解:function y=average(x) y=sum(x)/length(x) 注:上下两式等号前的符号"y"必须相同

為了要有隨機的數值,使用c內部的rand函式,是個不錯的選擇,但是以前沒有用過,所以重新來整理一下。

RAND_MAX: 是個非常大的數值,所以隨機函式庫所產生的數值,都會小、等於RAND_MAX。
rand(): 產生0至RAND_MAX的整數值。

所以(rand()+1.)/(RAND_MAX+2.)就會產生介於0與1的隨機實數。


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?