Plotting functions in MATLAB
MATLAB has built in functions that allow one to generate x-y polar,contour and three-dimentional plots and bar charts. MATLAB also allows...
https://things-for-students.blogspot.com/2012/03/plotting-functions-in-matlab.html
MATLAB has built in functions that allow one to generate x-y polar,contour and three-dimentional plots and bar charts.MATLAB also allows one to give title to graph,label the x and y axis, and a grid to graph.In addition, there are commands for controlling the screen and scalling.
X-Y Plot and annotations:
The plot command generates a linear-y plot. This command has three variations .
Plot(x)
Plot(x,y)
Plot(x1,y1,x2,y2,x3,y3,……..,xn,yn)
If x is a vector, the command plot(x) is used.
If x and y are vectors of the same length, then the command plot(x,y) is used.
Plot(x1,y1,x2,y2,x3,y3,……..,xn,yn) is used for multiple arguments.
To write text on a graphic screen beginning at a point (x,y) on the graphic screen ,one can use the command text(x,y,’any text’)
Example:
t=0:0.25:7;
y = sin(t);
plot(t,y)
Example:
Response of an RC Circuit:
Solution:
MATLAB Script
T=0:0.5:4;
Y=6*exp(-2*t);
Plot(t,y);
Title(‘Response of an RC Circuit: ’)
Xlabel(‘ Time in seconds’)
Ylabel(‘Voltage in volts’)
Grid
Voltage and current of an RL Circuit:
For an RL Circuit, the voltage v(t) and current i(t) are given as:
V(t)=10cos(377t)
I(t)=5cos(377t+600)
Solution:
MATLAB Script
% RL Circuit
% current i(t) and voltage v(t) are generated
% t is time
T=0:1E-3:20E-3;
V=10*cos(377*t);
A_rad=(60*pi/180); % angle in radians.
I=5*cos(377*t+a_rad);
Plot(t,v,t,v’*’,t,I,t,I,’o’)
Title(‘Voltage and current in RL Circuit : ’)
Xlabel(‘ Sec’)
Ylabel(‘Voltage(v) and current(mA)’)
Text(0.003,1.5,’v(t));
Text(0.009,2,’i(t));
Ø Logarithmic Plots:
Logarithmic and semi logarithmic plots can be generated using the commands loglog,semilog,semilogy.
The descriptions of these commands are as follows:
Loglog(x,y)-generate a plot of log10(x) vs log10(y).
Semilogx(x,y)- generate a plot of log10(x) vs linear axis of y.
Semilogy(x,y)- generate a plot of linear axis of x vs log10(y).
Example:
Frequency(Hz) | Gain(dB) | Frequency(Hz) | Gain(dB) |
20 | 5 | 2000 | 34 |
40 | 10 | 5000 | 34 |
80 | 30 | 8000 | 34 |
100 | 32 | 10000 | 32 |
120 | 34 | 12000 | 30 |
Draw a graph of gain vs frequency using logarithmic scale.
Solution:
MATLAB Script
% Gain vs frequency plot
F=[20 40 80 100 120 2000 5000 8000 10000 12000];
G=[5 10 30 32 34 34 34 34 32 30];
Semilogx(f,g)
Title(‘gain vs frequency plot ’)
Xlabel(‘ Frequency in hertz ’)
Ylabel(‘gain in dB’)
Basic Task: Plot the function sin(x) between 0≤x≤4π
Create an x-array of 100 samples between 0 and 4π.
Calculate sin(.) of the x-array
Plot the y-array
Solution:
MATLAB Script
x=linspace(0,4*pi,100);
y=sin(x);
plot(y)