0% found this document useful (0 votes)
60 views9 pages

MATLAB Functions and Calculations

This document contains MATLAB code defining several functions: - area_circ(radius) calculates the area of a circle given its radius - area_rect(l,b) calculates the area and perimeter of a rectangle given its length and breadth - area_sphere(radius) calculates the area of a sphere given its radius - eff(x,y,z) calculates the total efficiency of a power plant given the boiler, turbine, and generator efficiencies - ottocycle(d,S,VC) calculates the radius and efficiency of an Otto cycle engine given bore diameter, stroke, and clearance volume The code also includes examples calculating the sum of a geometric series and factorial series, and calculating

Uploaded by

202118bt961
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views9 pages

MATLAB Functions and Calculations

This document contains MATLAB code defining several functions: - area_circ(radius) calculates the area of a circle given its radius - area_rect(l,b) calculates the area and perimeter of a rectangle given its length and breadth - area_sphere(radius) calculates the area of a sphere given its radius - eff(x,y,z) calculates the total efficiency of a power plant given the boiler, turbine, and generator efficiencies - ottocycle(d,S,VC) calculates the radius and efficiency of an Otto cycle engine given bore diameter, stroke, and clearance volume The code also includes examples calculating the sum of a geometric series and factorial series, and calculating

Uploaded by

202118bt961
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

h a n g e Pro h a n g e Pro

XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

function area = area_circ(radius)


%This function is to find area of a circle
area=pi*radius^2;
end

function [area,perimeter] = area_rect(l,b)


%This function is to find area of a circle
area=l*b
perimeter=2*(l+b)
end

function area=area_sphere(radius)
%this function calculates the area of a sphere;
area=4*pi*radius^2;
end

clc
clear all
sum=0;
a=input('The first term of geometric series');
r=input('The common ratio of series');
n=5;
% sum for five terms is sought
sum=a*(r^n-1)/(r-1);
disp(sum)

clc
clear
sum=0;
fact=1;
for i=1:5
fact=fact*i
sum=sum+fact;
end
disp(sum)

clc
clear
h=500;
r=4:0.1:4.5;
for i=1:5
area(i)=2*pi*r(i)*h+pi*r(i)^2;
fprintf('\n The area of cylinder =%.2f for radius=%.2f\n',area(i),r(i));
end

function total_eff=eff(x,y,z )
% This program calculates total efficiency of power plant
%x=input('Boiler efficiency in fraction');
%y=input('turbine efficiency in fraction');
%z=input('generator efficiency in fraction');
total_eff=x*y*z;
end

function [r,efficiency]=ottocycle(d,S,VC)
h a n g e Pro h a n g e Pro
XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

% 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

function perimeter=perimetr_circle(radius)
%this function calculates the perimeter of a circle
perimeter=2*pi*radius;
end

% To generate 100 odd numbers


clc
clear
sum=0;
for i=1:200
if mod(i,2)==1
disp(i);
sum=sum+i;
else
continue
end
end
fprintf('\n The sum of first 100 odd numbers is %.2f',sum)

% This script calculates the area of a circle


% It prompts the user for the radius
% Prompt the user for the radius and calculate
% the area based on that radius
radius = input('Please enter the radius: ');
area = pi * (radius^2);
% Print all variables in a sentence format
fprintf('For a circle with a radius of %.2f\n',radius)
fprintf('the area is %.2f\n',area)

% To find the time of Projectile landing in desert


clc
clear
h=[-16,400,2400]
t=roots(h)
fprintf('It will land on desert in %.2f seconds',t(1))

% To find largest among 3 input numbers


clc
clear
a=input('\n Enter the value of a=');
b=input('\n Enter the value of b=');
c=input('\n Enter the value of c=');
h a n g e Pro h a n g e Pro
XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

%fprintf('\nThe largest among the three values is %.2f',max([a,b,c]))


if (a>b && a>c)
disp('a is largest')
else
if (b>c)
disp('b is largest')
else
disp('c is largest')
end
end

clc
clear
r=input('\nEnter radius of the circle r=');
area=pi*r^2;
fprintf('\n The area of the circle is %.2f',area);

clc
clear
r=input('\nEnter radius of the circle r=');
area=pi.*r.^2;
fprintf('\nThe area of the circle is %.2f',area);

clc
clear
large=0
for i=1:10
b(i)=input('\nEnter ten values =');
if (b(i)>large)
large=b(i)
else
continue
end
end
large

% To print first 50 fibonacci seies number


clc
clear
f1=1;
f2=1;
fprintf('%d\n %d\n',f1,f2);
for i=2:50
f3=f1+f2;
fprintf('The looo number is i=%d %d\n',i,f3);
f1=f2;
f2=f3;
end
h a n g e Pro h a n g e Pro
XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

