0% found this document useful (0 votes)
73 views

Numerical Lab Assignment2

This document contains instructions for a numerical methods lab assignment involving MATLAB code. It lists the names and student IDs of three students and their instructor. It then provides six problems to solve using techniques like subfunctions, if/else statements, for loops, and switch statements. The problems involve calculating bungee jumper velocity, grading scales, book indexes, and plotting functions. The document provides hints and templates for the code and expected output.

Uploaded by

Ramo com
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Numerical Lab Assignment2

This document contains instructions for a numerical methods lab assignment involving MATLAB code. It lists the names and student IDs of three students and their instructor. It then provides six problems to solve using techniques like subfunctions, if/else statements, for loops, and switch statements. The problems involve calculating bungee jumper velocity, grading scales, book indexes, and plotting functions. The document provides hints and templates for the code and expected output.

Uploaded by

Ramo com
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Numerical Methods (Lab.

)
GENG 300

Course Number: GENG 300


Course Name: Numerical Methods Lab
Summer 2023
Lab Assignment #2
Group Number in Blackboard: 4

Student Name 1 : Abdallah Al-Amiri


ID Number 201901683
Student Name 2 : Murshed Al-Muhanadi
ID Number 201706102
Student Name 3 : Khalifa Al-Malki
ID Number 201803375

Instructor: Eng. Nasser Alnohmi

Nasser Al-Nohmi Pa ge|1


Numerical Methods (Lab.)
GENG 300

A3-1. Find out the velocity of a free-falling bungee jumper using subfunction
structure, which is defined as:

𝑔𝑚 𝑔 𝐶𝑑
𝑣=√ 𝑡𝑎𝑛ℎ (√ 𝑡)
𝐶𝑑 𝑚

m=70 g, t=10 s, cd=0.30, g=9.81 m/s2


Hints: Find out the velocity using subfunction and call it from main function.

Code:

Output:

Nasser Al-Nohmi Pa ge|2


Numerical Methods (Lab.)
GENG 300

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:

Nasser Al-Nohmi Pa ge|3


Numerical Methods (Lab.)
GENG 300

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

Develop an M-file to perform the same function by using if structure.


Code:

Output:

Nasser Al-Nohmi Pa ge|4


Numerical Methods (Lab.)
GENG 300

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:

Nasser Al-Nohmi Pa ge|5


Numerical Methods (Lab.)
GENG 300

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]

positive_values = A(A > 0);


disp("Positive values in matrix A:");
disp(positive_values);

Output:

Nasser Al-Nohmi Pa ge|6


Numerical Methods (Lab.)
GENG 300

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

fprintf('k = %d, k^2 - 50 = %d\n', k, result);

k = k + 1;
end

Output:

Nasser Al-Nohmi Pa ge|7


Numerical Methods (Lab.)
GENG 300

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

Nasser Al-Nohmi Pa ge|8


Numerical Methods (Lab.)
GENG 300

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

Nasser Al-Nohmi Pa ge|9


Numerical Methods (Lab.)
GENG 300

Output:

A4-3.

(a) Plot the bungee jumper velocity by using fplot over the range from t=0 to 12 sec
by using @.

9.81 ∗ 80 9.81 ∗ 0.25


𝑣=√ 𝑡𝑎𝑛ℎ (√ 𝑡)
0.25 80

(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

You might also like