BIL113E - Introduction To Scientific and Engineering Computing (MATLAB)
BIL113E - Introduction To Scientific and Engineering Computing (MATLAB)
CONDITIONAL STATEMENTS IN
MATLAB
SWITCH-CASE
The switch-case statement is another method that can be used to affect the flow
of a program. The structure of the statement is as follows:
The program asks the user to enter the rental period and the type of car. The program
then displays the cost. If a period longer than 60 days is entered, a message “Rental is not
available for more than 60 days” is displayed.
If a rental period of less than 6 days is entered for class D, a message “Class D cars cannot
be rented for less than 6 days” is displayed.
Run the program nine times for the following cases: Class B, for 3, 14, and 50 days. Class
C, for 20, 28 and 61 days. Class D for 6, 18, and 60 days.
nested if
rental_period=input('Enter the rental period ');
car_type=input('Enter the type of car ','s');
if rental_period>60
fprintf('Rental is not available for more than 60 days\n');
else
if car_type=='B'
if (rental_period>=1)&&(rental_period<=6)
cost=rental_period*27;
elseif (rental_period>=7)&&(rental_period<=27)
cost=162+(rental_period-7)*25;
else
cost=662+(rental_period-28)*23;
end
elseif car_type=='C'
if (rental_period>=1)&&(rental_period<=6)
cost=rental_period*34;
elseif (rental_period>=7)&&(rental_period<=27)
cost=204+(rental_period-7)*31;
else
cost=284+(rental_period-28)*28;
end
else
if (rental_period>=1)&&(rental_period<=6)
cost=0;
elseif (rental_period>=7)&&(rental_period<=27)
cost=276+(rental_period-7)*43;
else
cost=1136+(rental_period-28)*38;
end
end
if cost==0
disp('Class D cars cannot be rented for less than 6 days');
else
fprintf('Total cost is %.2f\n',cost);
end
end
Simple Plot
X = [ 1 : 2 : 13 ];
Y = 2*x + 5*x^2;
plot ( X , Y )
grid
xlabel (‘time’)
ylabel (‘2x+5x^2’)
Plot-Example
>> x = 0:pi/30:2*pi; % x vector, 0 <= x <= 2*pi, increments of pi/30
>> y = sin(3*x); % vector of y values
>> plot(x,y) % create the plot
>> xlabel('x (radians)'); % label the x-axis
>> ylabel('sine function'); % label the y-axis
>> title('sin(3*x)'); % put a title on the plot
Multiple Plots
x = linspace(-pi, pi, 100);
y = sin(x);
z = cos(x);
plot(x, y, x, z);
Logarithmic axis scaling
Command Name Plot type
plot(x,y) linear y vs linear x
loglog(x,y) log(y) versus log(x)
semilogx(x,y) y versus log(x)
semilogy(x,y) log(y) versus x
Subplots
It allows you to split the graph window not subwindows.
Example:
x=0:0.5:5;
y=5*x.^2;
subplot (2,2,1),plot (x,y)
subplot (2,2,2),semilogx(x,y)
subplot(2,2,3),semilogy(x,y)
subplot(2,2,4),loglog(x,y)
x = linspace(0,10);
y1 = sin(x);
y2 = sin(2*x);
y3 = sin(4*x);
y4 = sin(8*x);
figure
subplot(2,2,1)
plot(x,y1)
title('Subplot 1: sin(x)')
subplot(2,2,2)
plot(x,y2)
title('Subplot 2: sin(2x)')
subplot(2,2,3)
plot(x,y3)
title('Subplot 3: sin(4x)')
subplot(2,2,4) Change the axis limits so that the x-axis ranges
from to and the y-axis ranges from -1.5 to 1.5.
plot(x,y4) axis([0 2*pi -1.5 1.5])
title('Subplot 4: sin(8x)')