Mat Lab Practice Set 2
Mat Lab Practice Set 2
% Answer 1
tic
mypick = menu ('Enter your preference', 'area of circle', 'Circumfrenece of the
circle', 'surface area of spere', 'volume of sphere');
if value <0
pick = no
else
switch mypick
Case 1
function area = calcarea (radius)
area = pi*radius*radius
disp (calcarea(rad))
Case 2
function circumfrence = calccircumfrence (radius)
circumfrence = 2*pi*radius
disp (calccircumfrence (rad))
Case 3
function surface area = calcsurface (radius)
surface area = 4*pi*r^2
disp (calcsurface (rad))
Case 4
function volume = calcvolume (radius)
volume = 1.33*pi*r^3
disp (calcvolume (rad))
end
toc
clear
clc
%% Question 2
% Answer 2
tic
my_pick = menu ('Enter your preference', 'A', 'B', 'C');
x = input('Enter the value of x: ');
switch my_pick
%% Question 3
% Write a script that asks the user for a number of rows r and a number of
columns c. Then, it generates
% a r × c matrix with the following values:
% • The script will prompt and ask the user for the entries of the first row
and the first column (with
% error checking).
% • The rest of the elements each has a value equal to the sum of the element
above it and the element
% to the left.
% Here is an example of such a matrix if the user inputs r = 4, c = 5, the
entries in the first row =
% {1, 2, 3, 4, 5} and the entries in the first column = {1, 2, 3, 4}.
%
% A =( 1 2 3 4 5
% 2 4 7 11 16
% 3 7 14 25 41
% 4 11 25 50 91)
% Answer 3
tic
r = input('Enter row no: ');
c = input('Enter Column no: ');
mat = zeros(r,c);
for i = 2:r
for j = 2:c
mat(i,j) = (mat(i-1,j)+mat(i,j-1));
end
end
Answer_3 = mat;
toc