# MATLAB EXERCISES 1. Equation of a straight line: The equation of a straight line is y =m x c where m and c are constants.

Compute the y-coordinates of a line with slope m = 0.5 and the intercept c=−2 at the following x-coordinates: x = 0, 1.5, 3, 4, 5, 7, 9, and 10 2. Multiply, divide, and exponentiate vectors: Create a vector t with 10 elements: 1,2,3, ..., 10. Now compute the following quantities: x =t sint  • t −1 y= • t 1 sin t 2  z= 2 • t 3. Points on a circle: All points with coordinates x =r cos  and y =r sin  , where r is a constant, lie on a circle with radius r, i.e. they satisfy the equation x 2 y 2=r 2 . Create a column vector for  with the values 0, / 4, /2, 3 / 4,  , and 5 / 4 . Take r =2 and compute the column vectors x and y. Now check that x and y indeed satisfy the equation of circle, by computing the radius r=  x 2  y 2 . [To calculate r you will need the array operator .^ for squaring x and y. Of course, you could compute x 2 by x.*x also.] 4. A simple sine plot: Plot y =sin x , 0≤x ≤2  , taking 100 linearly spaced points in the given interval. Label the axes and put 'Plot created by yourname' in the title. 5. Line-styles: Make the same plot as above, but rather than displaying the graph as a curve, show the unconnected data points. To display the data points with small circles, use plot(x,y,'o'). Now combine the two plots with the command plot(x,y,x,y,'o') to show the line through the data points as well as the distinct data points. 6. An exponentially decaying sine plot: Plot y =e−0.4 x sin x , 0≤x ≤4  , taking 10, 50, and 100 points in the interval. [ Be careful about computing y. You need array multiplication between exp(-0.4*x) and sin(x)]. 7. Space curve: Use the command plot3(x,y,z) to plot the circular helix x t =sin t , y t =cos t , z t =t , 0≤t≤20 . 8. Overlay plots: . Plot y =cos x in red and z=1− x2 x4 in blue for 0≤x≤ on the same plot.  2 24 [Hint: Use plot(x,y,'r',x,z,'--') ]. Add a grid to the plot using the command: grid on;

theta = [0./(t. y =[-2.*sin(x).4*x).pi.4*pi. plot(x. %with 100 points y=exp(-.5*x-2 Ans.4*pi.10). 2.y) x=linspace(0.^2+y.0000 -1.^4/24.y) 7.*sin(x)./(t+1) z = sin(t.3*pi/4.cos(t).y) x=linspace(0. y =r*sin(theta). sqrt(x. plot(x.pi/2.'o') xlabel('x'). x = linspace(0. t = 0:0. plot(x.^2/2+x.x.4*x). ylabel('sin(x)') title('Plot created by Stefania') 5.5*pi/4] r = 2.z.5000 3.*sin(x). plot(x. plot(x.pi/4.100). %with 50 points y=exp(-.y. plot3(sin(t).5000 0 0. x = t. z = 1-x.'r'.5 3 4 5 7 9 10].'z') %try this legend command .sin(x).100).*sin(t) y = (t-1). ylabel('sin(x)') 6. t = 1:1:10.^2) 3.50). x = linspace(0. y = 0. plot(x. legend('cos(x)'.2*pi.pi.100). x=[0 1.ANSWERS TO EXERCISES 1.2500 -0.x. x=linspace(0. %with 10 points y=exp(-.5000 2.0000].1:20. y = cos(x). grid on.sin(x).t) 8.4*pi.'--').^2) 4.5000 1. x = r*cos(theta).4*x).^2).sin(x)) xlabel('x').