MATLAB Functions and Calculations
MATLAB Functions and Calculations
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_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
function perimeter=perimetr_circle(radius)
%this function calculates the perimeter of a circle
perimeter=2*pi*radius;
end
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
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
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
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 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)
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)
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