Chapter 4_Programming With Matlab
Chapter 4_Programming With Matlab
Structured charts:
➢ a graphical description showing how the different parts of
the program are connected together.
➢is useful in the initial stages of top-down design.
➢displays the organization of a program without showing the
details of the calculations and decision process.
➢show the connection between the main program and the
modules.
➢Is limited by their size.
Structure chart of a game program
Main Program
False
Logical
Expression
True
Statements
End
Pseudocode:
➢ used to avoid dealing immediately with the possibly
complicated syntax of the programming language.
➢uses natural language and mathematical expressions to
construct statements that look like computer statements but
without detailed syntax.
➢use some simple Matlab syntax to explain the operation of
the program.
➢is an imitation of the actual computer code.
➢useful for outlining a program before writing the detailed
code.
Sequential Operations
Compute the perimeter p and the area A of a triangle whose
sides are a, b, c. The formulas are:
z= 0 0 0
1 0 1 >> z=(x>=y)
>> z=(x<=y) z=
z= 0 1 0
1 1 1 >> z=(x==y)
z=
0 1 0
>> z=(x~=y)
z=
1 0 1
Logical operators
w=log(x)-3*log(y) False
y>=0
?
end
The values of z and w are computed True
End
else
statement group 2 True
end
Statement Statement
group 1 group 2
End
y=sqrt(x)
else
y=exp(x)-1 True
end
y=sqrt(x) y=exp(x)-1
End
Flow chart
Use if with array.
True if all of elements
Consider the following statements of x <0
x = [ 4 -9 16]
if x<0
disp (‘Some of elements of x are negative.’)
else
y = sqrt (x)
end
y=2 0+3.000i 4
Use if with array.
True if all of elements
Consider the following statements of x >=0
x = [ 4 -9 16]
if x>=0
y = sqrt (x)
else
disp (‘Some of elements of x are negative.’)
end
Logical
False
expressio
n1
True
Statement Logical
False
group 1 expressio
n2
True
Statement Statement
group 2 group 3
if x>=5 if x>=5
y=log(x) y=log(x)
else elseif x>=0
if x>=0 y=sqrt(x)
y=sqrt(x) end
end
end
Example ln x if x 10
Supposing that y = x if 0 x 10
e x − 1 if x0
if x>10
y=log(x)
elseif x>=0
y=sqrt(x)
else
y=exp(x) - 1
end
Practice
1. Given the first order equation: ax+b=0.
Write the program to find its root.
Supposing that a and b are optional inputs.
2. Given the second order equation: ax2+bx+c=0.
Write the program to find its roots.
Supposing that a, b and c are optional inputs.