上机实验报告(实验一 MATLAB软件使用)

1.请用help查看以下运行操作符的功能:

>> A=3;B=6;
>> help +
 +   Plus.
    X + Y adds matrices X and Y. X and Y must have compatible sizes. In the
    simplest cases, they can be the same size or one can be a scalar. Two
    inputs have compatible sizes if, for every dimension, the dimension
    sizes of the inputs are either the same or one of them is 1.
 
    C = plus(A,B) is called for the syntax 'A + B' when A or B is an
    object.

    plus 的文档
    名为 plus 的其他函数

>> C = plus(A,B)

C =

     9

>> A + B

ans =

     9

>> help -
 -   Minus. 
    X - Y subtracts matrix Y from X. X and Y must have compatible sizes. In
    the simplest cases, they can be the same size or one can be a scalar.
    Two inputs have compatible sizes if, for every dimension, the dimension
    sizes of the inputs are either the same or one of them is 1.
 
    C = minus(A,B) is called for the syntax 'A - B' when A or B is an
    object.

    minus 的文档
    名为 minus 的其他函数

>> C = minus(A,B) 

C =

    -3

>> A - B

ans =

    -3

>> help *
 *   Matrix multiply.
    X*Y is the matrix product of X and Y.  Any scalar (a 1-by-1 matrix)
    may multiply anything.  Otherwise, the number of columns of X must
    equal the number of rows of Y.
 
    C = mtimes(A,B) is called for the syntax 'A * B' when A or B is an
    object.
 
    See also times.

    mtimes 的文档
    名为 mtimes 的其他函数

>>  C = mtimes(A,B) 

C =

    18

>> A * B

ans =

18

>> help /
 /   Slash or right matrix divide.
    A/B is the matrix division of B into A, which is roughly the
    same as A*INV(B) , except it is computed in a different way.
    More precisely, A/B = (B'\A')'. See MLDIVIDE for details.
 
    C = mrdivide(A,B) is called for the syntax 'A / B' when A or B is an
    object.
 
    See also mldivide, rdivide, ldivide.

    mrdivide 的文档
    名为 mrdivide 的其他函数

>>  C = mrdivide(A,B) 

C =

    0.5000

>>  A/B

ans =

    0.5000

>> help \
 \   Backslash or left matrix divide.
    A\B is the matrix division of A into B, which is roughly the
    same as INV(A)*B , except it is computed in a different way.
    If A is an N-by-N matrix and B is a column vector with N
    components, or a matrix with several such columns, then
    X = A\B is the solution to the equation A*X = B. A warning 
    message is printed if A is badly scaled or nearly singular.
    A\EYE(SIZE(A)) produces the inverse of A.
 
    If A is an M-by-N matrix with M < or > N and B is a column
    vector with M components, or a matrix with several such columns,
    then X = A\B is the solution in the least squares sense to the
    under- or overdetermined system of equations A*X = B. The
    effective rank, K, of A is determined from the QR decomposition
    with pivoting. A solution X is computed which has at most K
    nonzero components per column. If K < N this will usually not
    be the same solution as PINV(A)*B.  A\EYE(SIZE(A)) produces a
    generalized inverse of A.
 
    C = mldivide(A,B) is called for the syntax 'A \ B' when A or B is an
    object.
 
    See also ldivide, rdivide, mrdivide.

    mldivide 的文档
    名为 mldivide 的其他函数

>> C = mldivide(A,B) 

C =

     2

>> A\B

ans =

     2

>> help ^
 ^   Matrix power.
    Z = X^y is X to the y power if y is a scalar and X is square. If y
    is an integer greater than one, the power is computed by repeated
    squaring. For other values of y the calculation involves
    eigenvalues and eigenvectors.
 
    Z = x^Y is x to the Y power if Y is a square matrix and x is a scalar.
    Computed using eigenvalues and eigenvectors.
 
    Z = X^Y, where both X and Y are matrices, is an error.
 
    C = mpower(A,B) is called for the syntax 'A ^ B' when A or B is an
    object.
 
    See also power.

    mpower 的文档
    名为 mpower 的其他函数

>> C = mpower(A,B)

C =

   729

>> A^B

ans =

   729

