1 / 4100%
ENGR 133, Lab-04
Authored by: Prof. Jim Long
Created on: 7/19/2020
Exercise #1 ... Problem 3.7 with Function File
Problem presentation
We are asked to write a function that accepts a temperature expressed in degrees Fahrenheit (F) and computes
the corresponding value in degrees Celsius (C). We are further instructed to test our functions.
Function file definition:
function y = tempF2C(x)
y = (5/9)*(x-32);
end
Initialize Variables
Perform Calculations
Display Results
The test temperatures are (degrees F):
-460 32 212
The computed temperatures are (degrees C):
-273.3333 0 100.0000
Exercise #2 ... Problem 3.7 with Anonymous Function
Problem presentation
disp(test_results_C)
disp(test_temps_F)
clc,clear,close all
test_temps_F = [-460 32 212]; % test temperatures defined at key known points
test_results_C = tempF2C(test_temps_F); % calls function with test points
fprintf('The test temperatures are (degrees F):\n\n')
fprintf('\n\nThe computed temperatures are (degrees C):\n\n')
clc,clear,close all
We are asked to write a function that accepts a temperature expressed in degrees Fahrenheit (oF) and
computes the corresponding value in degrees Celsius (oC). We are further instructed to test our functions.
Function file definition:
Initialize Variables
Perform Calculations
Display results
Table = table(test_temps_F', test_results_C','VariableNames',{'Farenheit','Celcius'}); % create
disp('Table of Temperature Conversion'),disp(' '),disp(Table) % display table title
Table of Temperature Conversion
Farenheit Celcius
-460
-273.33
32
0
212
100
Exercise #3 ... Problem 3.14
Problem presentation
We are asked to use the fminbnd function to find the values of R and L of the figure provided that will minimize
the total cost of enclosing thearea with fence.
Function file definition:
function Ct = fence(R)
global A % area to be enclosed (square feet)
Cs = 40; % cost of straight fence per foot
Cc = 50; % cost of curved fence per foot
L = (A-0.5*pi*R.^2)./(2*R); % length as a function of A & R
Ct = Cs*(2*R+2*L)+Cc*pi*R; % cost calculation
end
Initialize variables
tempF2C = @(x) (5/9)*(x-32); % converts from Fahrenheit to Celsius
test_temps_F = [-460 32 212]; % test temperatures defined at key known points
test_results_C = tempF2C(test_temps_F); % calls function with test points
clc,clear,close all
Perform calculations
Display results
The optimum value of R is: 21.43 feet.
The corresponding value of L is: 29.84 feet.
The resulting minimum cost is: $7467.21.
Exercise #4 ... Problem 4.9
Problem presentation
We are given the history of daily prices for two stocks and are asked to determine how many days the price of
stock A was above that of stock B. Our pseudocode for solving this problem is:
-- Compare each price daily to see if stock A is higher than stock B
-- Create a list of the days where this condition is true
-- Count how many days are in our list
-- Display the result
Initialize variables
Perform calculations
Display results
price_A = [19,18,22,21,25,19,17,21,27,29]; % data for stock A
price_B = [22,17,20,19,24,18,16,25,28,27]; % data for stock B
fprintf('The corresponding value of L is: %5.2f feet.\n\n',optimum_L)
global A
A = 2000; % makes variable A available in both the function & main script
optimum_R = fminbnd(@fence,0,100); % finds R between 0-100 that minimizes Ct
min_cost = fence(optimum_R); % returns minimum cost at optimum R
optimum_L = (A-0.5*pi*optimum_R.^2)./(2*optimum_R); % corresponding L to R
fprintf('The optimum value of R is: %5.2f feet.\n\n',optimum_R)
fprintf('The resulting minimum cost is: $%5.2f.\n\n',min_cost)
clc,clear,close all
total_days = length(price_A); % total days of data
comp_days = find(price_A>price_B); % days stock A greater than stock B
num_days = length(comp_days); % count of days A>B
fprintf('Stock A price was compared to stock B for %2.0f days.\n\n',total_days)
Stock A price was compared to stock B for 10 days.
Stock A price exceeded stock B price on 7 days.
fprintf('Stock A price exceeded stock B price on %2.0f days.\n\n',num_days)
Students also viewed