Professional Documents
Culture Documents
Dr. R. Uppaluri Department of Chemical Engineering, Indian Institute of Technology, Guwahati. Email: ramgopalu@iitg.ernet.in
Contents
1. 2. 3. 4. 5. 6. 7. 8. 9. Quick Overview Data Types & Variables Operators Flow Control Functions Input/Output Array Manipulation Writing & Running Programs Plotting
1. Quick Overview
MATLAB is termed as MATRIX LABORATORY High Level Computer Language Scientific Computing Data Visualization Interactive Programming Environment Quick Testing and Debugging Allows one to focus more on principles of programming rather than on programming itself Programs can be developed in a Shorter Time than equivalent FORTRAN or C Programs Negative: No Stand-alone applications i.e., Programs have to be run on MATLAB environment only.
CODE
Code is shorter and simpler than equivalent FORTRAN code
code
Unlike FORTRAN & C Compilers, MATLAB can be operated in the interactive mode through its command window (>>)
Therefore, one has to be careful with even Single Quote Operator while coding
Cells
Strings
A string is a sequence of characters Treated as Character Array in MATLAB Concatenated (adding) using strcat command and extracted using colon (:) as shown in this example
3. Operators
Arthimetic Operators
3. Operators
Arthimetic Operators: Matrices
3. Operators
Division Operators
Scalars
Right division means a/b Left division means b/a
Matrices
Right division: Solution of X*A = B Left division: Solution of A*X = B
3. Operators
Comparison Operators: Example
Logical Operators
Used for building compounding expressions
3. Operators
Logical Operators: Example
4. Flow Control
Conditionals
Single condition
4. Flow Control
Conditionals: Example
4. Flow Control
Conditionals: Switch Statement
Here Expression is evaluated, control is passed to the case that matches the value
Multiple Conditions
CODE
Program for calculating number of years for compoundnig $ 1,000 with 6 % annual interest to $ 10,000
4. Flow Control
Tip: Loops be avoided when Vectors can be used
4. Flow Control
Break statement
Any loop can be terminated by the break statement Upon encountering a break statement, control is passed to the first statement outside the look.
buildvec constructs a row vector of arbitrary length by prompting its elements. When an empty element is encountered, break statement allows ignoring the statement
4. Flow Control
Error Statement
Program execution can be terminated with the error function
Above function has two inputs: x and epsilon Also has two outputs: x and numIter
code
Solve.m myfunc.m
Here, call solve(@myfunc, 2) creates a function handle to myfunc and passes it to solve as an argument Hence, the variable func in solve contains the handle to myfunc Function passed to another function by its handle is evaluated by feval function.
Colon operator
Logspace command
zeros
7. Array Manipulation
ones
rand
eye
9. Plotting
Example 1: Plot sin x and cos x on a two dimensional plot
9. Plotting
Example 1: Plot sin x and cos x on a two dimensional plot
9. Plotting
9. Plotting
CODE
9. Plotting
Example 3: Polar co-ordinates
CODE
9. Plotting
Example 4: Pie chart
pie(X) draws a pie chart using the data in X. Each element in X is represented as a slice in the pie chart. pie(X,explode) offsets a slice from the pie. explode is a vector or matrix of zeros and nonzeros that correspond to X. h = pie(...) returns a vector of handles to patch and text graphics objects.
9. Plotting
CODE
9. Plotting
Example 5: Bar Chart
A bar graph displays the values in a vector or matrix as horizontal or vertical bars bar(Y) draws one bar for each element in Y. If Y is a matrix, bar groups the bars produced by the elements in each row. bar(x,Y) draws a bar for each element in Y at locations specified in x, where x is a monotonically increasing vector defining the x-axis intervals for the vertical bars.
9. Plotting
Example 5: Bar Chart
x = -2.9:0.2:2.9; bar(x,exp(x.*x),'r')
CODE
t = -10*pi:pi/100:10*pi; x = t.*cos(t); y = t.*sin(t); h = plot3(x,y,t); title('Curve u(t) = < t*cos(t), t*sin(t), t >') xlabel('x') ylabel('y') zlabel('z') grid
CODE
It is used to create a mesh meshgrid This function generates two 2-dimensional arrays for 3-D plots.
x = -1:0.05:1; y = x; [xi, yi] = meshgrid(x,y); zi = yi.^2 - xi.^2; mesh(xi, yi, zi) axis off
CODE
CODE
CODE
CODE