0% found this document useful (0 votes)
6 views2 pages

Circle and Matrix Calculations in MATLAB

The document contains code for calculating geometric shapes and functions. It includes code to calculate the area and circumference of a circle, surface area and volume of a sphere, and functions A(x), B(x), and C(x) based on user input for x. It also includes code to generate a matrix with values where each element equals the sum of the element above it and to the left of it based on user-defined number of rows and columns.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Circle and Matrix Calculations in MATLAB

The document contains code for calculating geometric shapes and functions. It includes code to calculate the area and circumference of a circle, surface area and volume of a sphere, and functions A(x), B(x), and C(x) based on user input for x. It also includes code to generate a matrix with values where each element equals the sum of the element above it and to the left of it based on user-defined number of rows and columns.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

%% Question 1

% 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

case 1 % A(x) equation


if x<=0
A = x^2/2;
else
A = 2*sqrt(x);
end
disp('A')

case 2 % B(x) equation


B =3*x^3-2*x^2+1;
disp('B')

case 3 % C(x) equation


if x<-6
C = exp(-x);
elseif x>=-6 && x<3
C = exp(x);
elseif x>=3
C = exp(3);
end
disp('C')
end
toc

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

mat(1,:) = input('Enter the values of first row: ');


mat(:,1) = input('Enter the values of first Column: ');

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

You might also like