MATLAB Function Computes Temperature Values
Write a function that accepts temperature in degrees Fahrenheit and computes the corresponding value in degree Celsius. The relation between the two is
C=5(F-32)/9 C-Celsius F-Fahrenheit
Solution
function C = FarenheitToCelsius(F)
C = 5*(F-32)/9;
end
MATLAB Function that Computes Time and Height of a Given Value
An object is thrown vertically with a speed v reaches a height h at time t, where
h = vt – (gt2)/2
Write a MATLAB program using function that computes the time t required to reach a specified height h, for a given value of v. The inputs should be h, v , and g. Test your function for the case where h=100 meters, v =50 m/s and g=9.81 m/s2. Also use the Polynomial Roots to find the time.
MATLAB Script Solution
function t = HeightTime(h, v, g)
% To solve this equation, we can use fsolve
% We have that:
% h = vt - gt^2 /2
% Then
% (g/2)t^2 -vt + h = 0
% a = g/2
% b = -v
% c = h
a = g/2;
b = -v;
c = h;
% The roots are found using the following equation
% t = (-b +- sqrt(b^2 -4ac))/(2a)
t1 = (-b - sqrt(b^2 - 4*a*c))/(2*a);
t2 = (-b + sqrt(b^2 - 4*a*c))/(2*a);
% Because the motion is parabolic, there will be two instant of times
% when the projectile reaches that height. One is when it is going up,
% and the second is when going down.
t = [t1, t2];
end
Testing the Functions
clc, clear all, close all
%% HW3.1)
% Test our function to convert Farenheit to Celsius
% 150 F is equal to 65.56 Cº.
F = 150;
C = FarenheitToCelsius(F);
fprintf("%.2f Farenheit are equal to %.4f Celsius\n\n", F, C);
%% HW3.2)
h = 100;
v = 50;
g = 9.81;
% Test our function
t = HeightTime(h, v, g);
fprintf("The times for a projectile to reach an altitude of %.2f given v0 = %.2f and g = %.2f are %.2f s and %.2f s\n\n", h, v, g, t(1), t(2));
% Now, calculate the solution using MATLAB's root
% (g/2)t^2 -vt + h = 0
fprintf("Using MATLAB's roots function, the solutions are:\n");
solutions = roots([g/2, -v, h])