Download presentation
Presentation is loading. Please wait.
1
MATLAB 2 ENGR 2194
2
Saving a Script File Script files must be saved after completion
In our class use Save As to your Z:\ or USB drive This should be the same as the working directory you specified upon starting MATLAB SAVE YOUR WORK AS YOU GO!
3
Saving a Script File The name of the script file is governed by the following rules: No spaces are allowed The name cannot start with a numeric No special characters are allowed (except underscore) Allowed: Not Allowed: Prob1a.m Prob 1a.m (blank space) Prob_1a.m 1aProb.m (can’t start with numeric) Prob-1a.m (no special characters)
4
Script File Example Goal: Create a script file that calculates hypotenuse of triangle based on Pythagoras theorem. Pythagoras theorem: 𝑎 2 + 𝑏 2 = 𝑐 2 𝑎 𝑏 𝑐
5
Script File Example Steps for creating script
Create a script file and name it Pyth.m Clear the command window Clear all variables from memory Display your name and seat number Declare height of triangle as 3 units Declare base of triangle as 4 units Find the hypotenuse of the triangle 4 3 ?
6
Script File Example Print of the script file.
Print of the output (command window).
7
Homework Example Script file to print Command window to print clc
disp(‘Student, Joe’) disp(‘ENGR 1181, Seat 99’) disp(‘MAT – Introduction’) disp(‘Problem 4’) Prob4a = cos(5*pi … … Student, Joe ENGR 1181, Seat 99 MAT – Introduction Problem 4 prob4a = 0.2846 …
8
What is a Loop ? A loop allows a group of commands in a program to be repeated. Each repetition of the loop is called a pass. Either the number of passes can be fixed. Or the loop can be terminated after some condition is satisfied.
9
Examples of basic for–end Loops
Output: 1 2 3 for k = 1:3 disp(k) end
10
Example: Simple While Loop
a=0 while a<10 a=a+1 if (a==5) continue end disp(a) end Note: conditional variable is initialized variable advances each time one end corresponds to if one end corresponds to while
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.