MATLAB BASICS(input & output)
Input and Output
You input commands to MATLAB in the MATLAB Command Window. MAT-
LAB returns output in two ways: Typically, text or numerical output is re-
turned in the same Command Window, but graphical output appears in a
separate graphics window. A sample screen, with both a MATLAB Desktop
and a graphics window, labeled Figure No. 1, is shown in Figure 2–1.
Togeneratethisscreenonyourcomputer,firsttype1/2 + 1/3.Thentype
ezplot(’xˆ3 - x’). While MATLAB is working, it may display a “wait” symbol — for example,
an hourglass appears on many operating systems. Or it may give no visual
evidence until it is finished with its calculation.
Arithmetic
As we have just seen, you can use MATLAB to do arithmetic as you would a
calculator.You can use “+” to add, “-” to subtract , “*” to multiply, “/” to divide,and “ˆ” to exponentiate.
For example,
>> 3ˆ2 - (5 + 4)/2 + 6*3
ans =
22.5000
MATLAB prints the answer and assigns the value to a variable called ans.
If you want to perform further calculations with the answer, you can use the
variable ans rather than retype the answer. For example, you can compute
the sum of the square and the square root of the previous answer as follows:
>> ansˆ2 + sqrt(ans)
ans =
510.9934
Observe that MATLAB assigns a new value to ans witheachcalculation.
Todomorecomplexcalculations,youcanassigncomputedvaluestovariables
of your choosing.
For example,
>> u = cos(10)
u=
-0.8391
>> v = sin(10)
v=
-0.5440
>> uˆ2 + vˆ2
ans =
1
MATLAB uses double-precision floating point arithmetic,which is accurate
to approximately 15 digits;however,MATLAB displays only 5 digits by default.
To display more digits, type format long. Then all subsequent numerical
output will have 15 digits displayed. Type format short to return to 5-digit
display.
MATLAB differs from a calculator in that it can do exact arithmetic. For
example,it can add the fractions 1/2 and 1/3 symbolically to obtain the correct
fraction 5/6.
We discuss how to do this in the section Symbolic Expressions,
Variable Precision, and Exact Arithmetic Post
0 comments:
Post a Comment