You are on page 1of 18

Computer Programming (MATLAB)

Dr. Samir Kale


Lecture # 6
BITS Pilani Contact Session: 10.30-12.30
Pilani|Dubai|Goa|Hyderabad
BITS Pilani
Hyderabad Campus

Module: Functions and Plots


Outcomes for the Module

1. Students will be able to solve problems in functions


2. Students will be able to write MATLAB code using Plot 3 D commands

Textbook and Reference Book

T1 Stephen J. Chapman Matlab Programming for Engineers, 4th Ed. Cengage


Learning.
T2 Stormy Attaway Matlab: A Practical Introduction to Programming and
Problem Solving
R1 Amos Gilat MATLAB An Introduction with Applications, 5th Edition Wiley
Publications

TA ZC -164 L-5 BITS Pilani, Pilani Campus


Functions

function [outarg1,outarg2,…..]=fname (inarg1,inarg2,……)


% H1 comment line
% other comment line
(executable code)

(return)

(end)

4 BITS Pilani, Deemed to be University under Section 3, UGC Act


Function example
function [mean,std_dev1]= standarddeviation(v)
sum=0;
cumsum=0;
for i=1:numel(v)
mean=sum+v(i)/numel(v);
sum=mean;
std_dev1=cumsum+sqrt(v(i)-mean)^2/numel(v);
cumsum=std_dev1;
end
end

5 BITS Pilani, Deemed to be University under Section 3, UGC Act


Function example

function fact= ownfactfun(n)


fact=1
for i=1:n
fact=fact*i;
end
end

6 BITS Pilani, Deemed to be University under Section 3, UGC Act


Function example
Calculate the efficiency of a power plant using function in MATLAB if the
efficiencies of the boiler, turbine and generator are 88, 40 and 98%,
respectively.Write the function as general so that for varying efficiency it should
calculate the overall efficiency. Formula to calculate the efficiency is

function total_eff=eff(x,y,z)
x=input('Boiler efficiency in fraction');
y=input('turbine efficiency in fraction');
z=input('generator efficiency in fraction');
total_eff=x*y*z;
end

7 BITS Pilani, Deemed to be University under Section 3, UGC Act


Function example
Create a function in MATLAB to calculate the ideal air standard cycle
efficiency based on the Otto cycle for a gas engine with a cylinder bore of
50 mm, a stroke of 75 mm and a clearance volume of 21.3 cm3.Take the
inputs as Cylinder bore, clearance volume and get the outputs of
compression ratio and efficiency. The calculations are done as follows in the
snapshot

8 BITS Pilani, Deemed to be University under Section 3, UGC Act


Function example

function Efficiency=ottocycle(d,S,VC)
% d is bore diameter ENTER IN mm
% S is stroke ENTER IN mm
%VC is clearance volume ENTER IN mm3
VS=(pi*d^2*S/4)*(1/1000) ;% conversion to cm
disp(VS);
r=(VC+VS)/VC; % no difference in unit as units are
same
disp(r);
efficiency=(1- (1/(r^0.4)))*100;
end

9 BITS Pilani, Deemed to be University under Section 3, UGC Act


HW example
1. Find the mass flow rate of the liquid in lb/min flowing through a pipe of 5 cm
diameter which has density of 960 kg/m3 and viscosity 0.9 centipoise. The
Reynolds number is reported to be 3200 for the flow.

2. Steam is flowing at the rate of 2000 kg/h in a 3 NB, 40 schedule pipe at 440
kPa absolute and 453 K. Calculate the velocity of the steam in the pipeline

3. Corrosion rate are normally reported in miles per year (mpy) in the chemical
process industry. For the measurement of the rate, a corrosion test coupon is
inserted in the process stream for a definite period. The loss of weight is
measured during the period of insertion. In a particular test, a coupon of
carbon steel was kept in a cooling water circuit. The dimensions of the coupon
were measured to be 7.5 cm × 1.3 cm × 0.15 cm. Weight of the coupon before
insertion in the circuit and after exposure for 50 days were measured to be 15
g and 14.6 g respectively. Calculate the rate of corrosion. Take the density of
the carbon steel = 7.754 g/cm3. Note: 1 mpy = 0.001 in per year. [Ans. 5.3
mpy].

10 BITS Pilani, Deemed to be University under Section 3, UGC Act


Function handle

a= 1.3;
b = .2;
clear abc
c = 30; x = 1;
parabola = @(x) a*x.^2 + b*x + c; y = parabola(x)

11 BITS Pilani, Deemed to be University under Section 3, UGC Act


Plots 3 D :Concept
Using commands in MATLAB to plot a 3-D figure of the trajectory of a particle defined by
the following set of equations for x,y, and z. Use commands for proper labelling and title
x=t
y=t*cos(t)
z= e 0.2t

Solution
clear;
clc;
t=0:0.1:10*pi;
x=t;
y=t.*cos(t);
z=exp(0.2*t);
plot3(x,y,z)
title('Plot 3 Dimensional figure');
xlabel('x axis');
ylabel('y axis');
zlabel('z axis');
grid on

12 BITS Pilani, Deemed to be University under Section 3, UGC Act


Plots 3 D :Concept

x=0:0.1:2;
y=exp(-x)*sin(x);
stem(x,y);
bar(x,y);
title(‘stem and bar Plot’);
xaxis(‘x coordinate’)
yaxis(‘function’);

13 BITS Pilani, Deemed to be University under Section 3, UGC Act


Plots 3 D :Concept

14 BITS Pilani, Deemed to be University under Section 3, UGC Act


Plots 3 D :Concept

t = datetime(2014,6,28) +calweeks(0:9);
y = rand(1,10);
plot(t,y);

t = datetime('today') + caldays(1:100);
y = linspace(10,40,100) + 10*rand(1,100);
scatter(t,y)

15 BITS Pilani, Deemed to be University under Section 3, UGC Act


Plots 3 D :Concept

contour Contour plot.

contour(Z) draws a contour plot of matrix Z in the x-y plane, with the x-coordinates of the vertices corresponding
to column indices of Z and the y-coordinates corresponding to row indices of Z. The
contour levels are chosen automatically.

16 BITS Pilani, Deemed to be University under Section 3, UGC Act


Plots 3 D :Concept

x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2); f
figure
contour(X,Y,Z,'ShowText','on')

17 BITS Pilani, Deemed to be University under Section 3, UGC Act

You might also like