Download presentation
Presentation is loading. Please wait.
1
MATLAB Software
2
Introduction Matlab (Matrix Laboratory) –
An interactive program that is suitable for run computations, draw graphs and much more (graphical interface). A programming language for technical computing (scripts).
3
Introduction
4
Command Window Matlab as a calculator: Lets run our first command:
Command line Matlab as a calculator: Lets run our first command: 1 + 1
5
Command Window Previous Command line Output (answer)
Current Command line The output is displayed Get the previous command by pressing up arrow keyboard Can clear window using the command clc
6
All workspace variables
Workspace Window Workspace Window All workspace variables Variable value
7
Command History Window
8
Current Directory Window
Files
9
Changing The Current Directory
10
Variable Format format short g Best of fixed or floating point format with 5 digits. format long g Best of fixed or floating point format with 15 digits for double and 7 digits for single. format short e Floating point format with 5 digits. format long e Floating point format with 15 digits for double and 7 digits for single.
11
Starting MATLAB The various forms of help available are:
help Types a matlab help text helpwin Opens a matlab help GUI demo Starts the matlab demonstration
12
Matrices Variables: Array a = 1; speed = 1500; K = [0.1 0.15 0.2];
13
Matlab Special Numbers
pi … i and j Imaginary unit eps Relative precision 2-52 Inf Infinity (divide by zero or larger then realmax) NaN Not a Number (0/0 Inf/Inf)
14
Matrices Two Dimensional Matrices: Multi Dimensional Matrices:
1 2 3 4 5 6 Multi Dimensional Matrices:
15
Useful Matrix Generators
zeros a matrix filled with zeros ones a matrix filled with ones rand a matrix with uniformly distributed random elements eye identity matrix
16
Useful Matrix Generators
>> a = zeros(2,3) a = 0 0 0 >> b = ones(2,2) b = 1 1
17
Useful Matrix Generators
>> u = rand(1,5) u = >> eye(3) ans = 1 0 0 0 1 0 0 0 1
18
Subscripting >> u(3) ans = 0.1763 >> u(1:3)
19
Subscripting >> a = [1 2 3;4 5 6;7 8 9] a = 1 2 3 4 5 6 7 8 9
ans = 8
20
Subscripting >> a(2:3,3) ans = 6 9 >> a(2,:) 4 5 6
21
Deleting Rows or Columns
>> a(:,2) = [] a = 1 3 4 6 7 9
22
Matlab Arithmetic Operators
Plus + Minus - multiply * power ^ Backslash or left matrix divide \ Slash or right matrix divide /
23
Matlab Arithmetic Operators
multiply .* Array power .^ Left array divide .\ Right array divide ./
24
Matlab Arithmetic Operators
>> [1 2;3 4] / 2 ans = >>2 \ [1 2;3 4]
25
Matlab Arithmetic Operators
>> [1 2;3 4] ^ 2 ans = >> [1 2;3 4] .^ 2
26
Matlab Arithmetic Operators
>> [1 2;3 4] .\ [5 6;7 8] ans = >> [1 2;3 4] ./ [5 6;7 8]
27
Simple Operations on Matrices
Finding the maximal number in a vector >> x = 1 : 50; >> max(x) 50 Finding the minimal number in a vector >> min(x) 1 Finding the mean of a vector >> mean(x) 25.500
28
Simple Operations on Matrices
Finding the maximal numbers in each matrix column >> x = [1 2 3;7 8 9;4 5 6]; >> max(x) ans = Think of one way to get the maximal element in the entire matrix…
29
Simple Operations on Matrices
Finding the mean of each matrix column >> mean(x) ans = How do we get the mean of each matrix row?
30
Simple Operations on Matrices
Finding the size of a matrix >> x = 1 : 50; >> size(x) ans = Finding the length of a vector >> length(x) 50
31
Simple Operations on Matrices
x =[ 2 4 6;3 6 9]; size(x) 2 3 length(x) 3
32
Sub-array searching The “find” operation >> x = [2 8 7 6 4 2 3];
>> find(x == 2) 1 6 >> find(x > 3)
33
Sub-array searching >> x = [1 2 3 7 8 9 4 5 6];
>> [h, w] = find(x > 5) h = w =
34
3D arrays - example
35
3D arrays - example
36
Interpolation Function
One-dimensional data interpolation yi = interp1(x,y,xi, 'linear') yi = interp1(x,y,xi, 'spline') yi = interp1(x,y,xi, 'cubic')
37
Interpolation Function
>> x=[ ]; >> y=[ ]; >> yi = interp1(x,y,2.5, 'linear') yi = 5
38
Interpolation Function
Two-dimensional data interpolation ZI = interp2(X,Y,Z,XI,YI)
39
Interpolation Function
Cubic spline data interpolation yi = spline(x,y,xi) >> x=[ ]; >> y=[ ]; >> yi = spline (x,y,2.5) yi = 5
40
Learning Polynomials MATLAB represents polynomials as row vectors containing coefficients ordered by descending powers. For example, consider the equation p(x) = x x + 5 To enter this polynomial into MATLAB, use » p=[ ];
41
Polynomial Roots » p = [1 0 -2 -5];
The roots function calculates the roots of a polynomial: r = roots(p) r = i i
42
Polynomial Roots Function poly returns roots to the polynomial coefficients: p2 = poly(r) p2 = e poly and roots are inverse functions
43
Polynomial Evaluation
The polyval function evaluates a polynomial at a specified value. To evaluate p(5), use polyval(p,5) ans = 110
44
Polynomial Evaluation
It is also possible to evaluate a polynomial in a matrix sense. X = [2 4 5; ; 7 1 5]; p(X) = X X + 5 Y = polyvalm(p,X) Y = p(1)*X^3 + p(2)*X^(2) + p(3)*X + P(4)*I Y =
45
Polynomials Convolution
a(x) = x x && b(x) = 4x x + 6 a = [1 2 3]; b = [4 5 6]; c = conv(a,b) c =
46
Polynomials Deconvolution
The syntax for deconv is [q,r] = deconv(c,a) q = r =
47
Polynomial Derivatives
To obtain the derivative of the polynomial p = [ ]; q = polyder(p) q =
48
Polynomial Derivatives
b = [2 4 6]; c = polyder(a,b) c =
49
Polynomial Derivatives
[q,d] = polyder(a,b) q = d = q/d is the result of the operation.
50
Polynomial Integrates
polyint Integrate polynomial analytically. >> p=[ ]; >> polyint(p,-5) ans =
51
Residue
52
Residue [r,p,k] = residue(b,a)
53
Residue b = [ ] a = [ ] [r, p, k] = residue(b,a)
54
Residue r = 1.3320 p = 1.5737 k =
55
Polynomial Curve Fitting
polyfit Fit polynomial to data. P = polyfit(X,Y,N) P(1)*X^N + P(2)*X^(N-1) P(N)*X + P(N+1).
56
Polynomial Curve Fitting
>> x = (0: 0.2:1) x = >> y=x.^2+2.*x+1 y =
57
Polynomial Curve Fitting
>> p = polyfit(x,y,2) p =
58
Polynomial Curve Fitting
pp = spline(x,y) >> x = (0: 0.2:1) x = >> y=x.^2+2.*x+1 y =
59
Polynomial Curve Fitting
>> pp = spline(x,y) pp = form: 'pp' breaks: [ ] coefs: [5x4 double] pieces: 5 order: 4 dim: 1
60
Polynomial Curve Fitting
>> yy = ppval(pp, 0.6) yy = 2.5600
61
Integrate int(S,v) is the indefinite integral of S with respect to v.
>> int('1/(1+x^2)') ans = atan(x)
62
Integrate int(S,v,a,b) is the definite integral of S with respect to v from a to b. >> int('y/(1+x^2)','x',0,1) ans = 1/4*pi*y
63
Derivative diff(X,N,DIM) is the Nth difference function along dimension DIM. >> diff('y^3*sin(x)',3,'x') ans = -y^3*cos(x) >> diff('y^3*sin(x)',2,'y') 6*y*sin(x)
64
2–D Plot Function >> x = -pi:0.01:pi; >> y=sin(x);
plot(X,Y) plots vector Y versus vector X. >> x = -pi:0.01:pi; >> y=sin(x); >> plot(x,y)
65
2–D Plot Function
66
2–D Plot Function >> plot(x)
67
2–D Plot Function If z is complex, plot(z) is equivalent to plot(real(z),imag(z)) >> z=[ i 1+i 2+4i 3+9i i i]; >> plot(z)
68
2–D Plot Function
69
2–D Plot Function >> x = -pi:0.01:pi; >> y=sin(x);
>> z=cos(x); >> plot(x,y,x,z) >> plot(x,y) >> hold on >> plot(x,z)
70
2–D Plot Function
71
2–D Plot Function
72
2–D Plot Function
73
2–D Plot Function >> x = -pi:0.5:pi; >> y=sin(x);
>> z=cos(x); >> plot(x,y,'rp') >> hold on >> plot(x,z,'kd--')
74
2–D Plot Function
75
2–D Plot Function Axis >>axes1=axes('FontName','Arial','FontSize',10,'FontWeight','bold','LineWidth',2); >> box(axes1,'on'); >> hold(axes1,'all');
76
2–D Plot Function
77
2–D Plot Function set(gcf,'Color',[1,1,1])
78
2–D Plot Function >> x = -pi:0.5:pi; >> y=sin(x);
>> plot(x,y,'k-','LineWidth',2)
79
2–D Plot Function
80
2–D Plot Function >>xlabel('\beta')
>>ylabel('y_\beta','Rotation',0) >>title('plot') >>text(0.25,0.15,'HELLOW','FontName','Arial','FontSize',10,'FontWeight','bold')
81
2–D Plot Function
82
2–D Plot Function >> x = -pi:0.5:pi; >>y=sin(x);
>>z=cos(x); >>plot(x,y,'rp') >>hold on >>plot(x,z,'kd--') >>legend('sin(x)','cos(x)','Location','NorthWest')
83
2–D Plot Function
84
2–D Plot Function
85
2–D Plot Function Axis scaling and appearance
axis([xmin xmax ymin ymax]) Grid lines for two- and three-dimensional plots grid on grid off
86
2–D Plot Function subplot divides the current figure into rectangular panes that are numbered row wise. subplot(m,n,p)
87
2–D Plot Function >> x = -pi:0.5:pi; >>y=sin(x);
>>z=cos(x); >> t=x.*sin(x) >> c=x.*cos(x)
88
2–D Plot Function >> subplot(2,2,1) >>plot(x,y)
>>plot(x,z) >>subplot(2,2,3) >>plot(x,t) >>subplot(2,2,4) >>plot(x,c)
89
2–D Plot Function
90
2–D Plot Function >> x = -pi:0.5:pi; >>y=sin(x);
>>z=cos(x); >> t=x.*sin(x) >> c=x.*cos(x)
91
2–D Plot Function >> figure(1); >> plot(x,y)
>> plot(x,z) >> figure(3); >>plot(x,t) >> figure(4); >>plot(x,c)
92
2–D Plot Function
93
2–D Plot Function
94
2–D Plot Function
95
2–D Plot Function
96
fPlot Function fplot(FUN,LIMS) plots the function FUN between the x-axis limits specified by LIMS = [XMIN XMAX]. sin(1./x), [ ])
97
fPlot Function
98
Logarithmic scale plot
>>semilogx(x,y) Create semilogarithmic plot with logarithmic x-axis >>semilogy(x,y) Create semilogarithmic plot with logarithmic y-axis
99
Logarithmic scale plot
>> x = 0:0.1:10; >> y=10.^x; >> semilogy(x,y) >> grid on
100
Logarithmic scale plot
101
Logarithmic scale plot
>>loglog(x,y) Create a loglog plot >> x = logspace(-1,2); >> y=exp(x); >> loglog(x,y,'-s') >> grid on
102
Logarithmic scale plot
103
Polar Function >>polar(theta,rho) >>t = 0:.01:2*pi;
>>polar(t,sin(2*t).*cos(2*t),'--r')
104
Polar Function
105
Bar Function Plot bar graph (vertical and horizontal) >>bar(x,y)
>>barh(x,y)
106
Bar Function >> x = -2.9:0.2:2.9; >> y=exp(-x.*x);
>> bar(x,y,'r')
107
Bar Function >> x = -2.9:0.2:2.9; >> y=exp(-x.*x);
>> barh(x,y,'r')
108
3–D Plot Function mesh(X,Y,Z) draws a wire frame mesh 1.5m 1m y x
];
109
3–D Plot Function >> x=0:0.5:1.5; >> y=0:0.5:1;
>> mesh(x,y,T) >> axis([ ])
110
3–D Plot Function >> x=0:0.5:1.5; >> y=0:0.5:1;
>> surf(x,y,T) >> axis([ ])
111
3–D Plot Function >>meshc(x,y,T) >>meshz(x,y,T)
>> waterfall(x,y,T)
112
3–D Plot Function The shading function controls the color shading of surface and patch graphics objects. >>shading flat >>shading interp
113
3–D Plot Function >>sphere(16) >>axis square
>>shading flat >>title('Flat Shading')
114
3–D Plot Function >>sphere(16) >>axis square
>>shading interp >>title('Flat Shading')
115
3–D Plot Function >> view(az,el)
116
3–D Plot Function sets the default two-dimensional view, az = 0, el = 90. sets the default three-dimensional view, az = -37.5, el = 30. >> cylinder
117
3–D Plot Function >> peaks
118
3–D Plot Function >> contour(peaks)
119
3–D Plot Function >>ezmesh(fun,domain)
x.*exp(-x.^2-y.^2),[ ])
120
M-file Each M-file function has an area of memory, separate from the MATLAB base workspace, in which it operates. This area, called the function workspace, gives each function its own workspace context.
121
M-file user_entry = input(' Expression ')
122
Conditionally execute statements
if expression1 statements1 elseif expression2 statements2 else statements3 end
123
Conditionally execute statements
124
Relational Operators == ~= < > <= >= Equal Not equal
Less than < Greater than > Less than or equal <= Greater than or equal >=
125
for for variable = expression statement ... end
126
for
127
while while expression statement ... end
128
while
129
Switch-Case switch switch_expr case case_expr 1
statement, ..., statement case case_expr 2 otherwise end
130
Switch-Case
131
Continue Pass control to next iteration of for or while loop
132
Break Terminate execution of for or while loop
133
Save & Load >>save('filename', 'var1', 'var2', ...)
>>load('filename', 'var1', 'var2', ...)
134
Clear >>clear('name1','name2','name3',...)
135
Solve solve('eqn1','eqn2',...,'eqnN')
>>[x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0') >>x= solve('p*sin(x) = r')
136
Function function [out1, out2, ...] = funname(in1, in2, ...)
137
Function
138
Function
139
Function
140
Function
141
Function
142
Nonlinear Equations >>x = fsolve(fun,x0)
fsolve finds a root (zero) of a system of nonlinear equations. >>x = fsolve(fun,x0) starts at x0 and tries to solve the equations described in fun.
143
Nonlinear Equations You want to solve the following system for x
starting at x0 = [-5 -5].
144
Nonlinear Equations
145
Nonlinear Equations
146
Nonlinear Equations >>x = fsolve(fun,x0,options)
>> options= optimset ('param1',val.1,'param2',val.2,...) >>x = fsolve(fun,x0,options) This structure solves the equations with the optimization options specified in the structure options.
147
Nonlinear Equations Display 'off' | 'iter' | 'final' MaxIter
Positive integer TolFun
148
Nonlinear Equations
149
Nonlinear Equations
150
dsolve dsolve('eqn1','eqn2', ...)
>> S = dsolve('Dx = -a*x','x(0) = 1') >> [x y] = dsolve('Dx = y', 'Dy = -x', 'x(0)=0', 'y(0)=1')
151
Numerical Solution of ODE
Initial Value Problems (IVP)
152
Numerical Solution of ODE
[T,Y] = solver(odefun, [t0 tf],y0,options)
153
Numerical Solution of ODE
options = odeset('name1',value1,'name2',value2,...)
154
Numerical Solution of ODE
155
Numerical Solution of ODE
156
Numerical Solution of ODE
157
Numerical Solution of ODE
Boundary Value Problems (BVP)
158
Numerical Solution of ODE
(BVP) (IVP)
159
Numerical Solution of ODE
sol = bvp4c(odefun,bcfun,initguess,options)
160
Numerical Solution of ODE
initguess = bvpinit([t0 tf],[y10 y20]); options = bvpset('name1',value1,'name2',value2,...)
161
Numerical Solution of ODE
162
Numerical Solution of ODE
163
Numerical Solution of ODE
164
Optimization Unconstrained nonlinear optimization
[x,fval] = fminsearch(fun,x0,options) fminsearch attempts to find a minimum of a scalar function of several variables, starting at an initial estimate.
165
Optimization A classic test example for multidimensional minimization:
166
Optimization
167
Optimization
168
Optimization Constrained nonlinear optimization
[x,fval] = fminbnd(fun,x1,x2,options) fminbnd attempts to find a minimum of a function of one variable within a fixed interval.
169
Optimization
170
Optimization
171
Optimization Other optimization functions: >> fmincon
>> fminunc >> linprog
172
EXCEL Software
173
Introduction to Excel Sheet, Row, Column, Cell
174
Introduction to Excel Name Box (Cell Address)
Formula Bar (Contents of Cell)
175
Introduction to Formulas
All Formulas begin with = Basic Formulas (numbers, cells, or both) Add + Subtract - Multiply * Divide / =10+5 =10-5 =10*5 =10/5 =B3+C3 =B3-C3 =B3*C3 =B3/C3 =B3+5 =B3-5 =B3*5 =B3/5
176
Introduction to Formulas
Formulas can reference different “Sheets” =A10*Sheet2!B5 is the value from Cell A10 of our current worksheet multiplied by the value of Cell B5 from Sheet 2
177
Introduction to Formulas
All Formulas begin with = Automatic Functions (Toolbar button Σ) Menu Insert/Function for more options
178
Figures in Excel
179
Figures in Excel
180
Figures in Excel
181
Figures in Excel
182
Figures in Excel
183
Matrix Operations in Excel
Select the cells in which the answer will appear
184
Matrix Multiplication in Excel
Enter “=mmult(“ Select the cells of the first matrix Enter comma “,” Select the cells of the second matrix Enter “)”
185
Matrix Multiplication in Excel
Enter these three key strokes at the same time: Control Shift Enter
186
Matrix Commands in Excel
Excel can perform some useful, albeit basic, matrix operations: Addition & subtraction; Scalar multiplication & division; Transpose (TRANSPOSE); Matrix multiplication (MMULT); Matrix inverse (MINVERSE); Determinant of matrix (MDETERM);
187
Excel link for use with MATLAB
188
Excel link for use with MATLAB
189
Excel link for use with MATLAB
190
Excel link for use with MATLAB
191
Excel link for use with MATLAB
192
Excel link for use with MATLAB
193
Excel link for use with MATLAB
194
Excel link for use with MATLAB
195
Optimization
196
Optimization
197
Optimization
198
Optimization
199
Optimization
200
Optimization
201
Optimization
202
Optimization
203
Optimization
204
Optimization
205
Optimization
206
Optimization Optimum distribution
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.