Halaman

Minggu, 16 Januari 2011

Basic commands and syntax in MATLAB

Google Ads
Google Ads
    We will speak about Basic commands and syntax in MATLAB to show how we can write code in it .

    If you type in a valid expression and press Enter, MATLAB will immediately execute it and return the result.

>> 2+2
ans =
           4

>> 4ˆ2

ans =
           16

>> sin(pi/2)

ans =
           1

>> 1/0


Warning: Divide by zero.

ans =
            Inf

>> exp(i*pi)

ans =
            -1.0000 + 0.0000i

       Notice some of the special expressions here: pi for π, Inf for ∞, and i for √−1. Another is NaN, which stands for not a number. NaN is used to express an undefined value. For example,

>> Inf/Inf

ans =
            NaN

You can assign values to variables.

>> x = sqrt(3)

x =
          1.7321

>> 3*z

??? Undefined function or variable ’z’.

      Observe that variables must have values before they can be used. When an expression returns a single result that is not assigned to a variable, this result is assigned to ans, which can then be used like any other variable.

>> atan(x)

ans =
            1.0472

>> pi/ans

ans =
            3
 
      In floating-point arithmetic, you should not expect “equal” values to have a difference of exactly zero. The built-in number eps tells you the maximum error in arithmetic on your particular machine. For simple operations, the relative error should be less than this number. For instance,

>> exp(log(10)) - 10

ans =
            1.7764e-15

>> ans/10

ans =
            1.7764e-16

>> eps

ans =
             2.2204e-16

Here are a few other demonstration statements.

>>  %  This is a comment.
>>  x  =  rand(100,100) ;  %  ;  means "don’t print out"
>>  s  =  ’Hello world’ ;  %  quotes enclose a string
>>  t   =  1 + 2 + 3 + ...

   4 + 5 + 6                       % ... continues a line

t =
        21

     Once variables have been defined, they exist in the workspace. You can see what’s in the
workspace from the desktop or by using

>> who
Your variables are:

ans      s     t    x

How to  save  work ?

      If you enter save myfile, all the variables in the workspace will be saved to a file myfile.mat
in the current directory. Later you can use load myfile to recover the variables.

      If you right-click in the Command History window and select “Create M-File. . . ”, you can
save all your typed commands to a text file. This can be very helpful for recreating what you have done.

Q:

Evaluate the following mathematical expressions in MATLAB

----   tanh(e)   


----   log10 (2)
Google Ads
Facebook Twitter Google+

Back To Top