>> help ‘
  Operators and special characters.
 
  Arithmetic operators.
    plus       - Plus                               +    
    uplus      - Unary plus                         +    
    minus      - Minus                              -    
    uminus     - Unary minus                        -    
    mtimes     - Matrix multiply                    *    
    times      - Array multiply                    .*    
    mpower     - Matrix power                       ^    
    power      - Array power                       .^    
    mldivide   - Backslash or left matrix divide    \    
    mrdivide   - Slash or right matrix divide       /    
    ldivide    - Left array divide                 .\    
    rdivide    - Right array divide                ./    
    idivide    - Integer division with rounding option.
    kron       - Kronecker tensor product   
 
  Relational operators.
    eq         - Equal                             ==     
    ne         - Not equal                         ~=     
    lt         - Less than                          <      
    gt         - Greater than                       >      
    le         - Less than or equal                <=     
    ge         - Greater than or equal             >=     
 
  Logical operators.
    relop      - Short-circuit logical AND         &&     
    relop      - Short-circuit logical OR          ||     
    and        - Element-wise logical AND           &      
    or         - Element-wise logical OR            |      
    not        - Logical NOT                        ~      
    punct      - Ignore function argument or output ~
    xor        - Logical EXCLUSIVE OR
    any        - True if any element of vector is nonzero
    all        - True if all elements of vector are nonzero
 
  Special characters. 
    colon      - Colon                              : 
    paren      - Parentheses and subscripting      ( )              
    paren      - Brackets                          [ ]     
    paren      - Braces and subscripting           { }          
    punct      - Function handle creation           @
    punct      - Decimal point                      .      
    punct      - Structure field access             .      
    punct      - Parent directory                   ..     
    punct      - Continuation                       ...    
    punct      - Separator                          ,      
    punct      - Semicolon                          ;      
    punct      - Comment                            %      
    punct      - Invoke operating system command    !            
    punct      - Assignment                         =
    punct      - Quote                              '      
    punct      - Double quote                       "    
    transpose  - Transpose                         .'
    ctranspose - Complex conjugate transpose        ' 
    horzcat    - Horizontal concatenation          [,]     
    vertcat    - Vertical concatenation            [;]     
    subsasgn   - Subscripted assignment          ( ),{ },.   
    subsref    - Subscripted reference           ( ),{ },.   
    numArgumentsFromSubscript - Number of arguments for indexing methods
    subsindex  - Subscript index
    metaclass  - Metaclass for MATLAB class         ?
 
  Bitwise operators.
    bitand     - Bit-wise AND.
    bitcmp     - Complement bits.
    bitor      - Bit-wise OR.
    bitxor     - Bit-wise XOR.
    bitset     - Set bit.
    bitget     - Get bit.
    bitshift   - Bit-wise shift.
 
  Set operators.
    union      - Set union.
    unique     - Set unique.
    intersect  - Set intersection.
    setdiff    - Set difference.
    setxor     - Set exclusive-or.
    ismember   - True for set member.
 
  See also function_handle.
>> A=[1 2 3; 4 5 6];
>> A'

ans =

     1     4
     2     5
     3     6

>> A=[1 2 3; 4 5 6];
>> B=[0 1 2; 3 6 8];
>> help .*
 .*  Array multiply.
    X.*Y denotes element-by-element multiplication. X and Y must have
    compatible sizes. In the simplest cases, they can be the same size or
    one can be a scalar. Two inputs have compatible sizes if, for every
    dimension, the dimension sizes of the inputs are either the same or one
    of them is 1.
 
    C = times(A,B) is called for the syntax 'A .* B' when A or B is an
    object.
 
    See also mtimes.

    times 的文档
    名为 times 的其他函数

>> C = times(A,B)

C =

     0     2     6
    12    30    48

>> A .* B

ans =

     0     2     6
    12    30    48

>> help ./
 ./  Right array divide.
    A./B divides each element of A by the corresponding element of B. A and 
    B must have compatible sizes. In the simplest cases, they can be the 
    same size or one can be a scalar. Two inputs have compatible sizes if, 
    for every dimension, the dimension sizes of the inputs are either the 
    same or one of them is 1. The element-wise operators ./ and .\ are 
    related to each other by the equation A./B = B.\A.
 
    C = rdivide(A,B) is called for the syntax 'A ./ B' when A or B is an
    object.
 
    See also ldivide, mldivide, mrdivide.

    rdivide 的文档
    名为 rdivide 的其他函数

>>  C = rdivide(A,B) 

C =

       Inf    2.0000    1.5000
1.3333    0.8333    0.7500

>> A./B

ans =

       Inf    2.0000    1.5000
