You are on page 1of 1

Matlab

HW 3, Due Friday July 10



1) Write a function to compute the sum of the elements in an array. You need to
write the function using a for loop. Do not use the built-in sum function. Test
your function with the following arrays
a. V = [1 2 3 4 -1 -2 -3];
b. V = [0.2 1 5 -3];



2) Write a function that takes as an input an array Vin and outputs a new array
Vout with the same elements as Vin but in reverse order. For example:
if Vin = [1 2 3], then Vout = [3 2 1].

Test your function with the following two arrays
a. Vin = [5 3 7 8 1 -3];
b. Vin = [1 4 7 3 2 -9 1 2 6 82 1];


3) Write a function that takes as input an array Vin and returns a new array
Vout with the following characteristics. The elements of Vin that are greater
than zero are replaced by ones. The elements that are less than zero are
replaced by -1, and the elements that are equal to zero are left unchanged.
You are requested to use a while loop to iterate over the input array.
Example: If Vin = [4 2 1 -3 5 -2 0 1 3], then Vout = [1 1 1 -1 1 -1 0 1 1].

Test your function with the following two arrays.
a. Vin = [-2 0 -6 0 4 5 8];
b. Vin = [0 0 0 0 12 -8 7 6 -2 3 1];


4) The file dataHW3P4v2.txt contains experimental data. The first column has x
values and the second column y values. You can load the data using the
following Matlab command
data = load('dataHW3P4v2.txt'), data will be a matrix with the experimental
data. Note: you need to place the txt file in your folder for this command to
work.
a. Plot y vs. x using circles (add labels)
b. Using polyfit, fit a model of the following form to your data set = !
You need to overlay the predictions generated by your model on top of
the experimental data. Use a solid line for your model predictions.
c. Add a text showing the equation of your model by the side of the
generated curve.

You might also like