Chapter 5

Chapter 5 Code Examples

MATLAB Code Examples

x=[1 2 3 5 7 7.5 8 1];
y=[2 6.5 7 7 5.5 4 6 8];
plot(x,y)
year=[1988:1:1994];
sales=[127 130 136 145 158 178 211];
plot(year, sales, '--r*')
x=[-2: 0.01: 4];
y=3.5 .^ (-0.5*x) .* cos(6*x); plot(x,y)
x=[-2:0.01:4];
y=3*x.^3-26*x+6;
yd=9*x.^2-26;
ydd=18*x;
plot(x,y,'-b',x,yd,'--r',x,ydd,':k')
x=[10:0.1:22];
y=95000./x.^2;
x_data=[10:2:22];
y_data=[950 640 460 340 250 180 140];
plot(x,y,'-')
hold on
plot(x_data,y_data,'ro--')
hold off
xlabel('DISTANCE (cm)')
ylabel('INTENSITY (lux)')
title('Light Intensity as a Function of Distance')
axis([8 24 0 1200])
legend('Theory','Experiment')

Python Code Examples

x=np.array([1,2,3,5,7,7.5,8,10])
y=np.array([2,6.5,7,7,5.5,4,6,8])
plt.plot(x,y)
year=np.arange(1988,1994.1,1)
sales=np.array([127,130,136,145,158,178,211])
plot(year, sales, '--r*')
x=np.arange(-2,4.001,.01)
y=3.5**(-0.5*x)*np.cos(6*x)
plt.plot(x,y)
x=np.arange(-2,4.001,.01)
y=3*x**3-26*x+6
yd=9*x**2-26
ydd=18*x
plt.plot(x,y,'-b',x,yd,'--r',x,ydd,':k')
x=np.arange(10,22.01,0.1)
y=95000/x**2;
x_data=np.arange(10,22.01,2)
y_data=np.array([950,640,460,340,250,180,140])
plt.plot(x,y,'-',label='Theory')
plt.plot(x_data,y_data,'ro--',label='Experiment')
plt.xlabel('DISTANCE (cm)')
plt.ylabel('INTENSITY (lux)')
plt.title('Light Intensity as a Function of Distance')
plt.axis([8,24,0,1200])
plt.legend()

Exercises

  1. Make two separate plots of the function

        \[f(x)=0.01x^4-0.45x^2+0.5x-2\]


    one for -4\leqx\leq4\) and one for -8\leqx\leq8\)
  2. Plot the function

        \[f(x) = \frac{x^2-4x-5}{x-2}\]


    Notice that the function has a vertical asymptote at x=2. Plot the function by creating two vectors for the domain of x, the first vector with elements from -4 to 1.7, and the second vector with elements from 2.3 to 8. For each x vector, create a y vector with the corresponding values of y according to the function. To plot the function make two curves in the same plot.
  3. You have an electrical circuit that includes a voltage source vs with an internal resistance rs and a load resistance R_L. The power P dissipated in the load is given by:

        \[P = \frac{v_s^2R_L}{(R_L+r_s)^2}\]


    Plot the power P as a function of R_L for 1\Omega\leq R_L\leq10\Omega\) given that v_s = 12V and r_s = 2.5\Omega
  4. The Gateway Arch in St. Louis is shaped according to the equation:

        \[y=693.8-68.8\cosh(\frac{x}{99.7})\]


    Make a plot of the arch. Draw a horizontal line at ground level (y=0),
    x = \pm 99.7\arccos (\frac{693.8}{68.8})
  5. In astronomy, the relationship between the relative temperature T/T\textsubscript{SUN} (temperature relative to the sun), relative luminosity L/L\textsubscript{SUN} and relative radius R/R\textsubscript{SUN} of a star is modeled by:
    \frac{L}{L\textsubscript{SUN}} = (\frac{R}{R\textsubscript{SUN}})^2(\frac{T}{T\textsubscript{SUN}})^4
    The HR (Hertzsprung-Russell) diagram is a plot of L/L\textsubscript{SUN} versus the temperature. The following data are given:
SunSpicaRegulusAliothBarndard’s StarEpsilon IndiBeta Crucis
Temp (K)5840224001326094003130428028200
L/L\textsubcript{SUN}11340015010800.1543000
R/R\textsubscript{SUN}17.83.53.70.180.768

To compare the data with the model, plot a HR diagram. The diagram should have two sets of points. One uses the values of L/L\textsubscript{SUN} from the table (use asterisk markers), and the other uses values of L/L\textsubscript{SUN} that are calculated by the equation using R/R\textsubscript{SUN} from the table (use circle markers). In the HR diagram both axes are logarithmic. In addition, the values of the temperature on the horizontal axis are decreasing from left to right. Label the axes and use a legend. (See https://en.wikipedia.org/wiki/Hertzsprung%E2%80%93Russell_diagram for a general explanation of the HR diagram.)

  1. The position x as a function of time that a particle moves along a straight line is given by:

        \[x(t) = -0.1t^4+0.8t^3+10t-70\]


    The velocity v(t) is determined by the derivative of x(t) with respect to t, and the acceleration a(t) is determined by the derivative of v(t) with respect to t.
    Derive the expressions for the velocity and acceleration of the particle, and make plots for the position, velocity, and acceleration as a function of time for 0\leq t \leq 8\). Time t is measured in seconds, and position x is measured in meters. Make three plots, one for position, one for velocity, and one for acceleration. Label the axes appropriately with correct units.
  2. A resistor, R=2\Omega and an inductor, L=1.7H are connected to a voltage source in series (RL circuit). When the voltage source applies a rectangular voltage pulse with an amplitude of V=24V and a duration of 0.5s, the current i(t) in the circuit as a function of time is given by:
    i(t) = (\frac{V}{R})(1-e^\frac{-Rt}{L}) for 0\leq t \leq 0.5\)
    i(t) = e^\frac{-Rt-.5}{L}(\frac{V}{R})(1-e^\frac{0.5R}{L}) for 0.5\leq t
    Make a plot of the current as a function of time for 0\leq t \leq  5\) seconds.