Sie sind auf Seite 1von 19

Introduction to Matlab 6

Matlab Graphics

Omed Ghareb Abdullah


Sulaimani University
College of Sciences
Physics Department

Problem (1)
y Half‐wave rectifier
Diode

Vin=4sin(t) R vout

Volts Volts

Time Time

1 Cycle 1 Cycle

1
MATLAB Solution
clear all;clc
t=[0:pi/100:4*pi];
vi=4*sin(t);
i 4* i (t)
for i=1:max(size(t))
if (vi(i)>.6)
vo(i)=vi(i)-.6;
else
vo(i)=0;
(i) 0
end
end
plot(t,vi,'r.-',t,vo,'b.:')

Plotting Results
4

-1

-2

-3

-4
0 2 4 6 8 10 12 14

2
MATLAB Solution
clear all;clc
t=[0:pi/100:4*pi];
vi=4*sin(t);
i 4* i (t)
for i=1:max(size(t))
if (vi(i)>.6)
vo(i)=vi(i)-.6;
else
vo(i)=0;
(i) 0
end
end
plot(t,vi,'r.-',t,vo,'b.:')
gtext('input voltage');gtext('output voltage') 5

Plotting Results
4

output voltage
0

-1

input voltage
-2

-3

-4
0 2 4 6 8 10 12 14

3
Problem (2)
Height and speed of a projectile launched with a speed
of at an angle
g to the horizontal are g
given by:
y

where is the acceleration due to gravity. The


projectile will strike the ground when ,
which gives the time to hit,

Suppose that   / , , . /
7

MATLAB Solution
% Projectile program
vo=20;
g=9.81;
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;
v=sqrt(vo^2
v=sqrt(vo 2-2*vo*g*sin(theta)*t+g
2*vo*g*sin(theta)*t+g^2*t
2*t.^2);
2);
plot(t,y,t,v)

4
Plotting Results
20

18

16

14

12

10

0
0 0.5 1 1.5 2 2.5 3

Problem (3)
y Problem Statement:
y Calculate the velocity and acceleration using a script M‐file
¾Hand Example
H d E l

¾Algorithm Development (outline)
9Define time matrix
9Calculate velocity and acceleration
9Output results in table

Start Time = 0 sec


Velocity
Final Time = 120 sec
Acceleration
Time Increment = 10 sec
10

5
MATLAB Solution
clear, 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

Table Output

12

6
Plotting Results
%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)
plot(time,
p ot(t e, acce
acceleration,'>b:')
e at o , b: )
title('Acceleration as a function of time')
xlabel('time, seconds')
ylabel('acceleration, meters/sec^2')
grid on

13

Plotting Results

14

7
Plotting Results

15

Plotting Results
%Use plotyy to create a scale on each side of plot
figure(3)
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
Plotting Results

17

The Figure Structure

Menu Bar

Toolbar
Line 1

Axes
Line 2

18

9
The Toolbar

Print figure to the printer or to file


Save figure as a .fig file
Open file (either .fig or .m file)
New figure (instead of using the instruction figure)

19

The Toolbar

Enable plot editing: When


activated, it is possible to
mark items on the figure
and change their properties
by using the right mouse
button.

20

10
The Toolbar

Add text: When activated, it


is possible to graphically add
text on top of the figure. The
text properties (font, size
etc ) can be manipulated by
etc.)
the editing button.

21

The Toolbar

Add arrow: When


activated, it is possible to
graphically add an arrow
on top of the figure. The
arrow properties
(direction, width, color,
etc.) can be manipulated
by the editing button.

22

11
The Toolbar

Add line: When


activated, it is possible
to graphically add a line
on top of the figure. The
line properties
(direction, width, color,
etc.) can be manipulated
by the editing button.

23

The Toolbar

Zoom in(instead of zoom on/off)


Zoom out
Rotate 3D (instead of rotate3d)

24

12
An Example
y You have:
y Vector 1: x(0:0.2:10);
y Vector 2: sin(x);
y Vector 3: cos(x);

y You are asked to:


y Plot sin(x) vs. x and cos(x) vs. x in the same figure.
y Customize the figure so that it is suitable for presentation.

25

A Simple Matlab Program
clear all;
x=[0:0.2:10];
sinx=sin(x);
cosx=cos(x);
plot(x,sinx,x,cosx);
xlabel( x );
xlabel('x');
ylabel('y');
title(['sin and cos Functions']);

26

13
Default Plot Given by Matlab
sin and cos Functions
1

0.8

0.6

0.4

0.2

0
y

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7 8 9 10
x
27

We Want …
y Titles and labels bigger

y Lines thicker

y Colors more contrasting

y Key data points visible

y Legend or labels close to lines

28

14
Open the Property Editor

29

Edit Figure Properties

30

15
Edit Axes Properties

31

Edit Line Properties

32

16
So Far, A Better‐Looking Figure
sin and cos Functions
1

0.8

0.6

0.4

0.2

0
y

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7 8 9 10
x
33

Insert Legend

34

17
Edit Legend Properties

35

Insert Y Grid Line

9 -

36

18
Final Version!
sin and cos Functions
1.5
data1
data2

0.5

0
y

-0.5

-1

-1.5
0 1 2 3 4 5 6 7 8 9 10
x
37

19

Das könnte Ihnen auch gefallen