1.3333    0.8333    0.7500

>> help .^
 .^  Array power.
    Z = X.^Y denotes element-by-element powers. X and Y must have
    compatible sizes. In the simplest cases, they can be the same size or
    one can be a scalar. Two inputs have compatible sizes if, for every
    dimension, the dimension sizes of the inputs are either the same or one
    of them is 1.
 
    C = power(A,B) is called for the syntax 'A .^ B' when A or B is an
    object.
 
    See also mpower, nthroot, realpow.

    power 的文档
    名为 power 的其他函数

>> C = power(A,B)

C =

           1           2           9
          64       15625     1679616

>> A .^ B

ans =

           1           2           9
          64       15625     1679616

>> A=[1 2 3; 4 5 6];
>> B=[0 1 2; 3 6 8];
>> help .‘
  Operators and special characters.
 
  Arithmetic operators.
    plus       - Plus                               +    
    uplus      - Unary plus                         +    
    minus      - Minus                              -    
    uminus     - Unary minus                        -    
    mtimes     - Matrix multiply                    *    
    times      - Array multiply                    .*    
    mpower     - Matrix power                       ^    
    power      - Array power                       .^    
    mldivide   - Backslash or left matrix divide    \    
    mrdivide   - Slash or right matrix divide       /    
    ldivide    - Left array divide                 .\    
    rdivide    - Right array divide                ./    
    idivide    - Integer division with rounding option.
    kron       - Kronecker tensor product   
 
  Relational operators.
    eq         - Equal                             ==     
    ne         - Not equal                         ~=     
    lt         - Less than                          <      
    gt         - Greater than                       >      
    le         - Less than or equal                <=     
    ge         - Greater than or equal             >=     
 
  Logical operators.
    relop      - Short-circuit logical AND         &&     
    relop      - Short-circuit logical OR          ||     
    and        - Element-wise logical AND           &      
    or         - Element-wise logical OR            |      
    not        - Logical NOT                        ~      
    punct      - Ignore function argument or output ~
    xor        - Logical EXCLUSIVE OR
    any        - True if any element of vector is nonzero
    all        - True if all elements of vector are nonzero
 
  Special characters. 
    colon      - Colon                              : 
    paren      - Parentheses and subscripting      ( )              
    paren      - Brackets                          [ ]     
    paren      - Braces and subscripting           { }          
    punct      - Function handle creation           @
    punct      - Decimal point                      .      
    punct      - Structure field access             .      
    punct      - Parent directory                   ..     
    punct      - Continuation                       ...    
    punct      - Separator                          ,      
    punct      - Semicolon                          ;      
    punct      - Comment                            %      
    punct      - Invoke operating system command    !            
    punct      - Assignment                         =
    punct      - Quote                              '      
    punct      - Double quote                       "    
    transpose  - Transpose                         .'
    ctranspose - Complex conjugate transpose        ' 
    horzcat    - Horizontal concatenation          [,]     
    vertcat    - Vertical concatenation            [;]     
    subsasgn   - Subscripted assignment          ( ),{ },.   
    subsref    - Subscripted reference           ( ),{ },.   
    numArgumentsFromSubscript - Number of arguments for indexing methods
    subsindex  - Subscript index
    metaclass  - Metaclass for MATLAB class         ?
 
  Bitwise operators.
    bitand     - Bit-wise AND.
    bitcmp     - Complement bits.
    bitor      - Bit-wise OR.
    bitxor     - Bit-wise XOR.
    bitset     - Set bit.
    bitget     - Get bit.
    bitshift   - Bit-wise shift.
 
  Set operators.
    union      - Set union.
    unique     - Set unique.
    intersect  - Set intersection.
    setdiff    - Set difference.
    setxor     - Set exclusive-or.
    ismember   - True for set member.
 
  See also function_handle.

>> C=transpose(A)

C =

     1     4
     2     5
     3     6

>> A.'

ans =

     1     4
     2     5
     3     6

>> help &
 &  Logical and.
    A & B performs a logical and of arrays A and B and returns an array
    containing elements set to either logical 1 (TRUE) or logical 0
    (FALSE). An element of the output array is set to 1 if both input
    arrays contain a non-zero element at that same array location.
    Otherwise, that element is set to 0. A and B must have compatible
    sizes. In the simplest cases, they can be the same size or one can be a
    scalar. Two inputs have compatible sizes if, for every dimension, the
    dimension sizes of the inputs are either the same or one of them is 1.
 
    C = and(A,B) is called for the syntax 'A & B' when A or B is an object.
 
    Note that there are two logical and operators in MATLAB. The & operator
    performs an element-by-element and between matrices, while the &&
    operator performs a short-circuit and between scalar values. See the
    documentation for details.
 
    See also RELOP, or, xor, not.

    and 的文档
    名为 and 的其他函数

