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

Assignment-3_RAjveer

Uploaded by

taag21441
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assignment-3_RAjveer

Uploaded by

taag21441
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MATLAB for Engineers (MEE1006)

Department of Mechanical Engineering, SAMM


Manipal University Jaipur
Assignment-3
NAME: Rajveer Singh Rajput

REGISTRATION NO.: 2426040035

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 = 'MY NAME IS JOHN SMITH';

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('Matrix Z (horizontally appended):');

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(' ');

disp('Matrix X has 3 columns, while Matrix Y has 2 columns.');

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;

while odd_count < 10

if rem(number, 2) ~= 0

sum_odd = sum_odd + number;

odd_count = odd_count + 1;
end

number = number + 1;

end

disp(['The sum of the first 10 odd numbers is: ', num2str(sum_odd)]);

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≤𝜃≤

𝑦=6 for ≤𝜃≤

𝑦=6−3[1−0.5cos(3(𝜃− ))] for ≤𝜃≤

𝑦=3 for ≤𝜃≤

𝑦=3−1.5( )2 for ≤𝜃≤

/ 2
𝑦=0.75−0.75(1− /
) for ≤𝜃≤2𝜋

theta = linspace(0, 2*pi, 500);


y = zeros(size(theta));
for i = 1:length(theta)
if theta(i) >= 0 && theta(i) <= pi/2
y(i) = (6 * (2*theta(i) - 0.5*sin(theta(i)))) / pi;
elseif theta(i) > pi/2 && theta(i) <= 2*pi/3
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;

Q.5 Consider a polynomial 𝑓(𝑥)=3𝑥3−2𝑥2+5𝑥−7.

i i. Define this polynomial as a MATLAB func on named ‘polynomial_f’ that takes x as input and returns the
value of 𝑓(𝑥).

ii ii. Write MATLAB code to evaluate 𝑓(𝑥) at 𝑥=2.s

iii iii. Write MATLAB code to find the roots of 𝑓(𝑥).

Plot the graph of 𝑓(𝑥) in the range 𝑥∈[−3,3] with 100 elements. Show the output.

func on y = polynomial_f(x)

y = 3*x.^3 - 2*x.^2 + 5*x - 7;

end

x_value = 2.5;

f_at_2_5 = polynomial_f(x_value);

disp(['The value of f(x) at x = ', num2str(x_value), ' is: ', num2str(f_at_2_5)]);

coefficients = [3, -2, 5, -7];

roots_f = roots(coefficients);

disp('The roots of f(x) are:');

disp(roots_f);

x_plot = linspace(-3, 3, 100);

y_plot = polynomial_f(x_plot);

figure;

plot(x_plot, y_plot);

xlabel('x');

ylabel('f(x)');

tle('Graph of f(x) = 3x^3 - 2x^2 + 5x - 7');

grid on;

Q.6 Given the following data points:

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.

days = [0, 5, 10, 15, 20];

height = [0.2, 0.8, 1.5, 2.3, 3.0];

degree = 2;

coefficients = polyfit(days, height, degree);

disp('Coefficients of the second-degree polynomial:');

disp(['a (coefficient of x^2): ', num2str(coefficients(1))]);

disp(['b (coefficient of x): ', num2str(coefficients(2))]);

disp(['c (constant term): ', num2str(coefficients(3))]);

days_fit = linspace(min(days), max(days), 100);

height_fit = polyval(coefficients, days_fit);

figure;

plot(days, height, 'o', 'DisplayName', 'Original Data Points');

hold on;

plot(days_fit, height_fit, '-', 'DisplayName', 'Second-Degree Polynomial Fit');

hold off;

xlabel('Days');

ylabel('Height (meters)');

tle('Curve Fi ng with a Second-Degree Polynomial');

legend('show');

grid on;

Q.7 For the give sca ered data, write MATLAB codes for the following curve fi ng opera ons:

a) 5 degrees polynomial (represented by ‘-’)

b) 4 degrees polynomial (represented by ‘--‘)

(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

];

x_data = data_points(:, 1);

y_data = data_points(:, 2);

degree_5 = 5;

coefficients_5 = polyfit(x_data, y_data, degree_5);

x_fit = linspace(min(x_data), max(x_data), 100);

y_fit_5 = polyval(coefficients_5, x_fit);

degree_4 = 4;

coefficients_4 = polyfit(x_data, y_data, degree_4);

y_fit_4 = polyval(coefficients_4, x_fit);

figure;

sca er(x_data, y_data, 'o', 'DisplayName', 'Sca ered Data');

hold on;

plot(x_fit, y_fit_5, '-', 'DisplayName', '5 Degrees Polynomial');

plot(x_fit, y_fit_4, '--', 'DisplayName', '4 Degrees Polynomial');

xlabel('x');

ylabel('y');

