$35
Important Notes:
• This assignment is about writing user defined functions for applications. The questions are based on content from chapters 8 and 9 of the textbook “Matlab, An introduction with applications”.
• Complete this assignment by filling in the template file, assign6.m, with your Matlab function files.
• The function files will be tested by the auto-tester using randomly generated data.
• Do NOT make any plots in your code.
Make sure your final submission runs without syntax error. As usual, template files that do not run without syntax error will be rejected by the auto-marker, and you will have to visit Chris in person to demo your code and get the marks.
Q1. Write a function that finds the magnitude of the roots of an input polynomial and outputs the magnitude of the roots that fall within a user specified range of values LOW and HIGH. The input polynomial 𝑝 is entered as a row vector of coefficients as shown in the example on pp.2623 of the textbook.
The polynomial 𝑦 = −0.001𝑥) + 0.051𝑥, − 0.76𝑥/ + 3.8𝑥 − 1.4 is entered as 𝑝 = [−0.001, 0.051, −0.76, 3.8, −1.4]. The magnitude of the roots are: 29.6177, 10.8776, 10.8776, 0.3995. Now suppose the range is given as say LOW=9 and HIGH=11, then the second and third roots [10.8776, 10.8776] will be output.
Q2. Write a function that accepts the vector of data points 𝑇 and ℎ as input arguments and determines a linear equation in the form of 𝑇 = 𝑚ℎ + 𝑏 that best fits the data. Then output the magnitude of the error between the data points and the linear regression curve, denoted by the vector of 𝑒. Use the Matlab polyfit() and polyval() functions. The input arguments are the boiling temperature of water 𝑇 at various altitudes ℎ as in
𝒉 (meters)
0
608
1520
2280
3040
6384
7904
𝑻 (Celsius)
100.00
98.88
95.00
92.22
90.00
81.11
75.55
Q3. Write a function that accepts the vector of data points 𝑁 and 𝑡 as input arguments and determines the best exponential equation in the form of 𝑁 = 𝑏𝑒BC that best fits the data, using the Matlab polyfit() function. Then output 1x2 vector 𝑏 𝑚 . The input arguments are the number of bacteria 𝑁 measured at different times 𝑡 as
𝒕 (min)
10
20
30
40
50
𝑵
38,000
60,000
250,000
500,000
1000,000
Q4. Write a function named that accepts the integration limits of 𝐴 and 𝐵 as input arguments and outputs the integral of h x( ) =sin( )expx (-x x2) 3. Use the Matlab quad() function for this. That is the function does the following
B
Z =òsin( )expx (-x x dx2) 3
A
Q5. The following wind tunnel data shows the aerodynamic drag force on a car, 𝐹I, as a function of the car velocity, 𝑣.
v km hr( / )
20
40
60
80
100
120
140
160
F ND ( )
10
50
109
180
300
420
565
771
Write a function that accepts an arbitrary value for vand output the corresponding interpolated value of FD. Use Matlab function interp1() with the ‘spline’ method for the interpolation.
Q6. An RLC circuit with an alternating voltage source is assumed. The source voltage 𝑣K is given by 𝑣K = 𝑣Bsin (𝜔Q𝑡), where 𝜔Q = 2𝜋𝑓Q in which 𝑓Q is the driving frequency. The normalized amplitude of the current, 𝐼, in this circuit is given by
1
𝐼 =
where 𝑅 (Ω), 𝐿 (H), and 𝐶 (F) are the resistance of the resistor, the capacitance of the capacitor, and the inductance of the inductor, respectively. Write a function named that accepts 𝑅, 𝐿, and 𝐶 as input arguments and finds the natural frequency of the circuit (the frequency at which 𝐼 is maximum). The natural frequency of 𝑓Z (Hz) is the output argument. Use Matlab’s fminbnd() for this application and search over 0.5/ 𝐿𝐶 < 𝜔Q < 1.5/ 𝐿𝐶. Note that minimizing −𝐼 is equivalent to maximizing 𝐼.
Q7. The sudden outbreak of an insect population can be modelled by the equation
𝑑 𝑁 𝑁 𝑟 𝑁/
= 𝑅𝑁 1 − − / + 𝑁/ 𝑑 𝑡 𝐶 𝑁_
where 𝑁 is the number of insects, 𝑅 is an intrinsic growth rate, and 𝐶 is the carrying capacity of the local environment. The first term is a population growth model and the second term represents the effects of bird predation, which becomes significant when the population reaches a critical size 𝑁_ and has a maximum value of 𝑟. Solve for 𝑁at time 𝑡 = 50 days for 𝑅 = 0.55 per day, 𝑁 0 = 1000, 𝐶 = 10), 𝑁_ = 10), and 𝑟 = 10) per day. Use ode45(). Note: you must make your anonymous function a function of both 𝑡 and 𝑁 even though 𝑡 doesn’t appear explicitly in the differential equation.