Assignment-3_RAjveer
Assignment-3_RAjveer
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.
extracted_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];
Y = [7 8; 9 10];
Z = [X, Y];
disp(Z);
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.
sum_odd = 0;
odd_count = 0;
number = 1;
if rem(number, 2) ~= 0
odd_count = odd_count + 1;
end
number = number + 1;
end
Q.4 For 𝜃 ranging from 0 to 2𝜋, displacement y is given below for different ranges of theta. Write a MATLAB script
using if else condi ons and plot y w.r.t 𝜃 for all the ranges. Show the plot.
[ . ]
𝑦= for 0≤𝜃≤
/ 2
𝑦=0.75−0.75(1− /
) for ≤𝜃≤2𝜋
plot(theta, y);
xlabel('\theta');
ylabel('y');
title('Displacement y as a function of \theta');
grid on;
i i. Define this polynomial as a MATLAB func on named ‘polynomial_f’ that takes x as input and returns the
value of 𝑓(𝑥).
Plot the graph of 𝑓(𝑥) in the range 𝑥∈[−3,3] with 100 elements. Show the output.
func on y = polynomial_f(x)
end
x_value = 2.5;
f_at_2_5 = polynomial_f(x_value);
roots_f = roots(coefficients);
disp(roots_f);
y_plot = polynomial_f(x_plot);
figure;
plot(x_plot, y_plot);
xlabel('x');
ylabel('f(x)');
grid on;
Days 0 5 10 15 20
Height 0.2 0.8 1.5 2.3 3.0
(meters)
Write a MATLAB code to perform the curve fi ng using a second-degree polynomial. Show the output.
degree = 2;
figure;
hold on;
hold off;
xlabel('Days');
ylabel('Height (meters)');
legend('show');
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
];
degree_5 = 5;
degree_4 = 4;
figure;
hold on;
xlabel('x');
ylabel('y');
legend('show');
grid on;
hold off;
disp(coefficients_5);
disp(' ');
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');
quiz_grades = str2num(quiz_grades_str);
midterm_grades_str = input('Enter the three midterm grades as a vector (e.g., [82 95 89]): ', 's');
midterm_grades = str2num(midterm_grades_str);
sorted_quizzes = sort(quiz_grades);
average_top_5_quizzes = mean(top_5_quizzes);
average_midterm_grade = mean(midterm_grades);
else
end
if overall_grade_numerical >= 90
le er_grade = 'A';
le er_grade = 'B';
le er_grade = 'C';
elseif overall_grade_numerical >= 60
le er_grade = 'D';
else
le er_grade = 'E';
end
disp(' ');
final_exam_grade_a = 81;
sorted_quizzes_a = sort(quiz_grades_a);
top_5_quizzes_a = sorted_quizzes_a(2:6);
average_top_5_quizzes_a = mean(top_5_quizzes_a);
average_midterm_grade_a = mean(midterm_grades_a);
else
end
if overall_grade_numerical_a >= 90
le er_grade_a = 'A';
le er_grade_a = 'B';
le er_grade_a = 'C';
le er_grade_a = 'D';
else
le er_grade_a = 'E';
end
disp(' ');
final_exam_grade_b = 81;
sorted_quizzes_b = sort(quiz_grades_b);
top_5_quizzes_b = sorted_quizzes_b(2:6);
average_top_5_quizzes_b = mean(top_5_quizzes_b);
average_midterm_grade_b = mean(midterm_grades_b);
else
end
if overall_grade_numerical_b >= 90
le er_grade_b = 'A';
le er_grade_b = 'B';
le er_grade_b = 'C';
le er_grade_b = 'D';
else
le er_grade_b = 'E';
end
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.
for i = 1:N
for j = 1:N
end
end
end
order = 10;
result_matrix = SquareMatrix(order);
disp(result_matrix);
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
figure;
plot(t, x_t);
xlabel('Time (t)');
ylabel('x(t)');
grid on;
end