tle('Curve Fi ng with 5 and 4 Degrees Polynomials');

legend('show');

grid on;

hold off;

disp('Coefficients for 5 degrees polynomial:');

disp(coefficients_5);

disp(' ');

disp('Coefficients for 4 degrees 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');

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

final_exam_grade = input('Enter the final exam grade: ');

sorted_quizzes = sort(quiz_grades);

top_5_quizzes = sorted_quizzes(2:6); % Drop the lowest grade (first element)

average_top_5_quizzes = mean(top_5_quizzes);

quiz_component = 0.30 * average_top_5_quizzes * 10; % Scale to 0-100 and apply weight

average_midterm_grade = mean(midterm_grades);

if average_midterm_grade > final_exam_grade

midterm_component = 0.50 * average_midterm_grade;

final_exam_component = 0.20 * final_exam_grade;

else

midterm_component = 0.20 * average_midterm_grade;

final_exam_component = 0.50 * final_exam_grade;

end

overall_grade_numerical = quiz_component + midterm_component + final_exam_component;

if overall_grade_numerical >= 90

le er_grade = 'A';

elseif overall_grade_numerical >= 80

le er_grade = 'B';

elseif overall_grade_numerical >= 70

le er_grade = 'C';
elseif overall_grade_numerical >= 60

le er_grade = 'D';

else

le er_grade = 'E';

end

disp(['Numerical Course Grade: ', num2str(overall_grade_numerical)]);

disp(['Le er Grade: ', le er_grade]);

disp(' ');

disp('--- Case (a) ---');

quiz_grades_a = [6, 10, 6, 8, 7, 8];

midterm_grades_a = [82, 95, 89];

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

quiz_component_a = 0.30 * average_top_5_quizzes_a * 10;

average_midterm_grade_a = mean(midterm_grades_a);

if average_midterm_grade_a > final_exam_grade_a

midterm_component_a = 0.50 * average_midterm_grade_a;

final_exam_component_a = 0.20 * final_exam_grade_a;

else

midterm_component_a = 0.20 * average_midterm_grade_a;

final_exam_component_a = 0.50 * final_exam_grade_a;

end

overall_grade_numerical_a = quiz_component_a + midterm_component_a + final_exam_component_a;

if overall_grade_numerical_a >= 90

le er_grade_a = 'A';

elseif overall_grade_numerical_a >= 80

le er_grade_a = 'B';

elseif overall_grade_numerical_a >= 70

le er_grade_a = 'C';

elseif overall_grade_numerical_a >= 60

le er_grade_a = 'D';

else
le er_grade_a = 'E';

end

disp(['Numerical Course Grade (a): ', num2str(overall_grade_numerical_a)]);

disp(['Le er Grade (a): ', le er_grade_a]);

disp(' ');

disp('--- Case (b) ---');

quiz_grades_b = [9, 5, 8, 8, 7, 6];

midterm_grades_b = [78, 82, 75];

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

quiz_component_b = 0.30 * average_top_5_quizzes_b * 10;

average_midterm_grade_b = mean(midterm_grades_b);

if average_midterm_grade_b > final_exam_grade_b

midterm_component_b = 0.50 * average_midterm_grade_b;

final_exam_component_b = 0.20 * final_exam_grade_b;

else

midterm_component_b = 0.20 * average_midterm_grade_b;

final_exam_component_b = 0.50 * final_exam_grade_b;

end

overall_grade_numerical_b = quiz_component_b + midterm_component_b + final_exam_component_b;

if overall_grade_numerical_b >= 90

le er_grade_b = 'A';

elseif overall_grade_numerical_b >= 80

le er_grade_b = 'B';

elseif overall_grade_numerical_b >= 70

le er_grade_b = 'C';

elseif overall_grade_numerical_b >= 60

le er_grade_b = 'D';

else

le er_grade_b = 'E';

end

disp(['Numerical Course Grade (b): ', num2str(overall_grade_numerical_b)]);


disp(['Le er Grade (b): ', le er_grade_b]);

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.

func on output_matrix = SquareMatrix(N)

output_matrix = zeros(N, N); % Ini alize an NxN matrix with zeros

for i = 1:N

for j = 1:N

output_matrix(i, j) = i^2 + j^2;

end

end

end

order = 10;

result_matrix = SquareMatrix(order);

disp(['Square Matrix of order ', num2str(order), 'x', num2str(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

odefun = @(t, y) [y(2); -12*y(2) - 15*y(1) + 35];

ini al_condi ons = [0; 1]; % [x(0); x'(0)]

tspan = [0, 10];

[t, y] = ode45(odefun, tspan, ini al_condi ons);

x_t = y(:, 1);

figure;

plot(t, x_t);

xlabel('Time (t)');

ylabel('x(t)');

tle('Solu on of the Differen al Equa on');

grid on;

end

You might also like