CHAP-02e

advertisement
MATLAB Programming
Chapter 2
1
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MATLAB
a flagship software which was originally developed as a
matrix library. A variety of numerical functions, symbolic
computations, and visualization tools have been added to the
matrix manipulations.
c
v(ti 1 )  v(ti )  [ g  v(ti )](ti 1  ti )
m
Demo programs:
http://web.mst.edu/~ercal/228/MATLAB/1-2/analpara.m
http://web.mst.edu/~ercal/228/MATLAB/1-2/analpara2.m
http://web.mst.edu/~ercal/228/MATLAB/1-2/analpara4.m % file I/O
http://web.mst.edu/~ercal/228/MATLAB/1-2/numpara.m
http://web.mst.edu/~ercal/228/MATLAB/1-2/numpara2.m
2
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Sample Program
c
v(ti 1 )  v(ti )  [ g  v(ti )](ti 1  ti )
m
•
•
•
g=9.8;
cd=12.5;
m = 68.1;
•
•
dt = input('time increment (s):');
tf = input('final time (s):');
•
•
t (sec.)
V (m/s)
ti=0;
vi=0;
0
0
•
while (1)
2
19.60
•
•
•
•
•
dvdt = g-(cd/m)*vi;
vi = vi + dvdt*dt;
ti = ti + dt;
if ti >= tf, break, end
end
4
32.00
8
44.82
10
47.97
12
49.96
∞
53.39
•
•
disp('velocity (m/s):')
disp(vi)
m=68.1 kg; c=12.5 kg/s; g=9.8 m/s
3
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Built-in functions
Rounding functions
Display formats
sqrt(x)
round(x)
format short : 41.4286
exp(x)
fix(x) - round towards zero
format long:
abs(x)
ceil(x)
log(x)
floor(x)
log10(x)
rem(x,y) – returns the remainder
factorial(x)
after x is divided by y
(similar to % function in C)
41.42857142857143
format short e: 4.1429e+001
format long e:
4.142857142857143e+0001
TRIGONEMETRIC
format short g: 41.429
sin(x)
format long g:
sind(x)
41.4285714285714
cos(x)
format bank: 41.43
cosd(x)
tan(x)
format compact: eliminates
tand(x)
empty lines
cot(x)
format loose: adds empty lines
cotd(x)
4
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Plot function
>> x=0:1:5
x=
0
1
2
3
4
5
>> y = sin(10*x) + cos(3*x)
y=
1.0000 -1.5340
1.8731 -1.8992
1.5890 -1.0221
>> plot(x,y)
>> xlabel('x in radians')
>> ylabel('y = sin(10*x) + cos(3*x)')
>>
>> z = zeros(1,6)
% alternative way z=0*x
z=
0
0
0
0
0
0
>> plot(x,y, x,z)
>> xlabel('x in radians')
>> ylabel('y = sin(10*x) + cos(3*x)')
5
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Roots of polynomials
System of equations
>> r = [1, -2, 4]
>> x=[-1, 5]
r=
x=
1 -2
4
-1
>> A = [2, 3; -1, 4]
>> poly(r)
ans =
1 -3 -6
A=
8
>> p = poly(r)
p=
1 -3 -6
5
2
-1
3
4
>> b = A*x'
b=
8
>> solve = roots(p)
13
21
>> solveX = inv(A)*b
solve =
4.0000
-2.0000
1.0000
solveX =
-1.0000
5.0000
6
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Passing a function as an argument
The following program plots a function (passed as an argument)
between a specified range (xa, xb):
http://web.mst.edu/~ercal/228/MATLAB/1-2/myplot.m
function draw = myplot(func, xa, xb, inc, LabelX, LabelY)
X = xa:inc:xb
Y = func(X);
Z = 0*X;
plot(X,Y,X,Z);
xlabel(LabelX);
ylabel(LabelY);
function f1 = myfunc(x)
f1 = sind(x);
% stored in another file called myfunc.m
MATLAB call:
>> myplot(@myfunc, xmin, xmax, increment, LabelX, LabelY)
7
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Fundamental control structures in MATLAB
FOR-Loop
DOFOR i = start, step, final
(Loop Body)
ENDDO
sum = 0;
for i = 2:1:25
sum = sum + A[i];
end
Managing Variables
clear – removes all variables from memory
clear x y – removes only x and y from memory
who – displays a list of variables in the memory
whos – displays a list of variables in the
memory along with their size and class
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
EXCEL
• Spreadsheet that allows the user to enter and perform calculations on rows
and columns of data.
• When any value on the sheet is changed, entire calculation is updated,
therefore, spreadsheets are ideal for “what if?” sorts of analysis.
• Excel has some built in numerical capabilities including equation solving,
curve fitting and optimization.
• It has several visualization tools, such as graphs and three dimensional
plots.
• It also includes Visual Basic (VBA) as a macro language that can be used
to implement numerical calculations.
check out: http://www.anthony-vba.kefra.com/vba/vbabasic1.htm
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Download