Docsity

ENGR 1731 Lab 5: Switch-Case-Otherwise Structure in MATLAB - Prof. Maria Alba-Flores, Study notes of Engineering

Toggle sidebar
bg1
2/18/2009
1
Lecture #10
Georgia Southern University
Mechanical and Electrical Engineering Technology
Spring 2009
ENGR 1731
Computing for Engineers
Prof.Alba‐Flores
-Strings
- fprintf function
- if-elseif-else structure
%JaneDoe,ENGR1731C,Lab5February12,2009
%Thislabteachesthebasicsofswitch‐case‐otherwiseandhowitisused
DocumentationinPrograms
1.‐PoorprogramdescriptionsofLab5are:
Examples
%JohnDoe,
%Engr 1731C
%Lab5,02/12/09
%Thisprogramwillcalculatethegivenfunctionwiththenumberx
%JaneDoe,ENGR1731C,Lab502/12/09
%Thislabwillaskusertoinputanumbertoconvertittoadifferentone
%requestedbytheuser
Documentationinprograms(cont.)
2.‐AGoodprogramdescriptionofLab5is:
%Programname:lab5.m
%JaneDoe,
%February5,2009
%ENGR1731CLab5Switch‐Case‐otherwisestr ucture
%*****************************************
%

*****************************************
%Thisprogramperformsvariouscalculatorfunctions
%Thefunctionsitperformsare:sine,cosine,tangent,squareroot,
%inversesquare,naturallog,andcommonlog(log10)
%****************
%Requestforthenumbertobeprocessed
x=input('Enterthenumberx=')
3.‐AnexcellentprogramdescriptionofLab5is:
Documentationinprograms(cont.)
%Programname:lab5.m
%CreatedonFebruary12,2009byJaneDoe
%
%ENGR1731CLab5Switch‐Case‐otherwisestructure
%*****************************************
%Thisprogramperformsvariouscalculatorfunctions
%Thefunctionsitperformsare:sine,cosine,tangent,squareroot,
%inversesquare,naturallog,andcommonlog(log10)
%
****************
%

%InputVari abl e:
%x(numbertobeprocessed)
%Ifthefunctiontoevaluateissine,cosineortangent,thenthenumber
%xshouldbegiveninradians
%OutputVari abl e:
%y(isthefunctionofthenumberx)
%Theallowedfunctionsforthisprogramare
%‘s’or‘S’forsine;‘c’ or‘C’forcosine;‘t’or‘T’fortangent;
%‘r’or‘R’forsquareroot;‘i’or‘I’for1/x;‘q ’or‘Q’forsquared;
%‘l’or‘L’fornaturallog;‘o’or‘O’forlog10(x)
%Requestfor thenumbertobeprocessed
x=input(‘Enterthenumberx=‘)
Strings
Astringisasetofcharacters(letters)thatformanarray
(orstring).
Thestringisactuallyavectorwhosecomponentsarethe
numericcodesfortheletters(codesareASCII).
The length of Sis the number of characters Aquotation
Syntax
letters = 'Any character(s)'
The
length
of
S
is
the
number
of
characters
.
A
quotation
withinthestringisindicatedbytwoquotes.
Strings(cont.)
sentence = [word1 word2 word3]
% concatenates character arrays: word 1 word2 word3
% into a new character array: sentence
Examples
msg = 'You' 'are' 'right!'
n
ame =
[
'Thomas' ' R. ' 'Lee']
S = {'Hello' 'Yes' 'No' 'Goodbye'}
outside = input('It is sunny ? (Y) or (N): ', 's')
pf3
pf4

Partial preview of the text

Download ENGR 1731 Lab 5: Switch-Case-Otherwise Structure in MATLAB - Prof. Maria Alba-Flores and more Study notes Engineering in PDF only on Docsity!

Lecture

Georgia Southern University

Mechanical and Electrical Engineering Technology

Spring 2009

ENGR 1731

Computing for Engineers

Prof. Alba‐Flores

  • Strings
  • fprintf function
  • if-elseif-else structure

% Jane Doe, ENGR 1731C, Lab 5 February 12, 2009 % This lab teaches the basics of switch‐case‐otherwise and how it is used

Documentation in Programs

1.‐ Poor program descriptions of Lab 5 are:

Examples

% John Doe, % Engr 1731C % Lab 5, 02/12/ % This program will calculate the given function with the number x

% Jane Doe, ENGR 1731C, Lab 5 02/12/ % This lab will ask user to input a number to convert it to a different one % requested by the user

Documentation in programs (cont.)

2.‐ A Good program description of Lab 5 is:

% Program name: lab5.m % Jane Doe, % February 5, 2009 % ENGR 1731C Lab 5 Switch‐Case‐otherwise structure %% ********************************************************************************** % This program performs various calculator functions % The functions it performs are: sine, cosine, tangent, square root, % inverse square, natural log, and common log (log 10) % **************** % Request for the number to be processed x=input('Enter the number x= ')

