1 / 6100%
1
ENGR 133, PS-05
Authored by: Joseph Melchior on 2/21/2023
Exercise Problem 4.21
Problem Statement
Write a function (fxy) that evaluates the following function. Then test your function for all four cases.
Pseudocode
• Initialize Function
• Perform Calculations
• Display Results
Solution
Function Definition from Function file
Perform Calculations
z = -36
Display Results
clc, clear, close all
% function z =fxy(x,y)
% if (x>=0) && (y>=0) % test case 1
% z = x+y;
% elseif (x>=0) && (y<=0) % test case 2
% z = x-y;
% elseif (x<0) && (y>=0) % test case 3
% z = -(x.^2)*y;
% elseif (x<0) && (y<0) % test case 4
% z = -(x.^2)*y^2
% end
% end
z1 = fxy(2,2); % testing case 1
z2 = fxy(2,-3); % testing case 2
z3 = fxy(-2,2); % testing case 3
z4 = fxy(-2,-3); % testing case 4
fprintf('Test Case 1: %2.f',z1)
2
Test Case 1: 4
fprintf('Test
Case
2:
%2.f',z2)
Test Case 2: 5
fprintf('Test
Case
3:
%2.f',z3)
Test Case 3: -8
fprintf('Test
Case
4:
%2.f',z4)
Test Case 4: -36
Exercise Problem 4.31
Problem Statement
Write a program that plots the voltage in a diode over time t. For an ideal diode the voltage across the resistor is
given by:
a)
For a more accurate model of the diode the offset voltage is given by:
Suppose the supply voltage is
Pseudocode
• Initialize Variables
• Perform Logic Statements
• Display Results
Solution
a)
Initialize Variables
clc, clear, close all
t = 0:0.01:10; % time array, s
3
Perform First Logic Calculations
Display Results
b)
Perform Second Logic Calculations
Display Results
Vs = 3*exp(-t./3).*sin(pi.*t); % supply voltage, V
for k = 1:length(t)
if Vs(k) <= 0 % first case
Vl(k) = 0;
else % second case
Vl(k) = Vs(k);
end
end
subplot(2,1,1)
plot(t,Vl), grid minor
xlabel('Time (s)')
ylabel('Load Voltage (V)')
title('Load Voltage vs Time')
for k = length(t)
if Vs(k) <= 0.6 % first case
Vl(k) = 0;
else % second case
Vl(k) = Vs(k) - 0.6;
end
end
subplot(2,1,2)
plot(t,Vl), grid minor
xlabel('Time (s)')
ylabel('Load Voltage (V)')
title('More Accurate Plot of Load Voltage vs. Time')
4
fprintf('I would expect the second graph to tend closer to zero over a shorter interval than t
I would expect the second graph to tend closer to zero over a shorter interval than the first graph, but my graph j
Exercise Problem 4.38
Problem Statement
Write a program that determines how much longer it will take one bank account at a given interest rate to reach
a specified amount over a second bank account at a different interest rate.
Pseudocode
• Initialize Variables
• Perform While Loop Calculations
• Display Results
Solution
Initialize Variables
clc, clear, close all
k1 = 0; % counting variable
k2 = 0; % second counting variable
r1 = 4.5; % 1st annual interest rate, %
r2 = 3.5; % 2nd annual interest rate, %
balance1 = 2000; % balance, $
5
While Loop Calculations
Display Results
The first bank account takes: 89 years
The second bank account takes: 114 years
The second bank account takes 25 years longer than the first
Exercise Problem 4.46
Problem Statement
Write a program that uses the switch structure to compute the force F to begin moving an object of weight W on
the given surfaces with the given static coefficients of friction. The program should accept the value of W and
the type of material.
Pseudocode
• Initialize Variables
• Initialize User Input
• Using Switch Structure, Perform the Calculations and Display Results
Solution
Initialize Variables
fprintf('The second bank account takes: %.0f years',k2)
balance2 = 2000; % balance, $
while balance1 < 100000
balance1 = balance1*(1+(r1/100));
k1=k1+1;
end
while balance2 < 100000
balance2 = balance2*(1+(r2/100));
k2=k2+1;
end
years_diff = abs(k1-k2);
fprintf('The first bank account takes: %.0f years',k1)
fprintf('The second bank account takes %.0f years longer than the first',years_diff)
clc, clear, close all
Umm = 0.20; % Friction for metal on metal, unitless
Uww = 0.35; % Friction for wood on wood, unitless
Umw = 0.40; % Friction for metal on wood, unitless
Urc = 0.70; % Friction for rubber on concrete, unitless
W = str2double(inputdlg('Enter Weight of Object in Newtons: ')); % Asks for weight input
6
Initializing User Input
Using Switch Structure, Perform Calculations and Display Results
switch mat1
case 1 % case for metal
mat2 = menu('Choose the Second Material: ','Metal','Wood'); % Input second material
switch mat2
case 1 % case for metal
F = Umm*W; % Computes force for metal on metal
fprintf('Force required to move a metal object weighing %0.2f Newtons on metal
case 2 % case for wood
F = Umw*W; % Computes force for metal on wood
fprintf('Force required to move a metal object weighing %0.2f Newtons on wood
end
case 2 % Case for wood
mat2 = menu('Choose the Second Material:','Metal','Wood'); % Asks for second material
switch mat2
case 1 % case for metal
F = Umw*W; % computes force for metal on wood
fprintf('Force required to move a metal object weighing %0.2f Newtons on wood
case 2 % case for wood
F = Uww*W; % Computes force for wood on wood
fprintf('Force required to move a wood object weighing %0.2f Newtons on wood i
end
end
case 3 % case for rubber
mat2 = menu('Choose the Second Material: ','Concrete'); % asks for second material
F = Urc*W; % Computes the force for rubber on concrete
fprintf('Force required to move a rubber object weighing %0.2f Newtons on concrete is
Force required to move a metal object weighing 100.00 Newtons on wood is: 40.00 N
mat1 = menu('Choose the First Material: ','Metal','Wood','Rubber'); % Asks for first material
Students also viewed