>> C = and(A,B)

C =

  2×3 logical 数组

   0   1   1
   1   1   1

>> A & B

ans =

  2×3 logical 数组

   0   1   1
   1   1   1

>> help |
 |   Logical or.
    A | B performs a logical or of arrays A and B and returns an array
    containing elements set to either logical 1 (TRUE) or logical 0
    (FALSE). An element of the output array is set to 1 if either input
    array contains a non-zero element at that same array location.
    Otherwise, that element is set to 0. A and B must have compatible
    sizes. In the simplest cases, they can be the same size or one can be a
    scalar. Two inputs have compatible sizes if, for every dimension, the
    dimension sizes of the inputs are either the same or one of them is 1.
 
    C = or(A,B) is called for the syntax 'A | B' when A or B is an object.
 
    Note that there are two logical or operators in MATLAB. The | operator
    performs an element-by-element or between matrices, while the ||
    operator performs a short-circuit or between scalar values. See the
    documentation for details.
 
    See also RELOP, and, xor, not

    or 的文档
    名为 or 的其他函数

>> C = or(A,B)

C =

  2×3 logical 数组

   1   1   1
   1   1   1

>> A | B

ans =

  2×3 logical 数组

   1   1   1
   1   1   1

>> help ~
 ~   Logical not.
    ~A performs a logical not of input array A, and returns an array
    containing elements set to either logical 1 (TRUE) or logical 0 (FALSE).
    An element of the output array is set to 1 if A contains a zero value
    element at that same array location.  Otherwise, that element is set to
    0.
 
    B = not(A) is called for the syntax '~A' when A is an object.
 
    ~ can also be used to ignore input arguments in a function definition,
    and output arguments in a function call.  See "help punct"

    not 的文档
    名为 not 的其他函数

>>  B = not(A)

B =

  2×3 logical 数组

   0   0   0
   0   0   0

>> ~A

ans =

  2×3 logical 数组

   0   0   0
   0   0   0

>> help xor
 xor Logical EXCLUSIVE OR.
    xor(S,T) is the logical symmetric difference of elements S and T. The
    result is logical 1 (TRUE) where either S or T, but not both, is
    nonzero. The result is logical 0 (FALSE) where S and T are both zero or
    nonzero. S and T must have compatible sizes. In the simplest cases,
    they can be the same size or one can be a scalar. Two inputs have
    compatible sizes if, for every dimension, the dimension sizes of the
    inputs are either the same or one of them is 1.
 
    See also or, RELOP.

    xor 的文档
    名为 xor 的其他函数

>> xor(A,B)

ans =

  2×3 logical 数组

   1   1   1
   1   1   1

>> help ~=
 ~=  Not equal.
    A ~= B does element by element comparisons between A and B and returns
    an array with elements set to logical 1 (TRUE) where the relation is
    true and elements set to logical 0 (FALSE) where it is not. A and B
    must have compatible sizes. In the simplest cases, they can be the same
    size or one can be a scalar. Two inputs have compatible sizes if, for
    every dimension, the dimension sizes of the inputs are either the same
    or one of them is 1.
 
    C = ne(A,B) is called for the syntax 'A ~= B' when A or B is an object.

    ne 的文档
    名为 ne 的其他函数

>> C = ne(A,B)

C =

  2×3 logical 数组

   1   1   1
   1   1   1

>> A ~= B

ans =

  2×3 logical 数组

   1   1   1
   1   1   1

2.运算符号运用计算习题:

(1)输入矩阵x=[1,2],y=[3,4],x'*y,回车;

x=[1,2],y=[3,4],x'*y           %矩阵x的逆矩阵与矩阵y的乘积

【运行结果】

x =

     1     2


y =

     3     4


ans =

     3     4
     6     8

(2)输入x.*y回车;

x.*y                           %矩阵x与矩阵y的乘积

【运行结果】

ans =

     3     8

(3)输入x.^y回车;

x.^y                           %矩阵求权

【运行结果】

ans =

     1    16

(4)输入2^3,回车;

