Download presentation
Presentation is loading. Please wait.
1
Computer programming Dr. Ivan A. Hashim
2
MATLAB MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include: Math and computation Algorithm development Data acquisition Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building
3
MATLAB Desktop
4
Matrices and Arrays Entering Matrices
Separate the elements of a row with blanks or commas. Use a semicolon (;) to indicate the end of each row. Surround the entire list of elements with square brackets, [ ]. A = [ ; ; ; ]
5
Matrices and Arrays >> A = [ ; ; ; ] A =
6
Matrices and Arrays Subscripts >> A(1,3)+A(2,2)+A(3,4)+A(4,1)
ans = 28
7
Matrices and Arrays The Colon Operator (:) >> 1:10
>> 1:2:10 >> 100:-7:50
8
Matrices and Arrays The Colon Operator (:) >> A(2,:)
ans = 2 11 7 14
9
Matrices and Arrays The Colon Operator (:) >> A(1:2,1:3) ans =
10
Matrices and Arrays The Colon Operator (:) >> A(1:2,2:end) ans =
11
Matrices and Arrays The Colon Operator (:) >> A(2:3,3:4) ans =
12
Matrices and Arrays The Colon Operator (:) >> A(1:3,2:4) ans =
13
Matrices and Arrays The Colon Operator (:) >> A([1 3],:) ans =
14
Matrices and Arrays The Colon Operator (:) >> A(:,[1 4]) ans =
15
Matrices and Arrays The Colon Operator (:) >> A([1 3],[1 4])
ans =
16
Matrices and Arrays Functions
>> det(A) >> inv(A) ans = 1.0871e-12 ans = 1.0e+15 *
17
Matrices and Arrays Functions
>> diag(A) ans = 16 10 7 1
18
Matrices and Arrays Functions
>> length(A) >> length(B) ans = 4 ans = 8
19
Matrices and Arrays Functions
>> numel(A) >> numel(B) ans = 16 ans = 8
20
Matrices and Arrays Functions
>> size(A) >> size(B) ans = ans =
21
Matrices and Arrays Functions
>> max(A) >> max(B) ans = ans = 9 >> max(max(A)) ans = 16
22
Matrices and Arrays Functions
>> min(A) >> min(B) ans = ans = >> min(min(A)) ans = 1
23
Matrices and Arrays Functions
>> sort(B) >> sort(B,'ascend') ans = >> sort(B,'descend') ans =
24
Matrices and Arrays Functions
>> sort(A) or sort(A,1) >> sort(A,2) ans = ans =
25
Matrices and Arrays Functions
>> sum(A) >> sum(B) ans = ans = 34 >> sum(sum(A)) ans = 134
26
Matrices and Arrays Functions
>> tril(A) >> triu(A) ans = ans =
27
Matrices and Arrays Functions
>> magic(3) >> zeros(3) ans = ans = >> zeros(3,2) >> ones(3) ans = ans =
28
Matrices and Arrays Functions
>> rand(2) >> rand(2,3) ans = ans = >> randn(3) ans =
29
Matrices and Arrays Functions
>> mean(A) >> mean(B) ans = ans = 4.2500
30
Matrices and Arrays Functions
>> B' ans = 5 6 8 9 1 3 2
31
Matrices and Arrays Functions
ans =
32
Matrices and Arrays Functions
>> A+D >> A*D ans = ans =
33
Matrices and Arrays Functions
ans = ans =
34
Matrices and Arrays Functions
ans = ans =
35
Matrices and Arrays Functions
>> A.*D >> A./D ans = ans =
36
Matrices and Arrays Functions
>> A.^D >> sqrt(A) ans = ans =
37
Matrices and Arrays Functions
>> flip(A) >> flip(B) ans = ans =
38
Matrices and Arrays Functions
>> fliplr(A) >> fliplr(B) ans = ans =
39
Statistical Functions
Description corrcoef Correlation coefficients mean Average or mean value of array median Median value of array mode Most frequent values in array std Standard deviation var Variance
40
Variables MATLAB does not require any type declarations or dimension statements When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage If the variable already exists, MATLAB changes its contents num_students = 25 Variable names consist of a letter, followed by any number of letters, digits, or underscores MATLAB uses only the first 31 characters of a variable name. MATLAB is case sensitive
41
Numbers MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of legal numbers are e e23 1i j e5i All numbers are stored internally using the long format specified by the IEEE floating-point standard. Floating-point numbers have a finite precision of roughly 16 significant decimal digits and a finite range of roughly to
42
Operators Operators Description + Addition - Subtraction *
Multiplication / Division \ Left division (described in “Matrices and Linear Algebra” in the MATLAB documentation) ^ Power ' Complex conjugate transpose ( ) Specify evaluation order
43
Relational and Logic Operators
Description == Equal < Less than > Greater than <= Less than or equal >= Greater than or equal ~= Not equal & And | Or ~ Not
44
Functions cos(x) sin(x) tan(x) sec(x) csc(x) cot(x) acos(x) asin(x)
atan(x) atan2(y,x) exp(x) log(x) [log(x) is ln(x)] log10(x) log2(x) sqrt(x) cosh(x) sinh(x) tanh(x) sech(x) csch(x) coth(x) acosh(x) asinh(x) atanh(x)
45
Functions Function Description abs(x)
the absolute value of a number (real or complex) clc clears the command window; useful for beautifying printed output ceil(x) the nearest integer to x looking toward +1 clear clears all assigned variables close all closes all figure windows close 3 closes figure window 3 fix(x) the nearest integer to x looking toward zero mod(x,y) the integer remainder of x/y rem(x,y) round(x) the nearest integer to x sign(x) the sign of x and returns 0 if x=0
46
Special Functions pi 3.14159265… i Imaginary unit, j Same as i eps
Floating-point relative precision, realmin Smallest floating-point number,2-1022 realmax Largest floating-point number, Inf Infinity NaN Not-a-number
47
Basic Plotting Functions
Creating a Plot x = 0:pi/100:2*pi; y = sin(x); plot(x,y)
48
Basic Plotting Functions
xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function','FontSize',12)
49
Basic Plotting Functions
text(1,-1/3,'{\it Sinwave Example.}')
50
Basic Plotting Functions
Setting Axis Limits axis([xmin xmax ymin ymax]) axis([ ])
51
Basic Plotting Functions
Multiple Data Sets in One Graph x = 0:pi/100:2*pi; y = sin(x); y2 = sin(x-.25); y3 = sin(x-.5); plot(x,y,x,y2,x,y3)
52
Basic Plotting Functions
Multiple Data Sets in One Graph legend('sin(x)','sin(x-.25)','sin(x-.5)')
53
Basic Plotting Functions
Specifying Line Styles and Colors plot(x,y,'color_style_marker') x = 0:pi/100:2*pi; y = sin(x); plot(x,y,'r:+') r g b y w k m c - -- : -. + o * x s d ^ v < > p h
54
Basic Plotting Functions
Adding Plots to an Existing Graph x = 0:pi/10:2*pi; y1 = sin(x); y2 = cos(x); plot(x,y1) hold on plot(x,y2)
55
Basic Plotting Functions
Adding Plots to an Existing Graph x = 0:pi/10:2*pi; y1 = sin(x); y2 = cos(x); plot(x,y1) hold on plot(x,y2) grid on
56
Basic Plotting Functions
Multiple Plots in One Figure x = 0:pi/10:2*pi; y1 = sin(x);y3=tan(x); y2 = cos(x);y4=cot(x); subplot(2,2,1); plot(x,y1) subplot(2,2,2); plot(x,y2) subplot(2,2,3); plot(x,y3) subplot(2,2,4); plot(x,y4)
57
Basic Plotting Functions
Multiple Plots in One Figure x = 0:pi/10:2*pi; y1 = sin(x); y2 = cos(x); y3=tan(x); subplot(2,2,1); plot(x,y1) subplot(2,2,2); plot(x,y2) subplot(2,2,3:4); plot(x,y3)
58
Basic Plotting Functions
Multiple Plots in One Figure x = 0:pi/10:2*pi; y1 = sin(x); y2 = cos(x); y3=tan(x); subplot(2,2,1); plot(x,y1) subplot(2,2,3); plot(x,y2) subplot(2,2,[2 4]); plot(x,y3)
59
Complex Numbers >> A=3+4i >> B = complex(-1,-3) A =
>> A = complex(3,4) >> B=-1-3j A = i B = i
60
Complex Numbers >> abs(A) >> angle(A) ans = 5 ans = 0.9273
>> abs(B) >> angle(A)*180/pi ans = 3.1623 ans =
61
Complex Numbers >> conj(A) >> real(A) ans =
i ans = 3 >> conj(B) >> real(B) ans = i ans = -1
62
Complex Numbers >> imag(A) >> A+B ans = 4 ans =
>> imag(B) >> A-B ans = -3 ans = i
63
Complex Numbers >> A*B >> A^2 ans = 9.0000 -13.0000i ans =
>> sin(A) ans = i ans = i
64
M-File If-statement A=input('input the value of A =');
if mod(A,2)==0 disp('A is even') else disp('A is odd') end input the value of A =4 A is even input the value of A =5 A is odd
65
M-File If-statement input the value of A =5
A=input('input the value of A ='); B=input('input the value of B ='); if A>B 'greater' elseif A<B 'less' else 'equal' end input the value of A =5 input the value of B =6 ans = less input the value of A =10 input the value of B =2 ans = greater
66
M-File Switch and case input the value of A =2
A=input('input the value of A ='); switch A case {1,2,3} y=20 case 4 y=50 case 5 y=70 otherwise y=100 end input the value of A =2 y = 20 input the value of A =5 y = 70
67
M-File For input the value of A =2 A=input('input the value of A =');
B=input('input the value of B ='); sum=0; for i=A:B if mod(i,2)==0 sum=sum+i; end sum input the value of A =2 input the value of B =10 sum = 30
68
M-File while input the value of A =2
A=input('input the value of A ='); B=input('input the value of B ='); sum=0; while A<=B sum=sum+A; A=A+1; end sum input the value of A =2 input the value of B =10 sum = 54
69
M-File Continue input the value of A =2
A=input('input the value of A ='); B=input('input the value of B ='); sum=0; for i=A:B if mod(i,2)==1 continue end sum=sum+i; sum input the value of A =2 input the value of B =10 sum = 30
70
M-File Break input the value of A =2
A=input('input the value of A ='); B=input('input the value of B ='); sum=0; for i=A:10000 sum=sum+i; if i==B break end sum input the value of A =2 input the value of B =10 sum = 54
71
M-File Scripts sum=0; for i=1:10 sum=sum+i; sum1to10 end sum = sum 55
Save as sum1to10.m
72
M-File Functions function [sum]=sumAtoB(A,B) >> sumAtoB(1,5)
for i=A:B sum=sum+i; end >> sumAtoB(1,5) ans = 15 >> sumAtoB(5,20) ans = 200 Save as sumAtoB.m
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.