Basic Matlab Exercises

download Basic Matlab Exercises

of 2

Transcript of Basic Matlab Exercises

  • 8/8/2019 Basic Matlab Exercises

    1/2

    MATLAB EXERCISES

    1. Equation of a straight line: The equation of a straight line is y=m xc where m and c are constants.Compute they-coordinates of a line with slope m = 0.5 and the intercept c=2 at the followingx-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=tsin t

    y=t1

    t1

    z=sin t2

    t2

    3. Points on a circle: All points with coordinates x=rcos and y=rsin , where ris a constant, lie

    on a circle with radius r, i.e. they satisfy the equation x 2y2=r2. Create a column vector for with

    the values 0, /4, /2, 3/ 4, , and5/

    4.

    Take r=2 and compute the column vectorsx andy. Now check thatx andy indeed satisfy the equation of

    circle, by computing the radius r=x 2y2 .[To calculate r you will need the array operator .^ for squaringx andy. Of course, you could compute x

    2

    by x.*x also.]

    4. A simple sine plot: Plot y=sinx , 0x2, taking 100 linearly spaced points in the given interval.

    Label the axes and put 'Plot created byyourname' 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 twoplots with the command plot(x,y,x,y,'o') to show the line through the data points as well as the distinct datapoints.

    6. An exponentially decaying sine plot: Plot y=e0.4 x sin x , 0x4 , taking 10, 50, and 100 points in

    the interval. [ Be careful about computingy. 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 , zt=t , 0t20 .

    8. Overlay plots: . Plot y=cosx in red and z=1x

    2

    2x

    4

    24in blue for 0x on the same plot.

    [Hint: Use plot(x,y,'r',x,z,'--') ]. Add a grid to the plot using the command: grid on;

  • 8/8/2019 Basic Matlab Exercises

    2/2

    ANSWERS TO EXERCISES

    1. x=[0 1.5 3 4 5 7 9 10];y = 0.5*x-2

    Ans.y =[-2.0000 -1.2500 -0.5000 0 0.5000 1.5000 2.5000 3.0000].

    2.t = 1:1:10;

    x = t.*sin(t)y = (t-1)./(t+1)z = sin(t.^2)./(t.^2)

    3. theta = [0;pi/4;pi/2;3*pi/4;pi;5*pi/4]r = 2;x = r*cos(theta); y =r*sin(theta);sqrt(x.^2+y.^2)

    4. x = linspace(0,2*pi,100);plot(x,sin(x))

    xlabel('x'), ylabel('sin(x)')title('Plot created by Stefania')

    5. plot(x,sin(x),x,sin(x),'o')xlabel('x'), ylabel('sin(x)')

    6. x=linspace(0,4*pi,10); %with 10 pointsy=exp(-.4*x).*sin(x);plot(x,y)x=linspace(0,4*pi,50); %with 50 pointsy=exp(-.4*x).*sin(x);

    plot(x,y)x=linspace(0,4*pi,100); %with 100 pointsy=exp(-.4*x).*sin(x);plot(x,y)

    7. t = 0:0.1:20;plot3(sin(t),cos(t),t)

    8. x = linspace(0,pi,100);y = cos(x);z = 1-x.^2/2+x.^4/24;

    plot(x,y,'r',x,z,'--');grid on;legend('cos(x)','z') %try this legend command