[go: up one dir, main page]

0% found this document useful (0 votes)
36 views16 pages

Matlab-Ex (Chp#6) .

This document contains examples of MATLAB code snippets and functions for various engineering and math problems. It provides functions to convert between units, calculate derivatives and integrals, find maximums/minimums, plot functions, and more. Detailed explanations are given for each code example and function.

Uploaded by

Falak Sher
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)
36 views16 pages

Matlab-Ex (Chp#6) .

This document contains examples of MATLAB code snippets and functions for various engineering and math problems. It provides functions to convert between units, calculate derivatives and integrals, find maximums/minimums, plot functions, and more. Detailed explanations are given for each code example and function.

Uploaded by

Falak Sher
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/ 16

Matlab(5th edition) .

Chp#6.

Exercise.

1.
Here's the MATLAB function:
Code snippet
function [cm, kg] = STtOSI(in, lb)
% Convert height in inches to centimeters
cm = in * 2.54;
% Convert weight in pounds to kilograms
kg = lb * 0.453592;
end
a) To determine the height and mass of a 5 ft. 10 in. person
who weighs 175 lb:
Code snippet

[cm, kg] = STtOSI(70, 175) % 5 ft. 10 in. is 70


inches
b) To determine your own height and weight in SI units:
Code snippet
[cm, kg] = STtOSI(YOUR_HEIGHT_IN_INCHES,
YOUR_WEIGHT_IN_POUNDS)

For example, if you are 6 ft. 2 in. (74 inches) tall and weigh
185 lb, you would type:

Code snippet
[cm, kg] = STtOSI(74, 185)
This will output your height in centimeters and weight in
kilograms.
------------------------------------------------------------------------------------

2.

Here's the MATLAB function:


Code snippet
function y = myFunction(x)
% Calculate y(x) = 0.9*x^4 - 12*x^2 - 5*x
y = 0.9 * x.^4 - 12 * x.^2 - 5 * x;
end
a) To calculate y(-3) and y(5):
Code snippet
y_minus_3 = myFunction(-3);
y_5 = myFunction(5);
disp('y(-3) = ', y_minus_3);
disp('y(5) = ', y_5);
b) To plot the function for -4≤x≤4:
Code snippet
x_values = -4:1:4; % Generate x values from -4
to 4 with a step of 1
y_values = myFunction(x_values); % Calculate
corresponding y values
plot(x_values, y_values);
title('Plot of y(x) = 0.9x^4 - 12x^2 - 5x');
xlabel('x');
ylabel('y');
grid on;

------------------------------------------------------------------------------------

3.

Here's the MATLAB function:


Code snippet
function r = rho_theta(theta)
r = 2 * (1.1 - (sin(theta)).^2);
end
a) To calculate r(π/3) and r(3π/2):
Code snippet
r_pi_3 = rho_theta(pi/3);
r_3pi_2 = rho_theta(3*pi/2);

disp('r(pi/3) = ', r_pi_3);


disp('r(3pi/2) = ', r_3pi_2);
b) To plot the function for 0≤θ≤2π:
Code snippet
theta_values = 0:0.01:2*pi; % Generate theta
values from 0 to 2*pi with a step of 0.01
r_values = rho_theta(theta_values); % Calculate
corresponding r values

polarplot(theta_values, r_values);
title('Polar Plot of r(theta) = 2*(1.1 -
(sin(theta))^2)');
grid on;

------------------------------------------------------------------------------------

4.

Here's the MATLAB function:


Code snippet
function [x, y] = maxmin(a, b, c)
% Calculate the x-coordinate of the vertex
(where the maximum or minimum occurs)
x = -b / (2 * a);

% Calculate the y-coordinate of the vertex


y = a * x^2 + b * x + c;
% Determine if it's a maximum or minimum based
on the sign of a
if a > 0
disp('Maximum at x = ', x, ', y = ', y);
else
disp('Minimum at x = ', x, ', y = ', y);
end
end
Using the function for the given functions:
a) f(x) = 3x^2 - 18x + 48:
Code snippet
[x1, y1] = maxmin(3, -18, 48);
b) f(x) = -5x^2 + 10x - 3:
Code snippet
[x2, y2] = maxmin(-5, 10, -3);
------------------------------------------------------------------------------------