2^3                            %2的3次方

【运行结果】

ans =

     8

3.请建立一个4*4的矩阵,矩阵中的元数值自定。要求写下输入的指令并记录结果:

[1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4]

【运行结果】

ans =

     1     1     1     1
     2     2     2     2
     3     3     3     3
     4     4     4     4

【注】同行异列之间的数字用“,”隔开,不同行之间用“;”分隔。

4.本课程实验中的基本函数:

(1)输入a=3+4*j,b=abs(a),记录运算结果,说出函数abs()的功能;

a=3+4*j,b=abs(a)               %求a的模

【运行结果】

a =

   3.0000 + 4.0000i


b =

     5

(2)输入a=3+3*j,b=angle(a),记录运算结果,说出函数angle()的功能;

a=3+3*j,b=angle(a)             %求a的相角

【运行结果】

a =

   3.0000 + 3.0000i


b =

    0.7854

(3)zeros(m,n),m和n为正整数,请输入参数并记录结果,然后确定该函数的功能;

m=3,n=2,zeros(m,n)             %作3行2列的零矩阵

【运行结果】

m =

     3


n =

     2


ans =

     0     0
     0     0
     0     0

(4)ones(m,n),m和n为正整数,请输入参数并记录结果,然后确定该函数的功能;

m=2,n=3,zeros(m,n)             %作2行3列的零矩阵

【运行结果】

m =

     2


n =

     3


ans =

     0     0     0
     0     0     0

(5)y=conv(x1,h1),输入help conv 查看该函数的功能,并用讲过的例题或作业来验证,请写下指令程序并记录记录结果;

x1=[1,1,1],h1=[0,0,1,1],y=conv(x1,h1)  %求x1与h1的卷积

【运行结果】

x1 =

     1     1     1


h1 =

     0     0     1     1


y =

     0     0     1     2     2     1
>> A=[1,2],B=[3,4],C = conv(A, B)             %作业题引用

A =

     1     2


B =

     3     4


C =

     3    10     8

(6)x=exp((a+j*w0)*n),令a=5+5*j,w0=0,n=2,请记录结果:

a=5+5*j,w0=0,n=2,x=exp((a+j*w0)*n)

【运行结果】

a =

   5.0000 + 5.0000i


w0 =

     0


n =

     2


x =

  -1.8482e+04 - 1.1983e+04i

(7)请输入下面这段程序,根据运行结果来分析并注释一些函数;

x=0:pi/50:2*pi                 %x从0开始取值,且间隔为pi/50,直到2*pi (三角函数的一个周期的自变量)
k=[1 26 51 76 101]             %创建包含1、26、51、76、101共5个元素的向量k
x(k)=[ ]                       %选取k值对应的数组x的值
figure(1)                      %创建图窗窗口
subplot(2,2,1)                 %将当前图窗划分为 2×2 网格,并在位置1创建坐标区
plot(x,sin(x)),grid on         %自变量为x,绘画sin(x)图像(当前坐标区或图的主网格线)
subplot(2,2,2)                 %将当前图窗划分为 2×2 网格,并在位置2创建坐标区
plot(x,cos(x)),grid on         %自变量为x,绘画cos(x)图像(当前坐标区或图的主网格线)
subplot(2,2,3)                 %将当前图窗划分为 2×2 网格,并在位置3创建坐标区
plot(x,tan(x)),grid on         %自变量为x,绘画tan(x)图像(当前坐标区或图的主网格线)
subplot(2,2,4)                 %将当前图窗划分为 2×2 网格,并在位置4创建坐标区
plot(x,cot(x)),grid on         %自变量为x,绘画cot(x)图像(当前坐标区或图的主网格线)

【运行结果】

