0% found this document useful (0 votes)
48 views13 pages

Matlab Assignment - 3

Uploaded by

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

Matlab Assignment - 3

Uploaded by

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

MATLAB

ASSIGNMENT – 3
Name: M.Nihal
Section: D2
Red No: 2429030038

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.

Ans) B = 'MY NAME IS JOHN SMITH';


C = extractBetween(B, 'MY ', ' SMITH');
C = strrep(C{1}, 'IS ', '');
disp(C)

Ans)
Matrices X and Y can be appended horizontally as Z = [X Y], but
cannot be appended vertically because they have a different
number of 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.
Ans) sum_odd = 0;
for i = 1:10
odd_number = 2*i -1;
sum_odd = sum_odd + odd_number;
end
disp(['Sum of 1st 10 odd numbers:' num2str(sum_odd)]);

Ans) theeta = linspace(0, 2*pi, 100);


y = zeros(size(theeta));

for i = 1:length(theeta)
t = theeta(i);
if t >= 0 && t <= pi/2
y(i) = (6 * (2*t - 0.5*sin(t)))/pi;
elseif t > pi/2 && t <= (2*pi)/3
y(i)= 6;
elseif t > (2*pi)/3 && t <= (4*pi)/3
y(i) = 6 - 3*(1 - 0.5* cos(3*(t - (2*pi)/3)));
elseif t > (4*pi)/3 && t <= (3*pi)/2
y(i) = 3;
elseif t > (3*pi)/2 && t <= (7*pi)/4
y(i) = 3 - 1.5* ((t - ((3*pi)/2)) / (pi/4))^2;
elseif t > (7*pi)/4 && t <= 2*pi
y(i) = 0.75 - 0.75 * (1 - (t - 7*pi/4)/(pi/4))^2;
end
end

figure;
plot(theeta , y , 'LineWidth', 2);
grid on;
Ans) function y = polynomial_f(x);
y = 3*x.^3 - 2*x.^2 + 5*x - 7;
end

v = polynomial_f(2);
coff = [3 -2 5 -7];
roots = roots(coff);
x = linspace(-3 , 3, 100);
y = polynomial_f(x);

figure;
plot(x, y, 'LineWidth',2);
grid on;

Ans) days = [0 5 10 15 20];


height = [0.2 0.8 1.5 2.3 3.0];

coefficients = polyfit(days, height, 2);

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


height_fit = polyval(coefficients, days_fit);

figure;
plot(days, height, 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
hold on;

plot(days_fit, height_fit, 'b-', 'LineWidth', 2);

xlabel('Days');
ylabel('Height (meters)');
title('Second-degree Polynomial Fit');
legend('Data Points', 'Fitted Curve');
grid on;
Ans)
days = [0 5 10 15 20];
height = [0.2 0.8 1.5 2.3 3.0];
coefficients = polyfit(days, height, 2);

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


height_fit = polyval(coefficients, days_fit);

figure;
plot(days, height, 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
hold on;

plot(days_fit, height_fit, 'b-', 'LineWidth', 2);

xlabel('Days');
ylabel('Height (meters)');
title('Second-degree Polynomial Fit');
legend('Data Points', 'Fitted Curve');
grid on;
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 constitutes
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 constitutes 50% of the
course grade and the grade of the final exam constitutes 20%
of the course grade. If the final grade is higher than the
average of the midterms, the average of the midterms
constitutes 20% of the course grade and the grade of the final
exam constitutes 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 letter grade according to the following key:
< 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.

Ans) % Get input from user


quiz_grades = input('Enter the six quiz grades as a vector: ');
midterm_grades = input('Enter the three midterm grades as a
vector: ');
final_exam = input('Enter the final exam grade: ');

% Drop the lowest quiz grade


quiz_grades_sorted = sort(quiz_grades);
quiz_average = mean(quiz_grades_sorted(2:end)); % Average
of top 5 quizzes
quiz_contribution = quiz_average * 10 * 0.30; % Quizzes are
out of 10, scale to 100

% Calculate midterm average


midterm_average = mean(midterm_grades);

% Decide contributions based on midterm and final


if midterm_average > final_exam
midterm_contribution = midterm_average * 0.50;
final_contribution = final_exam * 0.20;
else
midterm_contribution = midterm_average * 0.20;
final_contribution = final_exam * 0.50;
end

% Total course grade


course_grade = quiz_contribution + midterm_contribution +
final_contribution;

% Determine letter grade


if course_grade >= 90
letter_grade = 'A';
elseif course_grade >= 80
letter_grade = 'B';
elseif course_grade >= 70
letter_grade = 'C';
elseif course_grade >= 60
letter_grade = 'D';
else
letter_grade = 'E';
end

% Display results
fprintf('Numerical Course Grade: %.2f\n', course_grade);
fprintf('Letter Grade: %s\n', letter_grade);

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

Ans) function M = SquareMatrix(N)

M = zeros(N, N);

for i = 1:N
for j = 1:N
M(i, j) = i^2 + j^2;
end
end
end
result = SquareMatrix(10);

disp('The 10x10 matrix is:');


disp(result);
Ans) function dxdt = myODE(t, x)
dxdt = zeros(2,1);
dxdt(1) = x(2);
dxdt(2) = 35 - 12*x(2) - 15*x(1);
end

tspan = [0 10];

x0 = [0; 1];

[t, x] = ode45(@myODE, tspan, x0);

plot(t, x(:,1), 'LineWidth', 2);


xlabel('Time t');
ylabel('x(t)');
title('Solution of the differential equation');
grid on;

You might also like