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\).
Sample Output#
Use the values in Table 14.3 below to test your program.
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\).
Sample Output#
Use the values in Table 14.4 below to test your program.
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\).
Sample Output#
Use the values in Table 14.5 below to test your program.
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