Uploaded by hurairabaig4

MATLAB REPORT

advertisement
NAME . ABU HURAIRA BAIG
CED 99 C
CMS 403566
SUBMITTED TO , DR IMRANULLAH
MATLAB LAB REPORT
QUESTION NO 1
I created a script file using .m extension and the code is pasted below.
function displayStudentGrades ()
% Ask/prompt the user to enter a numeric grade
numericGrade = input('enter numeric grade : ');
% Determintaion of grades using numeric marks
if numericGrade >= 90
StudentGrade = 'A';
elseif numericGrade >=80
StudentGrade= 'B';
elseif numericGrade >=70
StudentGrade = 'C';
elseif numericGrade >= 60
StudentGrade= 'D';
else
StudentGrade= 'F';
end
% Display the grade
fprintf('The Students grade is : %s\n',StudentGrade);
OUTPUT
QUESTION NO 2
function trianglecalculations ()
%prompt the user to input triangle sides
a = input('Enter the length of side a : ');
b = input('Enter the length of side b : ');
c = input('Enter the length of side c : ');
% calculation of semi perimeter
s = (a + b + c)/2;
% calculation of Area using Heron's formula
Area = sqrt(s*(s-a)*(s-b)*(s-c));
% calculation of perimeter
p = (a + b +c);
% Results
fprintf('Area: %.2f\n' , Area);
fprintf('Semi Perimeter : %.2f\n' , s);
fprintf('Perimeter: %.2f\n' , p);
OUTPUT
QUESTION NO 3
% Motion under gravity simulation
g = 9.8; % acceleration due to gravity (m/s^2)
u = input('Enter initial velocity (m/s): ');
t_max = input('Enter maximum time (s): ');
h = input('Enter time step size (s): ');
% Calculate time and distance
t = 0:h:t_max;
s = u*t - 0.5*g*t.^2;
% Plot the graph
figure;
plot(t, s, 'r-', 'LineWidth', 2);
title('Motion under Gravity');
xlabel('Time (s)');
ylabel('Distance (m)');
legend(['Initial Velocity = ' num2str(u) ' m/s']);
grid on;
% Display the plot
disp('Motion under gravity plot generated.');
OUTPUT
QUESTION NO 4
function result = custom_function()
% Get user input for x
x = input('Enter the value for x: ');
% Evaluate the piecewise function
if x >= 0 && x <= 10
result = sqrt(x);
elseif x > 10
result = log(x);
else
result = exp(x) - 1;
end
% Display the result
fprintf('f(%f) = %f\n', x, result);
end
OUTPUT
QUESTION NO 5
Part 1
% Define the function to integrate
f = @(x) sqrt(4 - x.^2);
% Integration limits
a = 0; % Lower limit
b = 2; % Upper limit
% Number of subintervals
n = 8;
% Simpson's 1/3 rule
h = (b - a) / n;
x = a:h:b;
y = f(x);
integral_1_3 = (h/3) * (y(1) + 4*sum(y(2:2:end-1)) + 2*sum(y(3:2:end-2)) +
y(end));
% Simpson's 3/8 rule
h_3_8 = (b - a) / (3 * n);
x_3_8 = a:h_3_8:b;
y_3_8 = f(x_3_8);
integral_3_8 = (3*h_3_8/8) * (y_3_8(1) + 3*sum(y_3_8(2:3:end-1)) +
3*sum(y_3_8(3:3:end-2)) + 2*sum(y_3_8(4:3:end-3)) + y_3_8(end));
% Display results
fprintf('Integration using Simpson''s 1/3 rule: %.6f\n', integral_1_3);
fprintf('Integration using Simpson''s 3/8 rule: %.6f\n', integral_3_8);
Output
Part 2
% Define the function
f = @(x) (sqrt(1 - exp(-x))) / x;
% Define the limits of integration
a = 0;
b = 2;
% Number of intervals (n=8)
n = 8;
% Simpson's 1/3 rule
h = (b - a) / n;
x = a:h:b;
y = f(x);
S1_3 = h/3 * (y(1) + 4*sum(y(2:2:end-1)) + 2*sum(y(3:2:end-2)) + y(end));
% Simpson's 3/8 rule (for the remaining interval)
h_3_8 = (b - x(end-2)) / 3;
y_3_8 = f(x(end-2):h_3_8:b);
S3_8 = 3 * h_3_8 / 8 * (y_3_8(1) + 3*sum(y_3_8(2:end-1)) + y_3_8(end));
% Total integration using both rules
total_integration = S1_3 + S3_8;
% Display the results
fprintf('Simpson 1/3 rule result: %.6f\n', S1_3);
fprintf('Simpson 3/8 rule result: %.6f\n', S3_8);
fprintf('Total integration result: %.6f\n', total_integration);
OUTPUT
QUESTION NO 6
% Get user input for x
x = input('Enter the value of x: ');
% Calculate f(x)
result = sqrt(x + 1);
% Display the result
fprintf('f(%f) = %f\n', x, result);
OUTPUT
QUESTION NO 7
%According to my cms number 403566
m = 6;
l = 6;
k = 5;
h = (m + l) / 100; % Step Size
x = m + k : h : m + k + 2;
y = m * x ./ (l * x + 1 ./ x.^k);
if m <= 0 || m < 4
m = 2;
end
if l <= 0 || l < 4
l = 2;
end
r1 = rem(m, 4);
r2 = rem(l, 4);
if r1 == 0
plot(x, y,
elseif r1 == 1
plot(x, y,
elseif r1 == 2
plot(x, y,
elseif r1 == 3
plot(x, y,
'yellow-.'), xlabel('x'), ylabel('y'), title('My Graph'), legend('y');
'red--'), xlabel('x'), ylabel('y'), title('My Graph'), legend('y');
'green:'), xlabel('x'), ylabel('y'), title('My Graph'), legend('y');
'black-'), xlabel('x'), ylabel('y'), title('My Graph'), legend('y');
end
hold on; % Add this line to keep the previous plot
if r2 == 0
plot(x, y,
elseif r2 == 1
plot(x, y,
elseif r2 == 2
plot(x, y,
elseif r2 == 3
plot(x, y,
end
'yellow-.'), xlabel('x'), ylabel('y'), title('My Graph'), legend('y');
'red--'), xlabel('x'), ylabel('y'), title('My Graph'), legend('y');
'green:'), xlabel('x'), ylabel('y'), title('My Graph'), legend('y');
'black-'), xlabel('x'), ylabel('y'), title('My Graph'), legend('y');
OUTPUT
Download