5.

Here's the MATLAB function:


Code snippet
function P = saval(Po, r, t)
% Convert interest rate to decimal.
r_decimal = r / 100;

% Calculate the final value using the formula.


P = Po * (1 + r_decimal)^t;
end
Calculating the value of the investment:
Code snippet
initial_investment = 10000;
interest_rate = 6;
years = 13;
final_value = saval(initial_investment,
interest_rate, years);

disp('The final value of the savings account


after 13 years is: $', final_value)

------------------------------------------------------------------------------------

6.

Here's the MATLAB function:


Code snippet
function Nm = lbintoNm(lb_in)
% Conversion factor: 1 lb-in. = 0.1129848 N-m
conversion_factor = 0.1129848;

% Convert torque using the conversion factor


Nm = lb_in * conversion_factor;
end
Converting 500 lb-in. to N-m:
Code snippet
torque_lb_in = 500;

% Convert torque to N-m


torque_Nm = lbintoNm(torque_lb_in);

disp('Torque in N-m: ', torque_Nm)

------------------------------------------------------------------------------------

7.

Here's the MATLAB function:


Code snippet
function [al, bet, gam] = triangle(a, b, c)
% Calculate the semi-perimeter
s = (a + b + c) / 2;

% Calculate the angles using the law of


cosines
al = acosd((b^2 + c^2 - a^2) / (2 * b * c));
bet = acosd((a^2 + c^2 - b^2) / (2 * a * c));
gam = acosd((a^2 + b^2 - c^2) / (2 * a * b));
end
Using the function for the given triangles:
a) a = 10, b = 15, c = 7:
Code snippet
[al1, bet1, gam1] = triangle(10, 15, 7);
disp('Angles for triangle a = 10, b = 15, c =
7:');
disp('al = ', al1, ' degrees');
disp('bet = ', bet1, ' degrees');
disp('gam = ', gam1, ' degrees');
b) a = 6, b = 8, c = 10:
Code snippet
[al2, bet2, gam2] = triangle(6, 8, 10);
disp('Angles for triangle a = 6, b = 8, c =
10:');
disp('al = ', al2, ' degrees');
disp('bet = ', bet2, ' degrees');
disp('gam = ', gam2, ' degrees');
c) a = 200, b = 75, c = 250:
Code snippet
[al3, bet3, gam3] = triangle(200, 75, 250);
disp('Angles for triangle a = 200, b = 75, c =
250:');
disp('al = ', al3, ' degrees');
disp('bet = ', bet3, ' degrees');
disp('gam = ', gam3, ' degrees');
-------------------------------------------------------------------------------

8.

Here's the MATLAB function:


Code snippet
function n = unitvec(A, B)
% Calculate the vector pointing from A to B
AB = B - A;

% Calculate the magnitude (length) of the


vector
magnitude = norm(AB);

% Divide by the magnitude to get the unit


vector
n = AB / magnitude;
end
Using the function for the given points:
a) (2, 6, 5) to (-10, 15, 9):
Code snippet
n1 = unitvec([2, 6, 5], [-10, 15, 9]);
disp('Unit vector from (2, 6, 5) to (-10, 15,
9):')
disp(n1)
b) (-10, 15, 9) to (2, 6, 5):
Code snippet
n2 = unitvec([-10, 15, 9], [2, 6, 5]);
disp('Unit vector from (-10, 15, 9) to (2, 6,
5):')
disp(n2)
c) (1, 1, 2) to (2, 1, 1):
Code snippet
n3 = unitvec([1, 1, 2], [2, 1, 1]);
disp('Unit vector from (1, 1, 2) to (2, 1, 1):')
disp(n3)

----------------------------------------------------------------------------------

9.

Here's the MATLAB function:


Code snippet
function d = PtoLdist(xo, yo, A, B, C)
% Calculate the numerator of the distance
formula
numerator = abs(A * xo + B * yo + C);