x =

  列 1 至 5

         0    0.0628    0.1257    0.1885    0.2513

  列 6 至 10

    0.3142    0.3770    0.4398    0.5027    0.5655

  列 11 至 15

    0.6283    0.6912    0.7540    0.8168    0.8796

  列 16 至 20

    0.9425    1.0053    1.0681    1.1310    1.1938

  列 21 至 25

    1.2566    1.3195    1.3823    1.4451    1.5080

  列 26 至 30

    1.5708    1.6336    1.6965    1.7593    1.8221

  列 31 至 35

    1.8850    1.9478    2.0106    2.0735    2.1363

  列 36 至 40

    2.1991    2.2619    2.3248    2.3876    2.4504

  列 41 至 45

    2.5133    2.5761    2.6389    2.7018    2.7646

  列 46 至 50

    2.8274    2.8903    2.9531    3.0159    3.0788

  列 51 至 55

    3.1416    3.2044    3.2673    3.3301    3.3929

  列 56 至 60

    3.4558    3.5186    3.5814    3.6442    3.7071

  列 61 至 65

    3.7699    3.8327    3.8956    3.9584    4.0212

  列 66 至 70

    4.0841    4.1469    4.2097    4.2726    4.3354

  列 71 至 75

    4.3982    4.4611    4.5239    4.5867    4.6496

  列 76 至 80

    4.7124    4.7752    4.8381    4.9009    4.9637

  列 81 至 85

    5.0265    5.0894    5.1522    5.2150    5.2779

  列 86 至 90

    5.3407    5.4035    5.4664    5.5292    5.5920

  列 91 至 95

    5.6549    5.7177    5.7805    5.8434    5.9062

  列 96 至 100

    5.9690    6.0319    6.0947    6.1575    6.2204

  列 101

    6.2832


k =

     1    26    51    76   101


x =

  列 1 至 5

    0.0628    0.1257    0.1885    0.2513    0.3142

  列 6 至 10

    0.3770    0.4398    0.5027    0.5655    0.6283

  列 11 至 15

    0.6912    0.7540    0.8168    0.8796    0.9425

  列 16 至 20

    1.0053    1.0681    1.1310    1.1938    1.2566

  列 21 至 25

    1.3195    1.3823    1.4451    1.5080    1.6336

  列 26 至 30

    1.6965    1.7593    1.8221    1.8850    1.9478

  列 31 至 35

    2.0106    2.0735    2.1363    2.1991    2.2619

  列 36 至 40

    2.3248    2.3876    2.4504    2.5133    2.5761

  列 41 至 45

    2.6389    2.7018    2.7646    2.8274    2.8903

  列 46 至 50

    2.9531    3.0159    3.0788    3.2044    3.2673

  列 51 至 55

    3.3301    3.3929    3.4558    3.5186    3.5814

  列 56 至 60

    3.6442    3.7071    3.7699    3.8327    3.8956

  列 61 至 65

    3.9584    4.0212    4.0841    4.1469    4.2097

  列 66 至 70

    4.2726    4.3354    4.3982    4.4611    4.5239

  列 71 至 75

    4.5867    4.6496    4.7752    4.8381    4.9009

  列 76 至 80

    4.9637    5.0265    5.0894    5.1522    5.2150

  列 81 至 85

    5.2779    5.3407    5.4035    5.4664    5.5292

  列 86 至 90

    5.5920    5.6549    5.7177    5.7805    5.8434

  列 91 至 95

    5.9062    5.9690    6.0319    6.0947    6.1575

  列 96

6.2204

Figure 1:

 

(8)请输入下面这段程序,根据运行结果来分析并注释一些函数;

x=-2:.1:2;                           %x从-2开始取值,且间隔为0.1,直到2
y1=x.^2;                             %因变量为y1,函数表达式为x的二次方
y2=x.^3;                             %因变量为y2,函数表达式为x的三次方
figure(1);                           %创建图窗窗口
plot(x,y1,'r-',x,y2,'k--'),grid on;  %绘画幂函数图像“r-”(红色的连续曲线),“k--”(黑色的间断曲线)    
legend('\ity=x^2','\ity=x^3')        %在坐标区上添加图例y=x^2和y=x^3
title('y=x^m')                       %将y=x^m添加到坐标区或图中
xlabel('x'),ylabel('y')              %返回的当前坐标区或图的 x 轴添加标签,返回的当前坐标区或图的 y 轴添加标签

Figure 1:

(9)请自己查看IF语句、FOR语句的用法,然后编写一个函数来实现求n!。

【jiech.m文件】

%函数的格式是:function[a,b]=fuanction_name(c,d)
%下面就是实现该函数指令代码。
%jiech.m
function [a]=jiech(n)
  a=1
    for i=1:n
        if n==0
            a=1;
        else
            a=a*i;
        end
    end

 【调用过程】

d=jiech(0)   %求0!
d=jiech(1)   %求1!
d=jiech(3)   %求3!
d=jiech(4)   %求4!

【运行结果】

a =

     1


d =

     1



a =

     1


d =

     1


a =

     1


d =

     6


a =

     1


d =

    24

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值