Download MATLAB Tutorial for Engineering Students and more Slides Computational Methods in PDF only on Docsity!
Chp1 MATLAB
OverView: Part-
Learning Goals
- Turn On MATLAB and use as a calculator
- Create Basic Cartesian Plots
- Write and Save simple āScriptā Program-
files
- Execute Conditional Statements
- IF, THEN, ELSE, >, <, >=, etc.
- Execute Loop Statements
Systems of Linear Equations cont
- We Will Use MATLABās āLeftā Division Solver
- Write the System of Eqns (below) in Matrix/Array Form ļ§ In MATLAB need to InPut the - 4x4 COEFFICIENT Matrix, A - 4x1 CONSTRAINT Vector, b












= 























ā
ā
ā ā
0
0
12
0
0 0 1 1000
1 1 1 0
0 1 0 0
1 0 1 2000
2
1
x
o
I
V
V
V
0 1 0 0 12 V
1 2
1 2
1 2
1 2
o x
o x
o x
o x
V V V I
V V V I
V V V I
V V V I
A (^) b
ļ² ā
x =
ļ²
Systems of Linear Equations cont
- Use MATLAB to Solve 4Eqns in 4Unkowns >> A = [1,0,-1,-2000; 0,1,0,0; 1,-1,1,0; 0,0,1,-1000]; >> b = [0;12;0;0]; >> Soln = A\b Soln = **9.
0.**
Row Separator Left Division ļ§ Thus the Solution by MATLAB
- 003 Amps
3 Volts
12 Volts
9 Volts
2
1
x
o
I
V
V
V
Left Div & Matrix Inverse
- Use MATLAB to Solve 4Eqns in 4Unkowns >> A = [1,0,-1,-2000; 0,1,0,0; 1,-1,1,0; 0,0,1,-1000]; >> b = [0;12;0;0]; >> Soln = A\b
The Matrix āInverseā (More on This Later)
ļ§ By MTH
x b
A =
x b
A A = A
( ) x x b
A A = = A
x b
= A \
Performing MATLAB Ops
- You can perform operations in MATLAB in two ways:
- In the interactive mode, in which all commands are entered directly in the Command window
- By running a MATLAB program stored in script āmā file.
- A Script file contains MATLAB commands, so running it is equivalent to typing all the commands - one at a time - at the Command window prompt.
- Run the file by typing its name at the Command window prompt
MATLAB Editor/Debugger
Script File Usage
- The name of a script file must begin with a letter , and may include digits and the underscore character, up to 63 characters.
- Do not give a script file the same name as a variable
- Do not give a script file the same name as a MATLAB command or function. - You can check to see if a command, function or file name already exists by using the exist command
Locating Program Errors
- To locate program errors, try:
- Use a simple version of the problem which can be checked by hand to Test your program
- Display any intermediate calculations by removing semicolons at the end of statements.
- Use the debugging features of the Editor/Debugger.
Programming Style
1. Comments section
a. The nam e of the program and any key words in the first line. b. The date created, and the creators' names in the second line. c. The definitions of the variable names for every input and output variable.
- Include definitions of variables used in the calculations and units of measurement for all input and all output variables! d. The (file)name of every user-defined function called by the program.
InPut/OutPut Commands
Command Description
disp(A) Displays the contents, but notthe name, of the array A.
disp(ātextā) Displays the text string enclosed within quotes.
x = input(ātextā)
Displays the text in quotes, waits for user input from the keyboard, and stores the value in x.
x = input(ātextā,āsā)
Displays the text in quotes, waits for user input from the keyboard, and stores the input as a TEXT STRING in x.
Script File Example
- Problem: The speed v of a falling object
dropped with no initial velocity is given as a
function of time t by v = gt.
- Where g is the Acceleration of Gravity; a CONSTANT = 32.2 ft/s^2
- Use MATLAB to Plot v as a
function of t for 0 ⤠t ⤠tf
- Where tf is the final time entered by the user.
Exploit the TextBook
- Throughout each chapter margin notes identify where key terms are introduced.
- Each chapter contains tables summarizing the MATLAB commands introduced in that chapter.
- At the end of each chapter is a summary guide to the commands covered in that chapter.
- Appendix A contains tables of MATLAB commands , grouped by category, with the appropriate page references.
- There are three indexes.
- lists MATLAB commands and symbols,
- lists SimuLink blocks
- lists topics.
MATLAB Help ā Hidden Tab