Matlab, Chap#6, Exercise

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Chapter No.

6
Problems

Q#1
function[cm,kg]=STtoSI(in,lb)

cm=in*2.54;
kg=lb*0.4535;
(a)

>> [cm,kg]=STtoSI(70,175)

cm =

177.8000

kg =

79.3625

(b)
>> [cm,kg]=STtoSI(72.5,138)

cm =

184.1500

kg =

62.5830
Q#2
function y = Poly(x)
y = 0.9 * x.^4 - 12 * x.^2 - 5 * x;
(a)
>> y=Poly(-3)

y=

-20.1000

>> y=Poly(5)

y=

237.5000

(b)
>> x=[-4:4];

>> y=Poly(x);

>> plot(x,y)

Q#3
function[r]=polar(th)
r = 2*(1.1-sin(th).^2);
(a)
>> [r]=polar(pi/3), [r]=polar(3*pi/2)

r=

0.7000

r=

0.2000
(b)
>> th=[0:2*pi];

>> [r]=polar(th);

>> polarplot(th,r)

Q#4
function[x, y]= maxmin(a, b, c)
x = -b / (2 * a);
y=(a*x^2+b*x+c);
(a)
>> a=input ('Enter value of a:');

b=input('Enter value of b:');

c=input('Enter value of c:');

[x,y]=maxmin(a,b,c)

Enter value of a:3

Enter value of b:-18

Enter value of c:48

x=

y=

21
Q#5
function [P] = saval (po, r,t)
P=po*(1+r/100)^t;

>> P=saval(10000,0.06,13)

P=

1.0078e+04

Q#6
function Nm=lbintoNm(lbin)
Nm=lbin/8.8507;

>> Nm=lbintoNm(500)

Nm =

56.4927

Q#7
function [alpha, beta, gamma] = triangle(a, b, c)
if a + b <= c || a + c <= b || b + c <= a
error('Invalid side lengths. Cannot form a triangle.');
end
alpha = acosd((b^2 + c^2 - a^2) / (2 * b * c));
beta = acosd((a^2 + c^2 - b^2) / (2 * a * c));
gamma = acosd((a^2 + b^2 - c^2) / (2 * a * b));

(a)
>> a=input ('Enter length of 1st side:');

b=input('Enter length of 2nd side:');


c=input('Enter length of 3rd side:');

[alpha, beta, gamma] = triangle(a, b, c)

Enter length of 1st side: 10

Enter length of 2nd side: 15

Enter length of 3rd side: 7

alpha =

34.0477

beta =

122.8783

gamma =

23.0739

Q#8
function n = unitvec(A, B)
AB =B-A;
magnitude = norm(AB);
n = AB / magnitude;
(a)
>> A=[2,6,5]; B=[-10,15,9];

>> n=unitvec(A,B)

n=

-0.7730 0.5797 0.2577


Q#9
function d = PtoLdist(x0, y0, A, B, C)
d = abs(A*x0 + B*y0 + C) / sqrt(A^2 + B^2);

>> x0=2; y0=-4; A=-2; B=3.5; C=-6;

>> d = PtoLdist(x0, y0, A, B, C)

d=

5.9537

Q#10
(a)
function[g]=fgrade(a1,a2,a3,a4,a5,m1,m2,f1)
g=0.2.*(a1+a2+a3+a4+a5)+0.2.*m1+0.2.*m2+0.4.*f1;

>> g=fgrade(10, 5, 8, 7, 9, 75, 87,69)

g=

67.8000

(b)
%Script File
StudentA=fgrade(7, 9, 5, 8, 10, 90, 70, 85);
StudentB=fgrade(6, 4, 7, 0, 7, 60, 71, 50);
StudentC=fgrade(5, 9, 10, 3, 5, 45, 75, 80);
StudentD=fgrade(8, 8, 7, 7, 9, 82, 81, 88);

%Recall Script File in Command Window

>> Studentgrades_script

>> StudentA

StudentA =
73.8000

>> StudentB

StudentB =

51

>> StudentC

StudentC =

62.4000

>> StudentD

StudentD =

75.6000

Q#11
function REQ = req (R)

if ~isnumeric(R) || any(R <= 0)


error('Input resistances must be positive numeric values.');
end

REQ = 1 / sum(1 ./ R);

>> R = [50, 75, 300, 60, 500, 180, 200];

>> REQ = req(R)

REQ =

15.1771
Q#12
function n = radint(a,b)
n = (b-a)*rand+a;

>> n=radint(1,49)

n=

31.3532

Q#13
function I= Ibeam(w, h, t)

% Calculate moment of inertia for each section


Ixo_flange=1/12*w*t^3;
Ixo_web=1/12*(w-2*t)*(h-2*t)^3;
A_flange=w*t;
A_web=(w-2*t)*(h-2*t);
dx_flange=h/2+t/2;
dx_web=0;
I_flange=Ixo_flange+A_flange*dx_flange^2;
I_web=Ixo_web+A_web*dx_web^2;

% Total moment of inertia


I = 2*I_flange + I_web;

>> w=200; h=300; t=22;

>> I= Ibeam(w, h, t)

I=

4.4656e+08
Q#14
function[smax,smin]=princstress(sxx,syy,sxy)
smax=(sxx+syy)/2+sqrt(((sxx-syy)/2)^2+sxy^2);
smin=(sxx+syy)/2-sqrt(((sxx-syy)/2)^2+sxy^2);

>> % Part(a), Units: MPa

>> [smax,smin]=princstress(150,-40,80)

smax =

179.1974

smin =

-69.1974

% Part(b), Units: ksi

>> [smax,smin]=princstress(-12,-16,-7)

smax =

-6.7199

smin =

-21.2801

Q#15
function RV = RV_lowpass(R, C, w)
RV = 1 ./ sqrt(1 + (w * R * C).^2);

% lowpass_plot_script.m
R = input('Enter the value of R (ohms): ');
C = input('Enter the value of C (Farads): ');
w_values = logspace(-2, 6, 1000); % Frequency values from 10^-2 to
10^6 rad/s
RV_values = RV_lowpass(R, C, w_values);

loglog(w_values, RV_values, 'LineWidth', 2);


xlabel('\omega (rad/s)');
ylabel('|V_o/V_i|');
title('Low-pass RC Filter Magnitude Ratio');
grid on;

% Display user-entered values on the plot


text(10^2, 0.8, sprintf('R = %d ohms, C = %duF', R, round(C*1e6)),
'FontSize', 10);

>>%Command Window

>> lowpass_plot_script

Enter the value of R (ohms): 1200

Enter the value of C (Farads): 8

Q#16
function RV=bandpass(R,C,L,w)
Z1 = w .* R .* C;
Z2 = (1 - (w.^2) .* L .* C);
RV = Z1 ./ sqrt(Z2.^2 + Z1.^2);

%bandpass script file


R = input('Enter the value of R: ');
L = input('Enter the value of L: ');
C = input('Enter the value of C: ');

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


to 10^7

% Call the user-defined function to calculate RV values


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

% Plot the result on a logarithmic scale


figure;
semilogx(w, abs(RV));
xlabel('Angular Frequency (w)');
ylabel('|RV|');
grid on;

>>%Running the Script File in Command Window

>> bandpass_script

Enter the value of R: 1100

Enter the value of L: 7

Enter the value of C: 9

You might also like