0% found this document useful (0 votes)
78 views4 pages

Examples - 1

The document provides 15 examples of using MATLAB to generate and plot vectors, matrices, and functions. Example 1-9 demonstrate functions like linspace, colon notation, and plotting simple equations. Examples 10-13 show plotting data points and fitting functions using different plot styles like logarithmic scales. Examples 14-15 are short questions testing MATLAB syntax and operations.

Uploaded by

Simay Oğuzkurt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
78 views4 pages

Examples - 1

The document provides 15 examples of using MATLAB to generate and plot vectors, matrices, and functions. Example 1-9 demonstrate functions like linspace, colon notation, and plotting simple equations. Examples 10-13 show plotting data points and fitting functions using different plot styles like logarithmic scales. Examples 14-15 are short questions testing MATLAB syntax and operations.

Uploaded by

Simay Oğuzkurt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

EXAMPLES_PART_1

1. Use the linspace function to create vectors identical to the following created with colon
notation:
a) t = 4:6:35
b) x = -4:2
2. Use colon notation to create vectors identical to the following created with the linspace
function:
a) v = linspace(-2,1.5,8)
b) r = linspace(8,4.5,8)
3. The command linspace(a,b,n) generates a row vector of n equally spaced points between
a and b. Use colon notation to write an alternative one-line command to generate the same
vector. Test your formulation for a = -3, b = 5, n = 6.
4. The following matrix is entered in MATLAB:
>> A=[3 2 1;0:0.5:1;linspace(6,8,3)]
a) Write out the resulting matrix.
b) Use colon notation to write a single-line MATLAB command to multiply the second row
by the third column and assign the result to the variable C.
5. The following equation can be used to compute values of y as a function of x:

𝑦 = 𝑏𝑒 −𝑎𝑥 sin⁡(𝑏𝑥)(0.012𝑥 4 − 0.15𝑥 3 + 0.075𝑥 2 + 2.5𝑥)

where a and b are parameters. Write the equation for implementation with MATLAB,
where a = 2, b = 5, and x is a vector holding values from 0 to π/2 in increments of ∆x =
π/40. Employ the minimum number of periods (i.e., dot notation) so that your
formulation yields a vector for y. In addition, compute the vector z = y2 where each
element holds the square of each element of y. Combine x, y, z into a matrix w, where each
column holds one of the variables, and display w using short g format. Plot the variables
with legend and corresponding names and title.
6. A simple electric circuit consisting of resistor, a capacitor, and an inductor. The charge on
the capacitor q(t) as a function of time can be computed as

1 𝑅 2
𝑞(𝑡) = 𝑞0 𝑒 −𝑅𝑡/(2𝐿)
cos⁡[√ − ( ) 𝑡]
𝐿𝐶 2𝐿

where t = time, q0 = the initial charge, R = the resistance, L = inductance, and C =


capacitance. Use MATLAB to generate a plot of this function from t = 0 to 0.8, given that
q0 = 10, R = 60, L = 9, and C = 0.00005.
7. If a force F(N) is applied to compress a spring, its displacement x(m) can often be modeled
by Hooke’s law:
𝐹⁡ = ⁡𝑘𝑥
where k = the spring constant (N/m). The potential energy stored in the spring U(J) can
be computed as
1
𝑈 = 𝑘𝑥 2
2

Five springs are tested and the following data compiled:

F,N 14 18 8 9 13
x,m 0.013 0.020 0.009 0.010 0.012

Use MATLAB to store F and x as vectors and then compute vectors of the spring constants
and the potential energies. Use the max function to determine the maximum potential
energy.
8. The density of freshwater can be computed as a function of temperature with the
following cubic equation:

𝜌 = 5.5289 × 10−8 𝑇𝐶3 − 8.5016 × 10−6 𝑇𝐶2 + 6.5622 × 10−5 𝑇𝐶 + 0.99987

where ⍴ = density (g/cm3) and TC = temperature (oC). Use MATLAB to generate a vector
of temperatures ranging from 32 oF to 93.2 oF using increments of 3.6 oF. Convert this
vector to degrees Celsius and then compute a vector of densities based on cubic formula.
Create a plot of ⍴⁡versus⁡TC.⁡Recall⁡that⁡𝑇𝐶 = 5/9(𝑇𝐹 − 32).
9. Manning’s equation can be used to compute the velocity of water in the rectangular open
channel:
√𝑆 𝐵𝐻 2/3
𝑈= ( )
𝑛 𝐵 + 2𝐻

where U = velocity (m/s), S = channel slope, n = roughness coefficient, B = width (m), and
H = depth (m). The following data are available for five channels:

n S B H
0.035 0.0001 10 2
0.020 0.0002 8 1
0.015 0.0010 20 1.5
0.030 0.0007 24 3
0.022 0.0003 15 2.5

Store these values in a matrix where each row represents one of the channels and each
column represents one of the parameters. Write a single-line MATLAB statement to
compute a column vector containing the velocities based on the values in the parameter
matrix.
10. It is general practice in engineering and science that equations be plotted as lines and
discrete data as symbols. Here are some data for concentration (c) versus time (t) for the
photodegradation of aqueous bromine:
t, min 10 20 30 40 50 60
c, ppm 3.4 2.6 1.6 1.3 1.0 0.5

These data can be described by the following function:

𝑐 = 4.84𝑒 −0.03𝑡

Use MATLAB to create a plot displaying both the data (using diamond-shaped, filled-red
symbols) and the function (using green, dashed line). Plot the function for t = 0 to 70 min.

11. The semiology function operates in an identical fashion to the plot function except that a
logarithmic (base-10) scale is used for the y axis. Use this function to the plot the data and
function as described in Ex.10. Explain the results.
12. Here are some wind tunnel data for force (F) versus velocity (v):

v, v/s 10 20 30 40 50 60 70 80
F, N 25 70 380 550 610 1220 830 1450

These data can be described by the following function:

𝐹 = 0.2741𝑣 1.9842

Use MATLAB to create a plot displaying both the data (using circular magenta symbols)
and the function (using a black dash-dotted line). Plot the function for v = 0 to 100 m/s
and label the plot’s axes.
13. The loglog function operates in an identical fashion to the plot function except that
logarithmic scales are used for both the x and y axes. Use this function to plot the data and
function as described in Ex.12. Explain the results.
14. The Maclaurin series expansion for the cosine is

𝑥2 𝑥4 𝑥6 𝑥8
cos 𝑥 = 1 − + − + −⋯
2! 4! 6! 8!

Use MATLAB to create a plot of the sine (solid line) along with a plot of the series
expansion (black dashed line) up to and including the term x8/8!. Use the built-in function
factorial in computing the series expansion. Make the range of the abscissa from x = 0 to
3π/2.
15. Exactly what will be displayed after the following MATLAB commands are typed?
a) >> x = 5;
>> x^3;
>> y = 8 – x

b) >> q = 4:2:12;
>> r = [7 8 4;3 6 -5];

>> sum(q)*r(2,3)

You might also like