06 matlab graphics
TRANSCRIPT
-
8/14/2019 06 Matlab Graphics
1/19
Matlab Graphics
IntroductiontoMatlab 6
Omed Ghareb AbdullahSulaimani UniversitySulaimani UniversityCollege of SciencesCollege of SciencesPhysics DepartmentPhysics Department
1
Problem(1)
HalfwaverectifierDiode
RVin
=4sin(t) vout
Volts Volts
1 Cycle
Time
1 Cycle
Time
2
-
8/14/2019 06 Matlab Graphics
2/19
MATLABSolutionclear all ;clc
t=[0:pi/ 100:4*pi];
vi=4 sin t ;
for i=1:max(size(t))
if (vi(i )>.6)
vo(i)=vi(i)-.6;
else
vo i =0;
end
end
plot(t,vi,'r.-',t,vo,'b.:')
3
PlottingResults
4
-1
0
1
2
0 2 4 6 8 10 12 14-4
-3
-2
4
-
8/14/2019 06 Matlab Graphics
3/19
MATLABSolutionclear all ;clc
t=[0:pi/ 100:4*pi];
vi=4 sin t ;
for i=1:max(size(t))
if (vi(i )>.6)
vo(i)=vi(i)-.6;
else
vo = ;
end
end
plot(t,vi,'r.-',t,vo,'b.:')
gtext('input voltage');gtext('output voltage')5
PlottingResults
4
-1
0
1
2
output voltage
0 2 4 6 8 10 12 14-4
-3
-2
input voltage
6
-
8/14/2019 06 Matlab Graphics
4/19
Problem(2)Height and speed of a projectile launched with a speed
of at an angle to the horizontal are given by:
where is the acceleration due to gravity. The
projectile will strike the ground when ,
which gives the time to hit,
Suppose that /, , . /
7
MATLABSolution
% Projectile program
vo=20;
g=9.81;
theta=40*pi/180;
t_hit=2*vo*sin(theta)/g;
t=[0:t_hit/100:t_hit];
y=vo*t*sin(theta)-0.5*g*t.^2;
^ ^ ^- .plot(t,y,t,v)
8
-
8/14/2019 06 Matlab Graphics
5/19
PlottingResults
20
8
10
12
14
16
18
0 0.5 1 1.5 2 2.5 30
2
4
6
9
Problem(3)
ProblemStatement:
CalculatethevelocityandaccelerationusingascriptMfile
an xamp e
AlgorithmDevelopment(outline)
Definetimematrix
Calculatevelocityandacceleration
VelocityStart Time = 0 sec
Acceleration
Final Time = 120 sec
Time Increment = 10 sec
Outputresults
in
table
10
-
8/14/2019 06 Matlab Graphics
6/19
MATLABSolutionclear, clc
%Define the time matrix
time = 0:10:120
%Calculate the velocity matrix
velocity = 0.00001*time.^3 - 0.00488*time.^2 ...
+ 0.75795*time + 181.3566;
%Use calculated velocities to find the acceleration
acceleration = 3 - 6.2e-5*velocity.^2;
%Present the results in a table
[time', velocity', acceleration']
11
TableOutput
12
-
8/14/2019 06 Matlab Graphics
7/19
PlottingResults
%Create x-y plots
plot(time,velocity,'or-')
title('Velocity as a function of time')
xlabel('time, seconds')
ylabel('velocity, meters/sec')
grid on
%
figure(2)
lot time acceleration '>b:'
title('Acceleration as a function of time')
xlabel('time, seconds')
ylabel('acceleration, meters/sec^2')
grid on
13
PlottingResults
14
-
8/14/2019 06 Matlab Graphics
8/19
PlottingResults
15
%Use plotyy to create a scale on each side of plot
figure(3)
PlottingResults
plotyy(time, velocity,time,acceleration)
title('Velocity and Acceleration as a function of time')
xlabel('time, seconds')
ylabel('velocity, meters/sec')
grid on
16
-
8/14/2019 06 Matlab Graphics
9/19
PlottingResults
17
TheFigure
Structure
Toolbar
Menu Bar
Line 1
AxesLine 2
18
-
8/14/2019 06 Matlab Graphics
10/19
The
Toolbar
Print figure to the printer or to file
Save figure as a .fig file
Open file (ei ther .fig or .m file)
New figure (instead of using the instruction figure)
19
TheToolbar
Enable plot editing: Whenactivated, it is possible tomark items on the figureand change their properties
button.
20
-
8/14/2019 06 Matlab Graphics
11/19
The
Toolbar
Add text: When activated, itis possible to graphically addtext on top of the figure. Thetext properties (font, size
.the editing button.
21
TheToolbar
Add arrow: Whenactivated, it is possible tographically add an arrowon top of the figure. The
(direction, width, color,etc.) can be manipulatedby the editing button.
22
-
8/14/2019 06 Matlab Graphics
12/19
The
Toolbar
Add line: Whenactivated, it is possibleto graphically add a lineon top of the figure. The
(direction, width, color,
etc.) can be manipulatedby the editing button.
23
TheToolbar
Zoom in(instead ofzoom on/ of f)
Zoom out
Rotate 3D (instead ofrotate3d)
24
-
8/14/2019 06 Matlab Graphics
13/19
AnExample
Vector 1: x(0:0.2:10);
Vector 2: sin(x);
Vector 3: cos(x);
You are asked to:
Plot sin(x) vs. x and cos(x) vs. x in the same figure.
Customize the figure so that it is suitable for presentation.
25
ASimpleMatlab Program
x=[0:0.2:10];
sinx=sin(x);
cosx=cos(x);
plot(x,sinx,x,cosx);
' 'ylabel('y');
title(['sin and cos Functions']);
26
-
8/14/2019 06 Matlab Graphics
14/19
Default
Plot
Given
by
Matlab1
sin and cos Functions
-0.2
0
0.2
0.4
0.6
0.8
y
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
x
27
WeWant
Linesthicker
Colorsmorecontrasting
Keydata
points
visible
Legendorlabelsclosetolines
28
-
8/14/2019 06 Matlab Graphics
15/19
OpenthePropertyEditor
29
EditFigure
Properties
30
-
8/14/2019 06 Matlab Graphics
16/19
EditAxesProperties
31
EditLine
Properties
32
-
8/14/2019 06 Matlab Graphics
17/19
SoFar,ABetterLookingFigure
0.8
1
sin and cos Functions
-0.2
0
0.2
0.4
0.6
y
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
x33
InsertLegend
34
-
8/14/2019 06 Matlab Graphics
18/19
EditLegendProperties
35
InsertYGrid
Line
-
36
-
8/14/2019 06 Matlab Graphics
19/19
FinalVersion!
1.5
sin and cos Functions
data1
data2
0
0.5
1
y
0 1 2 3 4 5 6 7 8 9 10-1.5
-1
-0.5
x37