Вы находитесь на странице: 1из 2

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=
t2

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 .

x2 x4
8. Overlay plots: . Plot y =cos x in red and z=1−  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;
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 points


y=exp(-.4*x).*sin(x);
plot(x,y)
x=linspace(0,4*pi,50); %with 50 points
y=exp(-.4*x).*sin(x);
plot(x,y)
x=linspace(0,4*pi,100); %with 100 points
y=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

Вам также может понравиться