Chp#7 Ex
Chp#7 Ex
Chp#7 Ex
tny = [31 26 30 33 33 39 41 41 34 33 45 42 36 39 37 45 43 36 41 37 32 32 35 42 38
33 40 37 36 51 50];
tanc = [37 24 28 25 21 28 46 37 36 20 24 31 34 40 43 36 34 41 42 35 38 36 35 33
42 42 37 26 20 25 31];
Q:7
function y = my_function(x)
if x >= -6 && x <= -2
y = 4 * exp(x + 2);
elseif x >= -2 && x <= 2
y = x^2;
elseif x >= 2 && x <= 6
y = (x + 62)^(1/3);
else
y = 0; % For x outside the specified ranges
end
end
x_values = -6: 0.012 :6
y_values = zeros(size(x_values));
for i = 1:length(x_values)
y_values(i) = my_function(x_values(i));
end
plot(x_values, y_values, 'LineWidth', 2);
xlabel('x');
ylabel('f(x)');
title('Plot of the given function');
grid on;
Q#8
a = input('Enter a: ');
b = input('Enter b: ');
c = input('Enter c: ');
D = b^2 - 4*a*c;
if D > 0
x1 = (-b + sqrt(D)) / (2*a);
x2 = (-b - sqrt(D)) / (2*a);
disp('Two roots:')
disp([x1, x2])
elseif D == 0
x = -b / (2*a);
disp('One root:')
disp(x)
else
disp('No real roots')
end
Q :9
matrix = zeros(4, 7);
for i = 1:4
for j = 1:7
matrix(i, j) = i + j;
end
end
disp('Resulting Matrix:')
disp(matrix)
Q:10
matrix = zeros(5, 8);
for i = 1:5 % Loop through rows (using size for clarity)
for j = 1:8 % Loop through columns
if mod(i + j, 2) == 0 % Check if sum of indices is even
matrix(i, j) = (i + j) ^ 2; % Square the sum
else
matrix(i, j) = (i + j) ^ 0.5; % Take the square root
end
end
end
disp('Resulting Matrix:')
disp(matrix)
Q:11
function result = leibniz_series_sum(m)
result = 0;
for n = 0:m
result = result + (-1)^n / (2*n + 1);
end
end
% Calculate and display the results for m = 10 and m = 500
result_10 = leibniz_series_sum(10);
result_500 = leibniz_series_sum(500);
Q;12
x = [1 5 -60 8 -2 5 4 -10 0.5 3];
positive_sum = 0;
for i = 1:length(x)
if x(i) > 0
positive_sum = positive_sum + x(i);
end
end
disp('Sum of positive elements:')
disp(positive_sum)
Q: 13
num = 1; % Start from 1
while true
if mod(num, 2) == 1 && mod(num, 3) == 0 && (num^3 > 4000)
disp('The req num is:')
disp(num) % Display the result directly
break % Exit the loop
end
num = num + 2; % Check only odd numbers
end
Q:14
function y = downsort(x)
% Create a copy of the input vector to avoid modifying it
y = x;
for i = 1:length(y)-1
for j = i+1:length(y)
if y(i) < y(j)
temp = y(i);
y(i) = y(j);
y(j) = temp;
end
end
end
end
Q:16
% Get package weight and service type from user
w = input('Enter the weight of the package (in pounds): ');
s = lower(input('Enter the type of service (Ground, Air, or Overnight): ', 's'));
% Check if the weight exceeds the maximum allowed weight for each service
type
if (strcmp(s, 'ground') || strcmp(s, 'air')) && w > 50
fprintf('Service is not available for packages exceeding 50 lb.\n');
return;
elseif strcmp(s, 'overnight') && w > 10
fprintf('Service is not available for packages exceeding 10 lb.\n');
return;
end
Q :17
x = 1:50;
filtered_x = x(~(mod(x, 3) == 0 | mod(x, 4) == 0 | mod(x, 5) == 0));
disp("Original vector:")
disp(x)
disp("New vector after removing elements divisible by 3, 4, or 5:")
disp(filtered_x)
Q:18
function [theta, radius] = CartesianToPolar(x, y)
% Calculate radius
r = sqrt(x^2 + y^2);
x2 = -7; y2 = 12;
[t2, r2] = CartesianToPolar(x2, y2);
disp(['For point (', num2str(x2), ', ', num2str(y2), '):']);
disp([' Polar Coordinates: Theta = ', num2str(t2), ' degrees, Radius = ',
num2str(r2)]);
x3 = 17; y3 = -9;
[t3, r3] = CartesianToPolar(x3, y3);
disp(['For point (', num2str(x3), ', ', num2str(y3), '):']);
disp([' Polar Coordinates: Theta = ', num2str(t3), ' degrees, Radius = ',
num2str(r3)]);
x4 = 10; y4 = -6.5;
[t4, r4] = CartesianToPolar(x4, y4);
disp(['For point (', num2str(x4), ', ', num2str(y4), '):']);
disp([' Polar Coordinates: Theta = ', num2str(t4), ' degrees, Radius = ',
num2str(r4)]);
Q :19
function V = Vfuel(h)
r = 0.4;
L = 1.2;
Vcyl = pi * r^2 * h;
Vcap = (2/3) * pi * r^3;
V = Vcyl + 2 * Vcap;
V = min(V, pi * r^2 * L);
end
h = 0:0.01:2;
V = Vfuel(h);
% Ensure the grid is displayed
grid on;
plot(h, V);
xlabel('Height (m)');
ylabel('Volume (m^3)');
title('Fuel Volume in Cylindrical Tank with Hemispherical Caps');
Q:20
function v = velocity(t)
% Calculate velocity based on the piecewise function
v = zeros(size(t));
function a = acceleration(t)
% Calculate acceleration based on the derivative of the velocity function
dt = 1e-3; % Small time step for numerical differentiation
v = velocity(t);
v_next = velocity(t + dt);
a = (v_next - v) / dt;
end
% Plot velocity
subplot(2,1,1);
plot(t, v);
xlabel('Time (s)');
ylabel('Velocity (m/s)');
title('Velocity vs. Time');
% Plot acceleration
subplot(2,1,2);
plot(t, a);
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
title('Acceleration vs. Time');
Q : 21
function W = scale(x)
% Given parameters
k1 = 800; % N/m
k2 = 1700; % N/m
d = 0.02; % 20 mm converted to meters
% Plotting
figure;
plot(x_values * 100, W_values, 'LineWidth', 2);
xlabel('Tray Displacement (cm)');
ylabel('Weight (N)');
title('Weight vs Displacement');
grid on;
2. `== 'ground'` compares the entire string `lower(s)` to the string `'ground'`.
This comparison will only evaluate to true if the lowercase version of the input
string `s` is exactly `'ground'`. However, if the user inputs `'Ground'`,
`'GROUND'`, or `'grOund'`, the comparison will fail because these strings are not
exactly equal to `'ground'`.
Therefore, using `strcmpi(s, 'ground')` is more robust and flexible for checking
whether the input service type is `'ground'` irrespective of its case.