0% found this document useful (0 votes)
115 views

Computational Biomechanics and Rehabilitation Lab # 2

The document provides instructions for using MATLAB's fprintf function to print variables with specified field widths and decimal places. It also provides examples of creating matrices and vectors, performing calculations on user inputs, plotting data, and loading/saving data to and from files. The focus is on demonstrating different ways to input, output, and manipulate numerical data in MATLAB.

Uploaded by

yechale tafere
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

Computational Biomechanics and Rehabilitation Lab # 2

The document provides instructions for using MATLAB's fprintf function to print variables with specified field widths and decimal places. It also provides examples of creating matrices and vectors, performing calculations on user inputs, plotting data, and loading/saving data to and from files. The focus is on demonstrating different ways to input, output, and manipulate numerical data in MATLAB.

Uploaded by

yechale tafere
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Computational Biomechanics and Rehabilitation

Lab # 2

1. Experiment in the Command Window, with using the fprintf function for real
numbers. Make a note of what happens for each. Use fprintf to print the real
number 12345.6789:

a. Without specifying any field width

b. in a field width of 10 with four decimal places

c. in a fields width of 10 with two decimal places

d. in a field width of 6 with four decimal places

e. in a field width of 2 with four decimal places

2. Experiment in the Command Window, with using the fprintf function for
integers. Make a note of what happens for each. Use fprintf to print the integer
12345:

a. without specifying any field width

b. in a field width of 5

c. in a field width of 8

d. in a field width of 3

3. Create the following variables:

x = 12.34;

y = 4.56;

Then fill in the fprintf statements using these variables that will accomplish the
following:

>>fprintf(

x is 12.340

>>fprintf(

x is 12

>>fprintf(

y is 4.6
>>fprintf(

y is 4.6 !

4. Create a 3 x 6 matrix of random integers, each in the range from 50 to 100.


Write this to a file called randfile.dat. Then create a new matrix of random
integers, but this time make it a 2 x 6 matrix of random integers, each in the
range from 50 to 100. Append this matrix to the original file. Then read the file
in (which will be to a variable called randfile) just to make sure it worked!

5. Write an input statement that will prompt the user for a real number, and store
it in a variable. Then use the fprintf function to print the value of this variable
using two decimal places.

6. The Body Mass Index (BMI) for a person is defined, using US units, as

BMI = 703*weight/height^2

Where the weight is in pounds and the height is in total inches. Write a function
called findbmi that would receive the weight and height as input arguments, and
would return the BMI. For example, to find the BMI for a person who weight 170
pounds and is 5’11” tall (71 inches), here are two examples of function calls:

>> findbmi(170,71)

ans =

23.7076

>>bmi = findbmi(170,71)

bmi =

23.7076

7. Create a script, temp_FC.m, that would prompt the user for a temperature, and
then ‘F’ or ‘C’, and store both inputs in variables. For example, when executed it
would look like this (assuming the user enters 85 and then F):
Enter the temperature: 85
Is that F or C?: F

8. For a project, some students are designing a device that will monitor a person’s
heart rate while on a treadmill. The device will let the subject know when the
target heart rate has been reached. A simple calculation of the target heart rate
(THR) for a moderately active person is

THR = (220-A)*0.6
where A is the person’s age. Write a function that will calculate and return the
THR.

9. If the length of two sides of a triangle and the angle between them are known,
the length of the third side can be calculated. Given the lengths of two sides (b
and c) or a triangle, and the angle between them (theta) in degrees, the third
side, a, can be calculated as:

A^2=b^2+c ^2-2bccos(theta)

Write a script thirdside that will prompt the user and read in values for b,c, and
theta (in degrees), and then calculate and print the value of a with three decimal
places. (Note: to convert an angle from degrees to radians, multiply the angle by
pi/180.)

The format of the output from the script should look like this:

>>thirdside

Enter the first side: 2.2

Enter the second side: 4.4

Enter the angle between them (in degrees): 50

The third side is 3.429

10. Prompt the user for the number of rows and columns of a matrix, create a matrix
with that many rows and columns of random numbers, and write it to a file.

11. a). Write a script file that calculates a damped sinusoidal waveform from ome
time data (you create). Use this as position data and calculate velocity and
acceleration using the diff function

x= A e−ct sin (2 πft )

Where
x = displacement
A= Amplitude
e = exponential (uses MATLAB function exp)
c = damping factor
t = time
f = frequency
b). Once calculated, plot the data in a 3x1 subplot so that position is on top, velocity
is in the middle, and acceleration is at the bottom. Be sure to include X and Y
labels on your plot.

c). Write a script file to save your data array in the form of

Output_Array = [ time position velocity acceleration];

12. The sales (in billions) for two separate divisions of the XYZ Corporation for each
of the four quarters of 2007 are stored in a file called salefigs.dat:

1.2 1.4 1.8 1.3

2.2 2.5 1.7 2.9

a) Create this file (just type the numbers in the editor, and Save as salesfigs.dat).

b) Load the data from the file into a matrix.

c) Write a script that will

i) separate this matrix into two vectors

ii) Plot Sales(billions) vs Quarter for each of the two divisions on the same
graph. Use o’s for Division 1 and +’s for Division 2. Label the x axis: Quarter,
the y axis: Sales (billions) and title: XYZ Corporation Sales: 2007. Include a
legend. Save the figure.

You might also like