Assignment-3 Tanmay
Assignment-3 Tanmay
Q.1 Let B be a variable that contains the sentence MY NAME IS JOHN SMITH. Write the command to extract
NAME JOHN out of the string.
B(4:12);
disp(extracted_string);
1 2 3 7 8
Q.2 Let's say you have two matrices 𝑋 and 𝑌: 𝑋= and 𝑌=
4 5 6 9 10
Write a MATLAB code to append matrices 𝑋 and 𝑌 horizontally to form a new matrix 𝑍. Append matrices 𝑋 and 𝑌
ver cally to form a new matrix 𝑊 if possible else jus fy your answer.
X= [1 2 3; 4 5 6];
disp(' ');
disp('Ver cal appending of X and Y is not directly possible because they have different numbers of columns.');
disp('To append matrices ver cally, they must have the same number of columns.');
disp(' ');
Q.3 Using a for loop in MATLAB, write a program to calculate the sum of the first 10 odd numbers. Show the
output.
/ ≤𝜃≤2
𝑦=0.75−0.75(1− )2 for 𝜋
/
y(i) = 6;
elseif theta(i) > 2*pi/3 && theta(i) <= 4*pi/3 y(i) =
6 - 3 * abs(1 - 0.5 * cos(3*(theta(i) - 2*pi/3))); elseif
theta(i) > 4*pi/3 && theta(i) <= 3*pi/2
y(i) = 3;
elseif theta(i) > 3*pi/2 && theta(i) <= 7*pi/4
y(i) = 3 - 1.5 * (theta(i) - 3*pi/4).^2; elseif theta(i) >
7*pi/4 && theta(i) <= 2*pi y(i) = 0.75 - 0.75 * (1 -
(theta(i) - 7*pi/4) / (pi/4)).^2; end
end
plot(theta, y);
xlabel('\theta'); ylabel('y'); title('Displacement y
as a function of \theta'); grid on;
ii ii. Write MATLAB code to evaluate 𝑓(𝑥) at 𝑥=2.s iii iii. Write
end
on;
Days 0 5 10 15 20
Write a MATLAB code to perform the curve fi ng using a second-degree polynomial. Show the output.
days = [0, 5, 10, 15, 20]; height = [0.2, 0.8, 1.5, 2.3, 3.0];
grid on;
Q.7 For the give sca ered data, write MATLAB codes for the following curve fi ng opera ons:
(0.3, 0.7), (1.3,1.2), (3.1, 2.2), (4,5), (6.4, 5.4), (7.6, 4.6), (8.1, 4.9), (8.4, 5), (9.1, 5.9), (9.8, 6.8)
Show x-axis and y-axis labels as ‘x’ and ‘y’ respec vely and compare the plots.
data_points = [
0.3, 0.7;
1.3, 1.2;
3.1, 2.2;
4.0, 5.0;
6.4, 5.4;
7.6, 4.6;
8.1, 4.9;
8.4, 5.0;
9.1, 5.9;
9.8, 6.8
];
disp(coefficients_5);
polynomial:'); disp(coefficients_4);
Q.8 The overall grade in a course is determined from the grades of 6 quizzes, 3 midterms, and a final exam, using the
following scheme: Quizzes: Quizzes are graded on a scale from 0 to 10. The grade of the lowest quiz is dropped and
the average of the 5 quizzes with the higher grades cons tutes 30% of the course grade. Midterms and final exam:
Midterms and final exams are graded on a scale from 0 to 100. If the average of the midterm scores is higher than
the score of the final exam, the average of the midterms cons tutes 50% of the course grade and the grade of the final
exam cons tutes 20% of the course grade. If the final grade is higher than the average of the midterms, the average of
the midterms cons tutes 20% of the course grade and the grade of the final exam cons tutes 50% of the course grade.
Write a MATLAB script that determines the course grade for a student. The program first asks the user to enter the
six quiz grades (in a vector), the three midterm grades (in a vector), and the grade of the final exam. Then the
program calculates a numerical course grade (a number between 0 and 100). Finally, the program assigns a le er
grade according to the following key: 𝐺𝑟𝑎𝑑𝑒 A for ≥ 90, B for 80 ≤ 𝐺𝑟𝑎𝑑𝑒 < 90, C for 70 ≤ 𝐺𝑟𝑎𝑑𝑒 < 80, D for 60
≤
𝐺𝑟𝑎𝑑𝑒 < 70, and E for a grade lower than 60. Execute the program for the following cases: (a) Quiz grades: 6, 10,
6, 8, 7, 8. Midterm grades: 82, 95, 89. Final exam: 81. (b) Quiz grades: 9, 5, 8, 8, 7, 6. Midterm grades: 78, 82, 75.
Final exam: 81.
quiz_grades_str = input('Enter the six quiz grades as a vector (e.g., [6 10 6 8 7 8]): ', 's');
le er_grade = 'C';
', le er_grade]);
sort(quiz_grades_a); top_5_quizzes_a =
sorted_quizzes_a(2:6); average_top_5_quizzes_a = mean(top_5_quizzes_a); quiz_component_a = 0.30
er_grade_a]);
disp(' '); disp('--- Case (b) ---'); quiz_grades_b = [9, 5, 8, 8, 7, 6]; midterm_grades_b = [78, 82, 75];
Q.9 Write a MATLAB code to create a func on named ‘SquareMatrix’ that will take the input as an integer ‘N’ and
gives the output as a N×N matrix where the (𝑖,𝑗)th element is 𝑖2 + 𝑗2 using the concept of loops (for/while). Show the
MATLAB output for a square matrix of order 10×10.
Q. 10 Given the differen al equa on: +12+15𝑥=35 ; 𝑡≥0 Using MATLAB program, find: 𝑥(𝑡) when 𝑥(0) = 0 and
𝑥̇(0)=1.
func on un tled
ylabel('x(t)');