Numerical Lab Assignment2
Numerical Lab Assignment2
)
GENG 300
A3-1. Find out the velocity of a free-falling bungee jumper using subfunction
structure, which is defined as:
𝑔𝑚 𝑔 𝐶𝑑
𝑣=√ 𝑡𝑎𝑛ℎ (√ 𝑡)
𝐶𝑑 𝑚
Code:
Output:
A3-2. Modify If.. Structure as used in exercise 3.3.1 by input and disp function. Test
the code by changing the variable ‘grade’
Code:
Output:
A3-3. For a scalar, the built-in MATLAB sign function returns the sign of its
argument (−1, 0, 1). Here’s a MATLAB session that illustrates how it works:
>> sign(25.6)
ans =
1
>> sign(-0.776)
ans =
-1
>> sign(0)
ans =
0
Output:
A3-4. Write a program using for loop to compute factorial. Suppose 5!=1x2x3x4x5.
Hints: x=1;
for i=1:n
x=x*i;
Code:
Output:
A3-5. Write a program to find out positive values from the matrix below:
10 −3 5
A=[−5 2 8 ]
4 0 −7
Hints:
[m,n]=size(A);
for i=1:m
for j=1:n
if A(i,j)……….
Code:
A = [10 -3 5; -5 2 8 ;4 0 -7]
Output:
A3-6. Calculate value of k2-50 for all integers in [-10, 10] domain but only until k2-50
becomes negative. Use while --- break structure and fprintf for displaying results.
Code:
k = -10;
while true
result = k^2 - 50;
if result < 0
break;end
k = k + 1;
end
Output:
A4-1. Using if…..elseif else structure, write a program for the following grading
policy of a University.
A (Excellent) = 90 to 100
B+ (Very Good) = 85 to <90
B (Good) = 80 to <85
Fail = <80
Note: If there is a negative value as an input, it will show an error message “negative
value is not allowed “.
Execute the program and display the results, whenever the grade is 90, -89, 85, and 79
Code:
function Grade
grade=input('Enter Your Gade: ');
if grade > 100
error('Invalid');
elseif (grade >=90 && grade <= 100)
disp('A (Excellent)');
elseif (grade >= 85 && grade < 90)
disp('B+ (Very Good)');
elseif (grade >= 80 && grade < 85)
disp('B (Good)');
elseif grade >= 0
disp('Fail');
else
error('negative value is not allowed');
end
end
Output:
A4-2. Create an index of a book by using Switch Structure with the condition below:
Chapter 1: Introduction
Chapter 2: Modeling
Chapter 3: Programming
If the chapter is not between 1-3, it will show an error message “The Index
number is not vailed”.
Execute the program and display the results, whenever the chapter number is 1, 2, 3,
and 4
Code:
function Book
chapterNo=input('Enter chapter number: ');
switch(chapterNo)
case 1
disp('Chapter 1: Introduction');
case 2
disp('Chapter 2: Modeling');
case 3
disp('Chapter 3: Programming');
otherwise
error('The Index number is not vailed');
end
end
Output:
A4-3.
(a) Plot the bungee jumper velocity by using fplot over the range from t=0 to 12 sec
by using @.
(b). Pass the function to the m-file and find out average velocity from t=1 to 12.
Code1: fplot
Code2: Passing the func. To m-file and finding the avg of velocity
function Vavg(f,xl,xu,step)
t=xl:step:xu;
v=f(t)
V_average= mean(v)
end
Nasser Al-Nohmi P a g e | 10
Numerical Methods (Lab.)
GENG 300
Output1:
Output 2:
Nasser Al-Nohmi P a g e | 11