% To generate 100 odd numbers


clc
clear
for i=1:200
if mod(i,2)==1
fprintf('%d ',i')
else
continue
end
end

% To generate 100 odd numbers


clc
clear
sum=0;
for i=1:200
if mod(i,2)==1
disp(i);
sum=sum+i;
else
continue
end
end
fprintf('\n The sum of first 100 odd numbers is %.2f',sum)

% To calculate the amount of PPF at end of 15 years


clc
clear
int_amount=0;
p=input('Enter the principal amount per month')
n=input('Enter the number of years')
r=input('Enter the yearly interest rate ')
int_amount=(p*n*r*12)/100;
maturity_amount=p*n*12+int_amount;
fprintf('\nThe maturity 15 years in PPF is %f and interest is %f
',maturity_amount,int_amount);

% To calculate the amount of PPF at end of 15 years


clc
clear
int_amount=0;
p=input('Enter the principal amount per month')
n=input('Enter the number of years')
r=input('Enter the yearly interest rate ')
maturity_amount=p*(1+(r/(100*12))^15);
fprintf('\nThe maturity 15 years in PPF is %f ',maturity_amount);
h a n g e Pro h a n g e Pro
XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

clc
clear
x=input('Enter the value of x= ');
if (x<-1)
y=1;
end
if (x>=-1 && x<=2)
y=x^2;
end
if (x>2)
y=4;
end
fprintf('The value of y is %f',y)

clc
clear
x=input('Enter the value of x= ');
if (x<-1)
y=1;
else
if (x>=-1 && x<=2)
y=x^2;
else
if (x>2)
y=4;
end
end
end
fprintf('The value of y is %f\n',y)

clc
clear
x=input('Enter the value of Fan=1, Light=2, no electric eqpt\n');
switch (x)
case 1
disp('Fan is ON');
case 2
disp('Light is ON');
otherwise
disp('Nothing is ON');
end

clc
mypick=menu('Pick your snacks','Samosa','Kachori','DHOKLA','Mirchi Pakoda');
switch mypick
case 1
disp('Ordered Snack of Samosa');

case 2
disp('Ordered Snack of Kachori')

case 3
disp('Ordered Snack of Dhoka')
h a n g e Pro h a n g e Pro
XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

case 4
disp('Ordered Snack of Mirchi Pakoda')
otherwise
disp('Nothing ordered')
end

clc
clear
large=0;
x=randi([-5 5],1,50);
for i=1:50
if large<x(i)
large=x(i)
else
continue
end
end

% To use MATLAB functions


sum=0;
n=input('Enter the value for which you need to calculate the sum');
for i=1:n
sum=sum+(i/factorial(i));
end
disp(sum)

function myfact = s121fact(n)


%This is my s121 students code for teaching functions using fact
% example
myfact=1;
for i=1:n
myfact=myfact*i;
end

% TO have 2 D PLOT
clc
clear
x=1:10;
y=sin(x);
plot(x,y,'k-')
xlabel('(x)')
ylabel('sin(x)')
h a n g e Pro h a n g e Pro
XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

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');

x=0:0.1:2;
y=exp(-x).*sin(x);
stem(x,y,'y');
bar(x,y,'y');
title('stem and bar Plot');
xlabel('x coordinate');
ylabel('function');

t = datetime(2021,10,16) +calweeks(0:6);
y = rand(1,7);
plot(t,y,'ko');

clc
clear
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
figure
contour(X,Y,Z)

clc
clear
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
figure
mesh(Z)

clc
clear
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
figure
plot3(X,Y,Z)
h a n g e Pro h a n g e Pro
XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure
mesh(Z)

[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
surf(X,Y,Z)

% File Handling Concepts


clc
clear
x=[Link];
y=[x;rand(1,5)]
fid=fopen('[Link]','w')
fprintf(fid,'%d %.2f\n',y)
fclose(fid)

clc
clear
str = '78°C 72°C 64°C 66°C 49°C';
fileID = fopen('[Link]','w');
fprintf(fileID,'%s',str);
fclose(fileID);

clc
clear
fileID = fopen('[Link]','r');
degrees = char(176);
[A,count] = fscanf(fileID, ['%d' degrees 'C']);
fclose(fileID)

function myfact = s121fact(n)


%This is my s121 students code for teaching functions using fact
% example
myfact=1;
for i=1:n
myfact=myfact*i;
end

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
h a n g e Pro h a n g e Pro
XC d XC d
F- F-

uc

uc
PD

PD
!

!
W

W
t

t
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

function sum=sum_n (n)


%UNTITLED9 Summary of this function goes here
% calculate sum of n numbers
sum=0;
for i=1:n
sum=sum+i;
end

You might also like