You are on page 1of 13

Plotting graphs in Matlab

glmoyo
Matlab Script
• Matlab can runs scripts (set of commands that you may save as a file)
• To open a script editor simple click “new script”
Example a: solving simultaneous equations:
V1 =1.2I1 +0.4V2=0.6
I2=‐0.4I1+0.4V2 =0.2
• % solve simultaneous equations
• % V1= 1.2I1 + 0.4V2
• % I2= -0.4I1 + 0.4V2
• % if V1=0.6 I2=0.2
• clc
• syms I1 V2 %declaring variables current I1 and voltage V2
• eq1 = 1.2*I1+ 0.4*V2==0.6;
• eq2 = -0.4*I1+0.4*V2==0.2;
• [V1,I2] = equationsToMatrix([eq1, eq2], [I1, V2])
• solution_I1_V2 = linsolve(V1,I2)
Type in script then click run
Results

• The solution
Example b: solving simultaneous equations:
V1 =1.2I1 +0.4V2=0.6
I2=‐0.4I1+0.4V2 =0.2
• % solve simultaneous equations
• % V1= 1.2I1 + 0.4V2
• % I2= -0.4I1 + 0.4V2
• % if V1=0.6 I2=0.2
• clc
• syms I1 V2 %declaring variables current I1 and voltage V2
• eq1 = 1.2*I1+ 0.4*V2==0.6;
• eq2 = -0.4*I1+0.4*V2==0.2;
• sol= solve([eq1, eq2], [I1, V2]);
• current_1=sol.I1
• Voltage_2=sol.V2
Click the + sign to add
another script

• Type this code and


then run
• Here we use solve
command

• Results are shown


on the command
window
The Help command
• On the command you can type help followed by the command you want to know about
• Example:
• >> help plot
• plot Linear plot.
• plot(X,Y) plots vector Y versus vector X. If X or Y is a matrix,
• then the vector is plotted versus the rows or columns of the matrix,
• whichever line up. If X is a scalar and Y is a vector, disconnected
• line objects are created and plotted as discrete points vertically at
• X.

• plot(Y) plots the columns of Y versus their index.
• If Y is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)).
• In all other uses of plot, the imaginary part is ignored.

• Various line types, plot symbols and colors may be obtained with plot(X,Y,S) where S is a
character string made from one element from any or all the following 3 columns:

• b blue . point ‐ solid
• g green o circle : dotted
• r red x x‐mark ‐. dashdot
• c cyan + plus ‐‐ dashed
• m magenta * star (none) no line
• y yellow s square
• k black d diamond
• w white v triangle (down)
• ^ triangle (up)
• < triangle (left)
• > triangle (right)
• p pentagram
• h hexagram

Example of help command: help solve
Graphical solution
• % solve simultaneous equations
• % V1= 1.2I1 + 0.4V2
• % I2= -0.4I1 + 0.4V2
• % if V1=0.6 I2=0.2
• close
• clc
• syms I1 V2 %declaring variables current I1 and voltage V2
• I1= [0:0.01:1];
• V2a= -(1.2/0.4)*I1 + (0.6/0.4); % same equations but make V2 subject of the formula
• plot(I1,V2a)
• grid on; % show grid
• grid minor;

• hold on; % do not remove previous graph rather plot over it

• V2b =(0.4/0.4)*I1 + (0.2/0.4);
• plot(I1,V2b, 'r --') % r for red - - for dashed line
Graph
• Matlab produces a graph on a separate window

You might also like