\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Oct 24, 2024 | 485 words | 5 min read

14.2.2. Task 2#

Learning Objectives#

Read code with user defined functions. Create user defined functions in MATLAB.

Task Instructions#

Part A: Tracking User Defined Functions through MATLAB Code#

The following exercise is to be worked on as a team in-class. Work with the problems by hand first and then type the solutions into MATLAB to check them. Be prepared to show a member of the instructional team after you find the correct solutions. When you are finished, write your answer in ma3_team_teamnumber.pdf and save the file.

Consider the user-defined functions, fn1 and fn2, as shown below.

function [x,y]=fn1(x,y)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Help description
%%%%%%%%%%%%%%%%%%%%%%%%%%%
x = x - 4;
[y,x] = fn2(x,y);
y = x^2 - 10;
end
function [out1,out2]=fn2(in1,in2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Help description
%%%%%%%%%%%%%%%%%%%%%%%%%%%
out1 = in1 + in2;
out2 = in2 - in1;
end

The following codes are executed in the MATLAB Command window. What values do the variables x and y have?

>> x = 3;
>> y = 5;
>> [x,y] = fn1(x,y)

Part B: Creating User Defined Functions in MATLAB#

In the following exercise, you will create three user-defined functions that will be used in Task #3 of this assignment. The background of these functions is explained in Task #3, but is not needed to complete this task. These functions will be created in three different MATLAB files using the MATLAB UDF file template, ENGR133_MATLAB_UDF_Template.m.

The geometric parameters of a blind are:

  • \(W\) = slat width (mm)

  • \(S\) = slat spacing (mm)

  • \(M\) = width of slat that is illuminated (mm)

  • \(\sigma\) = vertical shadow angle (given in degrees)

  • \(\Psi\) = slat angle with horizontal (given in degrees)

  • Additionally, the blind surface material has an absorptivity constant, \(a\) (alpha). This is a dimensionless value between 0-1.

User-Defined Function 1:#

Create a UDF named ma3_team_2_fractions_teamnumber.m that calculates and returns the three fraction values F1, F2, and F3 in a vector as [F1, F2, F3]. The inputs to this function are S, W, and \(\Psi\).

\[F_1=\frac{1}{2}\left( 1 + \frac{S}{W} - \sqrt[]{1 + \left(\frac{S}{W}\right)^{2} + 2\frac{S}{W}sin(\Psi)}\right)\]
\[F_2 = \frac{1}{2}\left( \sqrt{1 + \left( \frac{S}{W} \right)^2 + 2\frac{S}{W}sin(\Psi)} +\sqrt{1 + \left( \frac{S}{W} \right)^2 - 2\frac{S}{W}sin(\Psi)} -2\frac{S}{W}\right)\]
\[F_3 = \frac{1}{2}\left( 1 + \frac{S}{W} -\sqrt{1 + \left( \frac{S}{W} \right)^2 - 2\frac{S}{W}sin(\Psi)} \right)\]

Sample Output#

Use the values in Table 14.3 below to test your program.

Table 14.3 Test Cases#

Case

Slat Spacing (mm)

Slat Width (mm)

Slat angle with horizontal (degrees)

1

40

50

30

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

>> ma3_team_2_a_teamnumber Input Slat Spacing: 40 Input Slat Width: 50 Input Slat angle with horizontal: 30

The output of the function: ans =

0.1190 0.4393 0.4417

User-Defined Function 2:#

Create a UDF named ma3_team_2_transmission_teamnumber.m that calculates and returns \(T_D\). The inputs to this function are a vector containing [F1, F2, F3], S, W, \(\Psi\), \(a\), and \(\sigma\).

\[T_D = 1 - \frac{W}{S}\frac{sin(\sigma+\Psi)}{cos(\sigma)}\left( 1 - F_1(1-a) - \frac{F_2(1-a)^2[F_3+F_1\cdot F_2(1-a)]}{1-F_2^2(1-a)^2)} \right)\]

Sample Output#

Use the values in Table 14.4 below to test your program.

Table 14.4 Test Cases#

Case

F1

F2

F3

Slat Spacing (mm)

Slat Width (mm)

Slat angle with horizontal (degrees)

alpha

Vertical Shadow Angle (degrees)

1

0.119

0.4393

0.4417

40

50

30

0.5

45

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

>> ma3_team_2_b_teamnumber Input F1: 0.119 Input F2: 0.4393 Input F3: 0.4417 Input Slat Spacing: 40 Input Slat Width: 50 Input Slat angle with horizontal: 30 Input alpha: 0.5 Input vertical shadow angle: 45

The output of the function: ans =

-0.5138

User-Defined Function 3:#

Create a UDF named ma3_team_2_absorb_teamnumber.m that calculates and returns \(A_D\). The inputs to this function are F2, S, W, \(\Psi\), \(a\), and \(\sigma\).

\[A_D =\frac{aWsin(\sigma+\Psi)}{Scos(\sigma)[1-F_2(1-a)]}\]

Sample Output#

Use the values in Table 14.5 below to test your program.

Table 14.5 Test Cases#

Case

F2

Slat Spacing (mm)

Slat Width (mm)

Slat angle with horizontal (degrees)

alpha

Vertical Shadow Angle (degrees)

1

0.4393

40

50

30

0.5

45

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

>> ma3_team_2_c_teamnumber Input F2: 0.4393 Input Slat Spacing: 40 Input Slat Width: 50 Input Slat angle with horizontal: 30 Input alpha: 0.5 Input vertical shadow angle: 45

The output of the function: ans =

1.0941