A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.



Saturday, March 19, 2016

Plotting in MATLAB - Part 2

Controlling Axis Properties

The limits and scale of a plot will be automatically set by the MATLAB when you run a plot command. There are commands defined to change this default values. The common commands used for axis scaling and appearance are given below.
axis([xmin xmax ymin ymax]) command sets scaling for the x- and y-axes on the current plot. This command is useful to display a specific area of a plot. 
Example: axis([0 20 0 100]).
axis auto  - returns the axis scaling to its default, automatic mode
axis off  - turns off all axis labeling, tick marks and background.
axis on   - turns axis labeling, tick marks and background back on.
axis equal – makes both axes equal division
axis square - Sets axis region to be square.
axis tight - Sets the axis limits to the range of the data.
grid on  -  Adds grid lines to the plot.
grid off  - Removes grid lines from the plot.

Formatting the Text in Plots

The text displayed in the plots can be formatted by adding the property name and values following the string. The formatting can be used to define the font, size, superscript, subscript, italic, bold, and color of the characters, the color of the background, and many other properties. Font style can be formatted by adding the ‘modifiers’ in the string. These modifiers affect the text from the point at which they are inserted until the end of the string. If the modifier is required for a section only, then type the modifier and the text to be affected inside braces { }.
\bf - bold font
\it - italic style
\rm - normal font
_  - subscript (only one character)
^  - superscript (only one character)
For subscript and superscript, if we need to apply for more characters, put them in braces { }.
Syntax to define properties is
text(x,y,‘Your Text Here’,PropertyName, PropertyValue)
xlabel(‘d_2-d_1’)        displays x label as d2-d1
ylabel(‘x^2’)                displays y label as x2
title('\fontname{Arial}Plot of x^2','FontSize',14)  displays title in Arial font with size 14
text(pi/2,1,'\it This is the maxima of the function.','EdgeColor','r','LineWidth',2)  displays the text in the plot in italic font with the defined properties.

Typing Greek Characters

The Greek characters can be used in the strings by typing their codes following a \. The commands are given below.
α          \alpha
β          \beta
γ          \gamma
θ          \theta
π          \pi
σ          \sigma
Φ         \Phi
Δ          \Delta
Ω         \Omega

Plotting a Function

The fplot command is used to plot a function y = f(x) between given limits. The function can be specified as a text string writing, and the limits inside the square bracket.
fplot(‘function’,[limits],‘line specifiers’)
The function can be a Matlab built in function or a user defined function on independent variables. The line specifiers are same as in the plot.
Will plot sine wave for the specified period.

Logarithmic Plots

Logarithmic plots are very important in engineering analysis. MATLAB commands to plot logarithmic curves are given below.
semilogy(x,y)    Plots y versus x with a log10 scale for the y axis and linear scale for the x axis.
semilogx(x,y)    Plots y versus x with a log10 scale for the x axis and linear scale for the y axis.
loglog(x,y)        Plots y versus x with a log (base 10) scale for both axes.

(picture courtesy: MATLAB an Introduction, Amos Gilat)

Polar Plots

The polar command is used to plot functions in polar coordinates. The syntax is
where the angle of vector is theta, in radians, and the radius is rho.
>> t = linspace(0,2*pi,1000);
>> polar(t,sin(2*t))

Plotting Complex Data

When the arguments to plot are complex, the imaginary parts are ignored, except when the plot is given in single complex argument. For this case the plot is done between the real and imaginary parts. If z is a complex variable, plot(z) is same as plot(real(z), imag(z)).
>> t= linspace(0,2*pi,1000);
>> z=exp(i*t);
>> plot(z)
>> axis square

submit to reddit