In: Computer Science
4)
a.What are the rules in MATLAB for variable names and how do they compare to the rules in C++.
b.Find a complete list of reserved symbols in MATLAB™. How does this list differ from the reserved symbols in C++.
c.What does “vectorization” mean. Explain very briefly why vectorisation is useful in MATLAB.
d.MATLAB contains a very versatile plot() function. What arguments are required to execute this function. Name two arguments that are optional. How many different line colours and marker shapes are available to this function ?
e.Write the first line of a MATLAB function (i.e. only the line starting with “function” that accepts a single variable and returns three different variables.
.
4.
(a) Rules for naming variables.
1.A valid variable name starts with a letter, followed by letters, digits, or underscores
2. MATLAB is case sensitive, so A
and
a
are not the same variable.
3.The maximum length of a variable name is the value that the
namelengthmax
command returns.
4.You cannot define variables with the same names as MATLAB
keywords, such as if
or end
.
Example for naming :
Valid vaeiable names | Invalid variable names |
x7 | 7x |
lastValue | end |
n_factorial | n! |
(b)
This table contains a comprehensive listing of all MATLAB operators, symbols, and special characters.
In MATLAB | Name | IN C++ |
+ | addition | + |
+ | unary plus | ++ |
- | subtraction | - |
- | unary minus | -- |
.* | element multiplication | * |
* | matrix multiplication | * |
/ | matrix right division | / |
.^ | power | ^ |
.' | transpose | |
== | equal to | == |
~= | not equal to | != |
& |
logical AND | && |
| | Logical OR | || |
&& | AND with circuiting | |
|| | OR with circuiting | |
@ | at | @ |
... | ellipse | ... |
, | comma | , |
: | colon | : |
(c)
MATLAB is optimized for operations involving matrices and vectors. The process of revising loop-based, scalar-oriented code to use MATLAB matrix and vector operations is called vectorization. Vectorizing your code is worthwhile for several reasons:
Appearance: Vectorized mathematical code appears more like the mathematical expressions found in textbooks, making the code easier to understand.
Less Error Prone: Without loops, vectorized code is often shorter. Fewer lines of code mean fewer opportunities to introduce programming errors.
Performance: Vectorized code often runs much faster than the corresponding code containing loops.
(d)
The plot function in Matlab is used to create a graphical representation of some data. It is often very easy to "see" a trend in data when plotted, and very difficult when just looking at the raw numbers.
The default colors used in MATLAB changed in R2014b version.There are now 14 colours as per the standare RGB triplets.
(e)
The basic representation of a function in matlab is :
function [a b c] = something(a, b, c) % 3 inputs, 3 outputs
function something(a,b) % 2 inputs, 0 outputs
In the above code a, b, c are the variables or the inputs.The first line of every function is the definition statement, which includes the following elements. Use lowercase characters for the keyword. If your function returns one output, you can specify the output name after the function keyword.