% Calculate the denominator of the distance


formula
denominator = sqrt(A^2 + B^2);

% Calculate the distance


d = numerator / denominator;
end
Using the function for the given cases:
a) Point: (2,-4), line: -2x + 3.5y - 6 = 0:
Code snippet
d1 = PtoLdist(2, -4, -2, 3.5, -6);
disp('Distance for case a: ', d1)
b) Point: (11, 2), line: y = -2x + 6 (convert to standard form):
Code snippet
A2 = 2; % Coefficient of x in standard form
B2 = 1; % Coefficient of y in standard form
C2 = -6; % Constant term in standard form
d2 = PtoLdist(11, 2, A2, B2, C2);
disp('Distance for case b: ', d2)
------------------------------------------------------------------------------------

10.

Here's the MATLAB function fgrade:


Code snippet
function g = fgrade(R)
% Calculate the weighted average for each
student
homework_weight = 0.2;
midterm_weight = 0.2;
final_weight = 0.4;

homework_grades = R(:, 1:5); % Extract


homework grades
midterm_grades = R(:, 6:7); % Extract
midterm grades
final_exam_grades = R(:, 8); % Extract final
exam grades

g = homework_weight * mean(homework_grades, 2)
+ ...
2 * midterm_weight * mean(midterm_grades,
2) + ...
final_weight * final_exam_grades;
end
a) Calculating the grade of one student in the Command
Window:
Code snippet
grades = [10, 5, 8, 7, 9, 75, 87, 69];
final_grade = fgrade(grades)
disp('The final grade is: ', final_grade)
b) Script file to calculate grades for multiple students:
Code snippet
% Ask the user to enter the grades
num_students = input('Enter the number of
students: ');
grades = zeros(num_students, 8);

for i = 1:num_students
disp('Enter grades for student ', i);
grades(i, :) = input('Enter grades (homeworks,
midterms, final): ');
end

% Calculate final grades using fgrade


final_grades = fgrade(grades);

% Display the final grades


disp('Final Grades:')
disp(final_grades)
Example usage for the four given students:
1. Save the script file (e.g., as grade_calculator.m).
2. In the Command Window, run the script: grade_calculator
3. Enter the number of students: 4
4. Enter the grades for each student as prompted.
5. The script will display the final grades for each student

11.

Here's the MATLAB function req:


Code snippet
function Req = req(R)
% Calculate the reciprocal of each resistor
value
reciprocals = 1./R;
% Sum the reciprocals to find the reciprocal
of the equivalent resistance
reciprocal_Req = sum(reciprocals);

% Calculate the equivalent resistance


Req = 1 / reciprocal_Req;
end
Calculating the equivalent resistance for the given resistors:
Code snippet
resistors = [50, 75, 300, 60, 500, 180, 200];
Req = req(resistors);
disp('Equivalent resistance: ', Req)
------------------------------------------------------------------------------------

13.

Here's the MATLAB function Ibeam:


Code snippet
function I = Ibeam(w, h, t)
% Calculate the moment of inertia of each part
I_web = (t * (h - 2*t)^3) / 12; % Moment of
inertia of the web
I_flange = (w * t^3) / 12 + (w * t * (h/2 -
t)^2); % Moment of inertia of one flange
(including parallel axis theorem)

% Add the moments of inertia of the parts,


multiplied by 2 for both flanges
I = I_web + 2 * I_flange;
end
Using the function to determine the moment of inertia for the
given I-beam:
Code snippet
w = 200; % mm
h = 300; % mm
t = 22; % mm

I = Ibeam(w, h, t);
disp('Moment of inertia of the I-beam: ', I, '
mm^4')

14.

Here's the MATLAB function princstress:


Code snippet
function [Smax, Smin] = princstress(Sxx, Syy,
Sxy)
% Calculate the average stress
Savg = (Sxx + Syy) / 2;

% Calculate the stress difference


Sdiff = ((Sxx - Syy) / 2)^2;

% Calculate the principal stresses using the


formula
Smax = Savg + sqrt(Sdiff^2 + Sxy^2);
Smin = Savg - sqrt(Sdiff^2 + Sxy^2);
end
Using the function to determine the principal stresses for the
given cases:
a) Sxx = 150 MPa, Syy = 40 MPa, Sxy = 80 MPa:
Code snippet
[Smax1, Smin1] = princstress(150, 40, 80);
disp('Principal stresses for case a: ')
disp('Smax = ', Smax1, ' MPa')
disp('Smin = ', Smin1, ' MPa')
b) Sxx = -12 ksi, Syy = -16 ksi, Sxy = -7 ksi:
Code snippet
[Smax2, Smin2] = princstress(-12, -16, -7);
disp('Principal stresses for case b: ')
disp('Smax = ', Smax2, ' ksi')
disp('Smin = ', Smin2, ' ksi')

