1 / 6100%
Logical Functions&User-defined Functions Problem Set
Becca Arevalo 10/16/2023
Problem 3.10
clc, clear, close all
Test a function that computes the time t required ot reach a specified height h, for a given value of V0. Test your
function for the case where
h=100m
V0=50 m/s
g=9.81 m/s^2An object thrown vertically with a speed V0 reaches a height h at time t where h=V0t-(1-2)gt^2
Problem solution
function t = reach_time(h, v0, g)
t = (v0 +- sqrt(v0^2 - 2*g*h))/g;
end
Initialize Variables
h = 100;
v0 = 50;
g = 9.81;
calculate reach time
t = reach_time(h, v0, g)
t = 2.7324
Problem 3.17
clc, clear, close all
The volume V and paper surface area A of a conical paper cup is given by the equations
V=(1/3)pir^2h
A=pirsqrt(r^2+h^2)
r is the radius and h is the height
-make an expression for A as a function of r and V
-create a user defined function that accepts R as the arguement and computes for A for a given value of V.
Declare V to be global in the function.
-For V=10in^3 use the function fminbnd to compute the value of r that minimizes the area A.
1
What is the corresponding value of h?
Plot V vs r.
How much can R vary about its optimal value before the area increases 10 percent above its minimum value?
Problem solution
function result = computeA(R)
global V;
result =pi*R.*sqrt(R.^2+(9*V^2)./(pi^2*R.^4));
end
Initialize variables
global V;
V = 10;
optimalR = fminbnd(@computeA, 0, 10);
optimalA = computeA(optimalR);
optimalH = (3 * V) / (pi * optimalR^2);
calculate minimum
global V;
V = 10; % Set the global value of V
min_R=min(optimalR);
max_R=max(optimalR)*1.10;
plot
r_values = linspace(0.1, 2*optimalR, 500); % creating an array of r values for
plotting
A_values = arrayfun(@computeA, r_values);
plot(r_values, A_values, 'b', 'LineWidth', 2)
xlabel('r (Radius)');
ylabel('A (Area)');
title('Sensitivity of Area with respect to r');
grid on;
2
Display results
fprintf('Optimal r: %f\n', optimalR);
Optimal r: 1.890105
fprintf('Corresponding h: %f\n', optimalH);
Corresponding h: 2.673003
fprintf('r can vary between %f and %f before A increases by 10%%.\n', min_R, max_R)
r can vary between 1.890105 and 2.079116 before A increases by 10%.
Problem 3.20
clc, clear, close all
Determine the balance in a savings account at the end of every year for the first n years, where n is an input.
Display the information on screen in a table in which the first column is year and the second is balance.
Variables
n=10
A=10000
r=3.5
3
After 10 years the balance is 14105.99
With an initial investment of A and interest rate r, the balance B after n years is given by
B=A(1+r/100)^n
Initialize variables
r=3.5;
balance= @(n) 10000*(1+(3.5/100)).^n;
create table
year=[1 2 3 4 5 6 7 8 9 10 11 12];
balance_results=balance(year);
Table=table(year',balance_results','VariableNames',{'year','balance'}); %creates
table
disp('Table of Temperature Conversion)'), disp(Table)
Table of Temperature Conversion)
year balance
____ _______
1 10350
2 10712
3 11087
4 11475
5 11877
6 12293
7 12723
8 13168
9 13629
10 14106
11 14600
12 15111
balance(10);
Display results
fprintf(['The after 10 years the balance in the savings account is $%4.0f'],
balance(10))
The after 10 years the balance in the savings account is $14106
Problem 4.10
clc, clear, close all
A. determine how many days the price of stock A was above both the price of stock B and the price of stock C.
B. determine how many days the price of stock A was above stock B or the price of stock C.
4
C.determine how many days the price of stock A was above either the price of stock B or the price of stock C,
but not both.
Matrices:
price_A=[19 18 22 21 25 19 17 21 27 29]
price_B=[22 17 20 19 24 18 16 25 28 27]
price_C=[17 13 22 23 19 17 20 21 24 28]
Intitialize variables
price_A=[19 18 22 21 25 19 17 21 27 29];
price_B=[22 17 20 19 24 18 16 25 28 27];
price_C=[17 13 22 23 19 17 20 21 24 28];
calculate
result_A=(price_A>price_B)&(price_A>price_C);
A=nnz(result_A);
result_B=(price_A>price_B)|(price_A>price_C);
B=nnz(result_B);
result_C=(price_A>price_B)&~(price_A>price_C);
C=nnz(result_C);
Display results
fprintf('There are %1.0f days the price of stock A was above both the price of
stock B and the price of stock C ', A)
There are 4 days the price of stock A was above both the price of stock B and the price of stock C
fprintf('There are %1.0f days that the price of stock A was above stock B or the
price of stock C', B)
There are 9 days that the price of stock A was above stock B or the price of stock C
fprintf('There are %1.0f days the price of stock A was above either the price of
stock B or the price of stock C, but not both',C)
There are 3 days the price of stock A was above either the price of stock B or the price of stock C, but not both
Problem 4.12
clc, clear, close all
The height and speed of a projectile launched with a speed of V0 at an angle A to the horizontal are given by
the equations
h(t)=V0tsin(A)-0.5gt^2
v(t)=sqrt(V0^2-2Vogtsin(A)+g^2t^2)
5
where g is accelerarion due to gravity.
t_hit=2(V0/g)sindA
Variables
A=40
V0=35m/s
g=9.81m/s^2
Find the times when
-the height is no less that 18m
-The hegiht is no less than 10m and the speed is simultaneously no greater than 30m/s
Initialize variables
A = 40; % Angle in degrees
v0 = 35; % Initial speed in m/s
g = 9.81; % Acceleration due to gravity in m/s^2
target_height_a = 18; % Minimum height for part a
max_speed_b = 30; % Maximum speed for part b
calculate
t_hit = 2*(v0 / g)*sind(A);
t_values = linspace(0, t_hit, 1000);
height_values = v0*t_values*sind(A)-0.5* g*t_values.^2;
speed_values =sqrt(v0^2-2*v0*g*t_values*sind(A)+g^2*t_values.^2);
times_a = t_values(height_values >= target_height_a);
times_b = t_values(height_values >= target_height_a & speed_values <= max_speed_b);
Display results
fprintf('Part a: Times when height is no less than 18 m: %.5f sec to %.5f sec\n',
min(times_a), max(times_a));
Part a: Times when height is no less than 18 m: 1.03303 sec to 3.55363 sec
fprintf('Part b: Times when height is no less than 10 m and speed is no greater
than 30 m/s: %.5f sec to %.5f sec\n', min(times_b), max(times_b));
Part b: Times when height is no less than 10 m and speed is no greater than 30 m/s: 1.03303 sec to 3.55363 sec
6
Students also viewed