3.‐ An excellent program description of Lab 5 is:

Documentation in programs (cont.)

% Program name: lab5.m % Created on February 12, 2009 by Jane Doe % % ENGR 1731C Lab 5 Switch‐Case‐otherwise structure % ***************************************** % This program performs various calculator functions % The functions it performs are: sine, cosine, tangent, square root, % inverse square, natural log, and common log (log 10) %% **************** % Input Variable: % x ( number to be processed) % If the function to evaluate is sine, cosine or tangent , then the number % x should be given in radians % Output Variable: % y ( is the function of the number x) % The allowed functions for this program are % ‘s’ or ‘S’ for sine; ‘c’ or ‘C’ for cosine; ‘t’ or ‘T’ for tangent; % ‘r’ or ‘R’ for square root; ‘i’ or ‘I’ for 1/x; ‘q’ or ‘Q’ for squared; % ‘l’ or ‘L’ for natural log; ‘o’ or ‘O’ for log 10 (x) % Request for the number to be processed x=input(‘Enter the number x= ‘)

Strings

ƒ A string is a set of characters (letters) that form an array

(or string).

ƒ The string is actually a vector whose components are the

numeric codes for the letters (codes are ASCII).

ƒ The length of S is the number of characters A quotation

Syntax

letters = 'Any character(s)'

ƒ The length of S is the number of characters. A quotation

within the string is indicated by two quotes.

Strings (cont.)

sentence = [word1 word2 word3]

% concatenates character arrays: word 1 word2 word % into a new character array: sentence

Examples

msg = 'You' 'are' 'right!'

name = ['Thomas' ' R. ' 'Lee']

S = {'Hello' 'Yes' 'No' 'Goodbye'}

outside = input('It is sunny? (Y) or (N): ', 's')

The fprintf( ) function will place output on the screen

with the specified format

fprintf ‐ Write formatted function

Requires a conversion character for each variable that will

be sent to the screen.

Conversion Character

Description Character %c Single character %d Decimal notation (signed) %e (^) Exponential notation (using a lowercase e as in 3.1415e+00)

%E Exponential notation (using an uppercase E as in 3.1415E+00)

%f (^) Fixed‐point notation %s String of characters

%s string format (i e letters not numbers)

fprintf ‐ Examples

%s string format (i.e. letters not numbers)

%d means decimal number (no format is specified)

%1.5f fix‐point format. The 1 means minimum of 1 integer,

the 5 means 5 digits to the right of the decimal point

\n means new line

fprintf('color is: %s, num1 is: %d, num2 is: %1.3f \n', 'red', 8, pi)

text='yellow', x = 55, y = 3.56;

fprintf('color is: %s, num1 is: %d, num2 is: %1.3f \n', text, x, y)

fprintf ‐ Examples

x = -5:0.5:5;

y =[x; x.^3];

Using the Matlab funciton: fprintf

% This example computes the cubic of an

% array of numbers.

% x: -5 to + 5 with increments of 0.

% Display the resultant array. % a) Not very user friendly format

disp('The cubic function is: ' )

y

% b) A more user friendly format

fprintf('The cubic function is: %1.2f %1.8f \n', y)

fprintf('The cubic function is: %1.2f %1.3f \n', y)

Decision Making & Loops

Decisions

• In most engineering applications we need to

make decisions in order to solve the problem

• Most decisions are based on the value of

some parametersome parameter.

• When we write the MATLAB program used to

find the result, the decisions will be used to

control the execution the program

Syntax

if condition

Action_A

else

Action_B

end

The if-else Statement

end

if the condition is true then

execute Action_A

else

execute Action_B

end

sentence = input('It is sunny? (Y) or (N): ', 's')

if (sentence == 'Y')

If‐else Example 1

display ('go outside')

else

display( 'take umbrella')

end

If‐elseif‐else Structure

if condition

commands 1

elseif condition

commands 2

elseif condition

If … elseif ‐ else Structure

elseif condition

commands 3

else

commands else

end

% This program illustrates the use of if-then-else % statement %************* % The program provides the user with the Tax Rate % for three different states. % The three states are: % Texas(T), Virginia(V) and Delaware(D)

If … elseif Example 2

disp('Please enter the state you want to see the tax rate')

state=input('Type T for Texas, V for Virginia or D for Delaware: ', 's')

if state== 'T' SalesTax=.0835; statename= 'Texas,';

elseif state=='V' SalesTax=.085; statename= 'Virginia,';

elseif state=='D' SalesTax=.079; statename= 'Delaware,';

else display('Error. Only allowed: T, V or D') SalesTax= 0.00 ; statename= 'NOT REGISTERED HERE,';

End

fprintf('For the state of: %s the sales tax is: %1.3f\n', statename, SalesTax)