Problem 1 (Write a Function)
The distance a freely falling object falls is given by:
where g is acceleration due to gravity (9.81 m/s^2), t is the time in seconds, and x is the distance travelled in meters. The derivative of the above equation gives us the velocity of the object:
Complete a function called freefall
which takes in a vector of times and returns the distance and the velocity as outputs.
You can test your function with the following values:
>> [d v] = freefall(1:10)
d =
Columns 1 through 6
4.9050 19.6200 44.1450 78.4800 122.6250 176.5800
Columns 7 through 10
240.3450 313.9200 397.3050 490.5000
v =
Columns 1 through 6
9.8100 19.6200 29.4300 39.2400 49.0500 58.8600
Columns 7 through 10
68.6700 78.4800 88.2900 98.1000
>> [d v] = freefall(100:25:500)
d =
1.0e+06 *
Columns 1 through 6
0.0491 0.0766 0.1104 0.1502 0.1962 0.2483
Columns 7 through 12
0.3066 0.3709 0.4415 0.5181 0.6009 0.6898
Columns 13 through 17
0.7848 0.8860 0.9933 1.1067 1.2263
v =
1.0e+03 *
Columns 1 through 6
0.9810 1.2263 1.4715 1.7167 1.9620 2.2073
Columns 7 through 12
2.4525 2.6978 2.9430 3.1883 3.4335 3.6787
Columns 13 through 17
3.9240 4.1692 4.4145 4.6597 4.9050
Problem 2 (Plotting)
Now, write the function freefallPlot
so that it generates a plot of the results as well as returning the vectors d and v. Put both the distance and velocity in the same figure and add a legend. Reuse the function freefall
you wrote for the previous problem by calling it from within your new function freefallPlot
.
NOTE: Adding files and folders to the MTALB path
MATLAB will only find your previously implemented functions, if they are in your current folder or their location is added to the MATLAB path. To achieve this, put the following command at the beginning of your script (or execute it in the command window): addpath(genpath('path/to/your/local/repsotiory/folder'))
If you get an error message while calling a function (saying that the function does not exist), then MATLAB does not find it, because the path was not added correctly to the MATLAB path. Check your file locations and spelling!
You can test your new function with the following values:
>> [d v] = freefallPlot(1:10)
d =
Columns 1 through 6
4.9050 19.6200 44.1450 78.4800 122.6250 176.5800
Columns 7 through 10
240.3450 313.9200 397.3050 490.5000
v =
Columns 1 through 6
9.8100 19.6200 29.4300 39.2400 49.0500 58.8600
Columns 7 through 10
68.6700 78.4800 88.2900 98.1000
>> [d v] = freefallPlot(100:25:500)
d =
1.0e+06 *
Columns 1 through 6
0.0491 0.0766 0.1104 0.1502 0.1962 0.2483
Columns 7 through 12
0.3066 0.3709 0.4415 0.5181 0.6009 0.6898
Columns 13 through 17
0.7848 0.8860 0.9933 1.1067 1.2263
v =
1.0e+03 *
Columns 1 through 6
0.9810 1.2263 1.4715 1.7167 1.9620 2.2073
Columns 7 through 12
2.4525 2.6978 2.9430 3.1883 3.4335 3.6787
Columns 13 through 17
3.9240 4.1692 4.4145 4.6597 4.9050
Problem 3 (Fibonacci Sequence)
The fibonacci sequence is defined as: 1, 1, 2, 3, 5, 8, …
Write a function called fibonacci1
that takes in an integer n and returns the first n numbers of the sequence.
Write a function called fibonacci2
that takes in an integer n and returns the numbers of the sequence smaller than n.