Chapter 5: Problem 1
What is the difference between a script file and a function?
Short Answer
Step by step solution
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
Chapter 5: Problem 1
What is the difference between a script file and a function?
These are the key concepts you need to understand to accurately answer the question.
All the tools & learning materials you need for study success - in one app.
Get started for freeWrite three MATLAB functions to caiculate the hyperbolic sine, cosine, and tangent functions. $$ \sinh (x)=\frac{e^{x}-e^{-x}}{2}, \cosh (x)=\frac{e^{x}+e^{-x}}{2}, \tanh (x)=\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}} $$ Use your functions to plot the shapes of the hyperbolic sine, cosine, and tangent functions.
Use the Help Browser to look up information about the standard MATLAB function sortrows and compare the performance of sortrows with the sort-with-carry function created in the previous exercise. To do this, create two copies of a \(1000 \times 2\) element array containing random values, and sort column 1 of each array while carrying along column 2 using both functions. Determine the execution times of each sort function using tic and toe. How does the speed of your function compare with the speed of the standard function sorerows?
Read Traffic Density. Function random0 produces a number with a uniform probability distribution in the range \([0.0,1.0)\). This function is suitable for simulating random events if each outcome has an equal probability of occurring. However, in many events the probability of occurrence is not equal for every event, and a uniform probability distribution is not suitable for simulating such events. For example, when traffic engineers studied the number of cars passing a given location in a time interval of length \(t\), they discovered that the probability of \(k\) cars passing during the interval is given by the equation $$ P(k, t)=e^{-\lambda t} \frac{(\lambda t)^{4}}{k !} \text { for } t \geq 0, \lambda>0, \text { and } k=0,1,2, \ldots $$ This probability distribution is known as the Poisson distribution: it occurs in many applications in science and engineering. For example, the number of calls \(k\) to a telephone switchboard in time interval \(t\), the number of bacteria \(k\) in a specified volume \(t\) of liquid, and the number of failures \(k\) of a complicated system in time interval \(t\) all have Poisson distributions. Write a function to evaluate the Poisson distribution for any \(k, t\), and A. Test your function by calculating the probability of \(0,1,2, \ldots, 5\) cars passing a particular point on a highway in I minute, given that \(\lambda\) is \(1.6\) per minute for that highway. Plot the Poisson distribution for \(t=1\) and \(\lambda=1.6\).
Dice Simulation. It is often useful to be able to simulate the throw of a fair die. Write a MATLAB function dice that simulates the throw of a fair die by returning some random integer between 1 and 6 every time that it is called. (Hint: Call randomo to generate a randem number. Divide the possible values out of random0 into six equal intervals, and return the number of the interval that a given random value falls into.)
Gravitational Force. The gravitational force \(F\) between two bodies of masses \(m_{1}\) and \(m_{2}\) is given by the equation $$ F=\frac{G m_{1} m_{2}}{r^{2}} $$ where \(G\) is the gravitational constant \(\left(6.672 \times 10^{-11} \mathrm{~N} \mathrm{~m}^{2} / \mathrm{kg}^{2}\right), m_{1}\) and \(m_{2}\) are the masses of the bodies in kilograms, and \(r\) is the distance between the two bodies. Write a function to calculate the gravitational force between two bodies given their masses and the distance between them. Test your function by determining the force on an \(800-\mathrm{kg}\) satellite in orbit \(38,000 \mathrm{~km}\) above the Earth. (The mass of the Earth is \(5.98 \times 10^{24} \mathrm{~kg}\).)
What do you think about this solution?
We value your feedback to improve our textbook solutions.