Lecture 8

®
Lecture 8
Basic Programming in MATLAB

p=(a+b+c );
% compute area A
s=p/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
disp('The perimeter p is')
fprintf('p=%f cm \n' , p)
disp('Area A is:')
fprintf(' A= %f cm square \n' , A)
enter a=5
enter b=7
enter c=9
The perimeter p is
p=21.000000 cm
Area A is:
A= 17.412280 cm square
The if….end structure is shown
schematically in figure given below.
If the condition expression in if statement is
true (1) the program continues to execute the

x0
6
>> x=4;
if (x>0 )
y=sqrt(x)
end
y=
2
We may nest the if statements as shown
below.
7
if ( logical expression -1)
statements
if ( logical expression-2)
Statements
end
end
Note that each if statement has its end
statement.
8
The if –else-end structure
This structure provides a means for
choosing one group of commands ,out of
a possible two groups, for execution.

>> clear
>> x= -5;
if (x>=0)
y=sqrt(x)
else
y=exp(x)-1
end
y=
-0.9933

if( x>=0)
theta=atan(y/x);
else
theta=atan(y/x)+pi;
end
disp(' the hypotheneuse is')
fprintf('r=%10.5f \n', r)
% convert theta from radians to degrees
theta=theta*(180/pi);
disp('The angle in degrees is')
fprintf('angle=%10.5f \n', theta)
Enter x=1
Enter y= 1
the hypotheneuse is
r= 1.41421
The angle in degrees is
angle= 45.00000
The if-elseif-else-end structure
This structure makes it possible to select
one out of three groups of statements.
Example
Suppose that y=ln(x) for x>10 , y=√x for
0≤x≤10 , and y=exp(x)-1 for x<0. Given x=12
Calculate y.
Solution
>> x=12;
if (x>10)
y=log(x)
elseif (x>=0)
y=sqrt(x)
else
y=exp(x)-1
end
y=
2.4849
The switch–case statement
The switch –case stamen can be used to
direct the flow of a program.
Eample
Suppose the variable angle has an integer value
that represents an angle measured in degrees
from North.Display the point on the compass
that corresponds to thjat angle
Solution
>> angle=input('Enter angle=')
switch angle
case 45
disp('Northeast')
case 135
case 225
disp('Southwest')
case 315
disp('Northwest')
otherwise
disp('Direction is unknown')
end
Enter angle=135
angle =
135
Southeast
for – end loop
In for – end loop the execution of a command
or a group of commands is repeated in a
predetermined times.
Example
for k=1:3:8
x=k^2
end
x=
1
x=
16
x=
49
Example
>> clear
>> n=input('Enter the number of terms=');
S=0;
for k=1:n
S=S+( ((-1)^k)*k )/(2^k);
end
fprintf('Sum of the series =%f\n' , S)
Enter the number of terms=25
Sum of the series = -0.222222
while – end loop
Example
>> x=0;
sum=0;
while x<=15
sum=sum+x;
x=x+1;
end
fprintf(' sum=%f \n',sum)
sum=120.0

disp('the number of terms is')
fprintf('k=%f \n',k)
disp('The sum is')
fprintf('total=%f \n',total)
the number of terms is
k=18.000000
The sum is
total=20442.000000
Example
Determine how long it will take to accumulate
at least $10 000 in a bank account if you
deposit $500 initially and $500 at the end of
each year, if the account pays 5 percent
annual interest.
solution
>> % amount is the money deposited
amount=500;
% k is the number of years
k=0;
while amount < 10000
k=k+1;
amount=500+amount*1.05;
end
disp('
')
disp('Total amount accumulated in the bank')
fprintf('amount=%10.5f \n' , amount)
disp('
')
disp('Number of years')
fprintf('k=%f \n', k)
Total amount accumulated in the bank
amount=10789.28179
Number of years
k=14.000000

Example
Evaluating functions
>> n=10;
x=linspace(0,1,n);
y=zeros(1,n);
for k=1:n
y(k)=exp( x(k) );
end
See next page
ans =
0
1.0000
0.1111 1.1175
0.2222 1.2488
0.3333 1.3956
0.4444 1.5596
0.5556 1.7429
0.6667 1.9477
0.7778 2.1766
0.8889 2.4324
1.0000 2.7183