0% found this document useful (0 votes)
7 views

Ex Matlab

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Ex Matlab

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Matlab Exercises

This file contains both the questions and the necessary theory. The tasks are colored blue for
clarity. The easiest way to save the solutions and upload them to Moodle is probably to open a
separate memo and a Notepad file while working on the solutions, and copy the correct codes there
as you create them. Providing the code as the answer is sufficient unless explicitly asked to provide
the final result or "answer" which doesn’t even necessarily need to be copied.
A picture is worth a thousand words. With Matlab or Octave, you can create high-quality im-
ages. Creating images is not as straightforward as with Mathcad or many other drawing programs,
but the quality and application possibilities are on another level, and the program is very logical
to use.

1. Perhaps the simplest way to plot with Matlab is to use the so-called easy-to-use plotting
function, the ezplot command. Its syntax is ezplot('f(x)'), where f(x) is the function
you want to plot. For example, the line y = x + 1 can be plotted with the command 'x+1'.
Use the ezplot function to plot the following functions:

(a) f (x) = x2 (type, character by character, ezplot('x^2') )


(b) g(x) = sin(x)
(c) h(x) = tan(x)
2
ecos(x )
(d) F (x) =
xcos(x)

2. The actual plotting command in Matlab is plot. Its main principle of operation is as follows:
If x=[x1,...,xn] and y=[y1,...,yn], then plot(x,y) creates a scatter plot, connecting the
point [x1,y1] with the point [x2,y2] with a polyline, and so on. For example, the line segment
connecting the origin and the point (1,-1) can be plotted by typing the command plot([0 ...
1],[0 −1]).

(a) Plot the line segment between the points (0, 1) and (4, 3).
(b) Plot the line segments that connect the following points: (0, 1), (4, 3), (2, 0), and (5, −2).
(c) Plot the "house" shown below using Matlab.
3. If you want to plot the graph of a function f over the interval (a, b) using the plot command,
it can usually be done as follows:

• Create a vector with sufficiently dense points over the interval (a, b), e.g., x=a:0.01:b is
often a good choice.
• Form y=f(x)
• Plot with plot(x,y)

The line y = x + 1 over the interval (-5,5) can thus be plotted by running the following
commands (either via a script or directly in the command window):

x=-5:0.01:5;
y=x+1;
plot(x,y)

There is no need to define auxiliary variables; if desired, they can be directly included in the
plot, e.g., the previous line can also be plotted as:

plot(-5:0.01:5, -5:0.01:5 +1 )

(a) Plot the graph of the sine function over the interval (−π, 2π).
(b) Plot the parabola y = x2 + 2x − 1 over the interval (−5, 5).
(c) The command polyval([an,...,a1,a0],x) calculates the value of the polynomial

p(x) = an xn + . . . , +a1 x + a0

at the point(s) x. Plot the graph of the polynomial

p(x) = 1 + x + x2 + . . . + x9 + x10

over the interval (−2, 2) using the polyval command.

4. If you want to plot multiple graphs in the same coordinate system, you can do this by issuing
the command hold after the first graph is plotted. After this, all plotted graphs will remain in
the plot window. For example, to plot the line y = x + 1 first with the easy plotting function
and then add the parabola y = x2 in the same plot, run the following commands:
ezplot('x+1')
hold
ezplot('x^2')
Using the standard plotting method, you can achieve the same (over the interval (-5,5)) by
running the commands:

x=-5:0.01:5;
y1=x+1;
y2=x.^2;
plot(x,y1)
hold
plot(x,y2)

(a) Plot the graphs of the sine and cosine functions in the same plot over the interval (−5π, 5π).
(b) Use the ezplot function to plot in the same plot the lines passing through the origin with
slopes of 0,1,2,3,4, and 5.

Another method is to give the plot command multiple arguments using the logic plot(x1,y1, ...
x2,y2, x3,y3...), which will cause Matlab to plot the scatter plots of the vector pairs
(x1,y1), (x2,y2) etc. Matlab will automatically use different colors for the pairs’ plots. The
vector pairs do not need to be of the same size. Using this technique, the line y = x + 1 over
the interval (-5,5) and the parabola y = x2 over the interval (-4,4) can be plotted together by
running the commands:

x1=-5:0.01:5;
x2=-4:0.01:4;
y1=x1+1;
y2=x2.^2;
plot(x1,y1, x2,y2)

(c) Using the technique mentioned above, plot the graph of the parabola y = x2 over the
interval (0, π) and the graph of the sine function over the interval (π, 2π) in the same plot.
(d) Using the technique mentioned above, plot the line segments that connect the points (0, 1),
(4, 3), (2, 0), and (5, −2) with each line segment in a different color.

5. Text can be added to a specific point in the plot using the command gtext('text'). A
"crosshair" will appear on the plot, which you can move and click to place it where you want.

(a) Place the text Crest at the peak of the parabola y = −x2 + 3.

The same and other adjustments mentioned below can also be made via the plot window’s
Tools and Edit Plot options.

• The command grid adds/removes a grid to/from the plot.


• The command title('Title') adds a title above the plot.
• The command
xlabel('horizontal') labels the x-axis.
• The command ylabel('vertical') labels the y-axis.

(b) Plot the graph of the parabola y = x2 − 2, add a grid, place the text Pit at the bottom
of the parabola, give the plot the title Pit, label the x-axis as Pit Width and the y-axis
as Pit Height.

You might also like