------------------------------------------------------------------------------------

15.

Here's the MATLAB function lowpass:


Code snippet
function RV = lowpass(R, C, w)
% Calculate the denominator of the magnitude
ratio
a = 1 + (R * w * C).^2;

% Calculate the magnitude ratio


RV = 1./sqrt(a);
end
Here's the script file to generate the plot:
Code snippet
% Get user input for R and C
R = 1200
C =8e-6
% Define the frequency range
w = logspace(-2, 6); % Create a logarithmically
spaced vector from 10^-2 to 10^6

% Calculate the magnitude ratio for each


frequency
RV = lowpass(R, C, w);

% Plot the magnitude ratio vs frequency


semilogx(w, RV); % Use semilogx for logarithmic
scale on x-axis
xlabel('Frequency (rad/s)');
ylabel('Magnitude Ratio');
title('Low-Pass RC Filter Response');
grid on;

16.

Here's the MATLAB function bandpass:


Code snippet
function RV = bandpass(R, C, L, w)
% Calculate the intermediate values
a = w * R * C;
b = (1 - L * C * w.^2).^2 + (w * R * C).^2;
c = sqrt(b);

% Calculate the magnitude ratio


RV = a ./ c;
end
Here's the script file to generate the plot:
Code snippet
% Get user input for R, L, and C
R = input('Enter the value of the resistor (in
ohms): ');
L = input('Enter the value of the inductor (in
millihenrys): ');
L = L * 1e-3; % Convert millihenrys to henrys
C = input('Enter the value of the capacitor (in
microfarads): ');
C = C * 1e-6; % Convert microfarads to farads

% Define the frequency range


w = logspace(-2, 7); % Create a logarithmically
spaced vector from 10^-2 to 10^7

% Calculate the magnitude ratio for each


frequency
RV = bandpass(R, C, L, w);

% Plot the magnitude ratio vs frequency


semilogx(w, RV); % Use semilogx for logarithmic
scale on x-axis
xlabel('Frequency (rad/s)');
ylabel('Magnitude Ratio');
title('Band-Pass Filter Response');
grid on;
To run the script file for the given cases:
1. Save the script file (e.g., as bandpass_plot.m).
2. In the Command Window, run the script: bandpass_plot
3. Enter the values for R, L, and C when prompted.
For case a (R = 1100, C = 9 μF, L = 7 mH):
 Enter 1100 for R.
 Enter 7 for L.
 Enter 9 for C.
For case b (R = 500, C = 300 μF, L = 400 mH):
 Enter 500 for R.
 Enter 400 for L.
 Enter 300 for C.

The script will generate a plot of RV vs. W for each case,


demonstrating the band-pass filter's behavior.
12.

Here's the MATLAB function req:


Code snippet
function Req = req(R)
% Calculate the reciprocal of each resistor
value
reciprocals = 1./R;

% Sum the reciprocals to find the reciprocal


of the equivalent resistance
reciprocal_Req = sum(reciprocals);

% Calculate the equivalent resistance


Req = 1 / reciprocal_Req;
end
Image depicting resistors in parallel:
Calculating the equivalent resistance for the given resistors:
Code snippet
resistors = [50, 75, 300, 60, 500, 180, 200];
Req = req(resistors);
disp('Equivalent resistance: ', Req)

You might also like