Uploaded by omojustina93

Matlab Manual

advertisement
COURSE TITLE: Automotive Engineering Lab. 1
COURSE CODE: MEC 2610
Software Module: Matlab
Course lecturers:
Drs. Asif and Kashif
[Mechanical Engineering Dept.]
Lab. Demonstrator: Oluwo Adeyinka A.
Contents
•
•
•
•
•
Introduction.
Working in Matlab Environment.
Importing and exporting data to and from MS Excel.
Simple programming techniques in Matlab.
MATLAB exercises involving:
* linear systems,
* interpolation,
* quadrature
* ordinary differential equations.
•
•
•
•
•
Working in Matlab Environment
Command window.
Workspace.
Editor.
Command history.
Opening [loading] and Saving of files.
* Matlab “Mat-files”. [Command window]
* Matlab “M-files”. [Editor]
• Entering and storing data in Matlab Mat-files.
Importing and Exporting Data To and
From MS Excel
S/N
1
2
3
4
Matlab
Command
Xlsfinfo
Description
Provide Info
on an Excel
file
Syntax
typ = xlsfinfo(filename)
[typ, desc] = xlsfinfo(filename)
[typ, desc, fmt] = xlsfinfo(filename)
Xlsread
num = xlsread(filename, sheet)
Read from an num = xlsread(filename, range)
Excel file
num = xlsread(filename, sheet, range)
Xlswrite
Write to an
Excel file
Status
completion
status of a
write
operation
xlswrite(filename, M, sheet)
xlswrite(filename, M, range)
xlswrite(filename, M, sheet, range)
status = xlswrite(filename, ...)
Simple Programming Techniques in Matlab
Example 1: XLSread
• The Microsoft Excel spreadsheet file testdata1.xls contains
this data:
1 6
2 7
3 8
4 9
5 10
To read this data into MATLAB, use this command:
• A = xlsread('testdata1.xls')
A=1 6
2 7
3 8
4 9
5 10
•
•
•
•
Example 2: XLSwrite
This example writes the following mixed text and numeric
data to the file tempdata.xls:
d = {'Time', 'Temp'; 12 98; 13 99; 14 97}
Use “xlswrite”, specifying the worksheet labeled
Temperatures, and the region within the worksheet to write
the data to.
s = xlswrite('tempdata.xls', d, 'Temperatures', 'E1')
s = 1;
The output status s shows that the write operation
succeeded.
The data appears as shown here in the output file:
Time Temp
12
98
13
99
14
97
Example 3: Solving a system of linear equations
%
•
•
•
•
•
•
•
Solving the equation Ax=B
close
clear
clc
load ‘Matrices A & B’;
x = inv(A)*B;
P = A*x - B;
xlswrite('C:\Users\Abdullah\Desktop\General\classwork', 'A=',
'sheet1', 'A2')
• statusA = xlswrite('C:\Users\Abdullah\Desktop\General\classwork',
A, 'sheet1', 'C2:E4')
• xlswrite('C:\Users\Abdullah\Desktop\General\classwork', 'B=',
'sheet1', 'A5')
• statusB = xlswrite('C:\Users\Abdullah\Desktop\General\classwork',
B, 'sheet1', 'C5:C7')
MATLAB Exercises
linear Systems
Use Matlab to solve the following system of linear
equations
• Class Example
Example 1
x + 3y - 2z = 5
3x + 5y + 6z = 7
2x + 4y + 3z = 8
• Assignments
Example 2
x+y+z=0
x – 2y + 2z = 4
x + 2y – z = 2
Example 3
x+y+z=6
2y + 5z = - 4
2x + 5y – z = 27
Plotting Graphs in Matlab
2-D plots
3-D plots
plot(x,y)
plot3(x,y,z)
title('string')
xlabel('string')
Grid
ylabel('string')
Gridding the graph space.
Example
x = -pi:pi/10:pi;
y = sin(x) - cos(x);
plot(x,y)
include title, x-label & y-label.
Line Style Specifiers
Marker Specifiers
Specifier
Line Style
Specifier
Marker Type
Solid line (def.)
+
Plus sign
-Dashed line
O
Circle
:
Dotted line
*
Asterisk
-.
Dash-dot line
.
Point
Color Specifiers
x
Cross
Specifier Color
Example
r
b
y
g
c
k
w
Red
Blue
Yellow
Green
Cyan
Black
White
Plot several graphs on one set of axes using:
i. different line styles to differentiate the graphs,
ii. different markers to differentiate the graphs,
iii. different colors to differentiate the graphs.
Interpolation
Interpolation: the process of creating equally spaced
data from irregularly spaced data.
n-D linear Interpolation: yi = interpn(x,y,xi)
1-D linear Interpolation: yi = interp1(x,y,xi)
x: must be a vector.
y: can be a scalar or vector.
xi: can be a scalar or vector.
Example 1
Generate a coarse sine curve and interpolate
over a finer abscissa.
x = 0:10;
y = sin(x); xi = 0:.25:10;
yi = interp1(x,y,xi); plot(x,y,'o',xi,yi,'x')
Example 2
t = 1900:10:1990; p = [75.995 91.972 105.711
123.203 131.669 150.697 179.323 203.212 226.505
249.633];
x = 1900:1:2000;
y = interp1(t,p,x,'spline');
z = interp1(t,p,1975) plot(t,p,'o',x,y)
Quadrature
Quadrature is a numerical method used to find the area
under the graph of a function, that is, to compute a
definite integral.
∫ f(x) dx
(a < x <b)
Syntax
q = quad(fun,a,b)
q = quad(fun,a,b,tol)
Example
1. Repeat the Excel manual question under integration topic.
2. Using atlab solve the equations
i. ∫ 1 /(x3-2x-5) dx
( 0 < x < 2)
ii. ∫ sin (t) dt
( 0 < t < 2π)
Ordinary Differential Equations [ODEs]
•
y΄ = f (t , y)
•
•
•
•
•
•
•
•
•
•
•
Syntax
y = dsolve (‘function', ‘initial condition')
y = dsolve ('DNy = f(t,y), 'y(0) = a’)
i.e
function is the diff. equation.
initial condition is the ODE’s initial condition.
Here
DN = dN /dtN,
hence, Dy means dy/dt.
D2y means d2y/dt2.
and D4y means d4y/dt4.
• Differential Equation
•
dy/dt + 4y(t) = e-t
•
y(0) = 1
MATLAB Command
y = dsolve('Dy+4*y = exp(-t)', 'y(0) = 1')
•
y = dsolve('2*x^2*D2y + 3*x*Dy - y =
2x2y′′ + 3xy′ – y = 0
0','x')
•
( ′ = d/dx)
•
•
•
•
Notes
1. If no initial conditions are included then a general solution is obtained.
2. Nonlinear equations may have multiple solutions, even when initial conditions are given.
3. The independent variable can be changed from t to x (or any other variable).
Examples
Solve the following ODEs
• d2y/dt2 = cos2t – y
y(0) = 1, dy(0)/dt = 0
Solution
Use y = dsolve('D2y=cos(2*x)-y','y(0)=1','Dy(0)=0')
• In the problem above, solve by replacing the variable ‘t’ with
‘x’.
•
d3u/dx3 = u
u(0) = 1, u’(0) = -1, u’’(0) = π.
Solution
Use u = dsolve('D3u=u','u(0)=1','Du(0)=-1','D2u(0) = pi','x‘)
• Assignments
• Solve the following ODEs using Matlab.
• 1. dy/dt = - y + t
• 2. d2y/dt2 + 6dy/dt – y = 0
• 3. dv/dt – 6v = 0
• 4. d2x/dt2 = -4x
• 5. Y’ = - 2x - y
• 6. y’’(x) + y(x) = 0
•
y(0) = 0, y(π/2) = 2.
• 7. x’’(t) + x(t) = 0
•
x(0) = 1, x’(0) = 0.
8. dy/dt + αy = 0
•
y(1) = 2, y(2) = 1.
Download