computer programming in matlab - atatürk Üniversitesi

23
Atatürk University Plots in MATLAB Atatürk University Prof. Dr. İrfan KAYMAZ Computer Programming in MATLAB Atatürk University Engineering Faculty Department of Mechanical Engineering

Upload: others

Post on 14-Nov-2021

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

Atatürk University

Prof. Dr. İrfan KAYMAZ

Computer Programming in

MATLAB

Atatürk University Engineering Faculty

Department of Mechanical Engineering

Page 2: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

Atatürk University

Arrays in MATLAB; Vectors and Matrices Graphing Plots in MATLAB

Page 3: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

Atatürk University

Before graphing plots in MATLAB, different vector generation methods will be presented here:

Vector Generation

clc clear all for i=1:5 A(i)=i; end A

A =

1 2 3 4 5

1. Method

>> A=1:5 A = 1 2 3 4 5

2. Method

Page 4: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB Vector Generation

clc clear all s=0; for i=1:2:7 s=s+1; A(s)=i; end A

A =

1 3 5 7

1. Method

>> A=1:2:7 A = 1 3 5 7

2. Method

A=start:increment:end

creates row vector x

starting with start, counting

by increment, ending at or

before end.

Page 5: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB Element-by-element

If you want to execute any arithmetic operation element-by-element basis on arrays,

clc clear all A=[9 5 6 12 8]; B=[-2 3 5 7 6]; s=0; for i=1:length(A) s=s+1; C(s)=A(s)*B(s); end C

C =

-18 15 30 84 48

1. Method

>> A=[9 5 6 12 8]; >> B=[-2 3 5 7 6]; >> C=A.*B C = -18 15 30 84 48

2. Method

Page 6: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB Vector Production

The operations are sometimes called array operations, or element-by-element operations because the operations are performed element by element. For example, a.*b results in the following vector (sometimes called the array product):

[a(1)*b(1) a(2)*b(2) a(3)*b(3)]

a./b gives element-by-element division.

Operator Description

.* Multiplication

./ Right division

.\ Left division

. ˆ Power

Page 7: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

A picture, it is said, is worth a thousand words. MATLAB has a powerful graphics system for presenting and visualizing data, which is reasonably easy to use.

Graphs (in 2-D) are drawn with the plot statement.

Graphing Plots in MATLAB

Page 8: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB Example 1

Write a MATLAB program that computes the values of the following equation for x varying from −2 to 2 in increments of 1, and then plots the x and y data.

2092 xxyx=1:20;

y=x.^2-9*x-20;

plot(x,y)

Page 9: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

Title the plot

Label the axes

title(’text’) Labels top of plot with text in quotes.

xlabel(’text’) Labels horizontal (x) axis with text in quotes.

ylabel(’text’) Labels vertical (y) axis with text in quotes.

Plot Editing Commands

Page 10: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB Example 2

x=1:1:20;

y=x.^2-9*x-20;

plot(x,y)

title('variation of x.^2-9*x-20 equation wit x ');

xlabel('x-axis');

ylabel('y-axis');

Page 11: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

It is possible to draw multiple plots on the same set of axes.

The first way is to use plot with multiple arguments, e.g.

plot(x1, y1, x2, y2, x3, y3, ... )

Multiple Plots

Page 12: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB Example 3

Write a MATLAB program that plots the variation of following equation and its deviation with t on the same set of axes.

853)( 2 ttty 56)( tty

t=0:1:20;

y_1=t.^3-5*t+8;

y_2=6*t-5;

plot(t,y_1,t,y_2)

Page 13: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

Write a MATLAB program that plots the sin(x) and cos(x) variations on the same set of axes for x varying from 0 to 2*pi.

Example 4

Page 14: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

Color, Markers and Line styles

plot(x,y,’ro:’) this command plots the variation of x and y with a red colored line, draws circles at the data points and joins the plotted points with dotted line

Color, markers and Line styles can be specified by giving plot an additional argument following the complex variable name. This optional additional argument is a character string (enclosed in single quotes) consisting of characters from the following table:

Plot Editing Commands

Page 15: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB Color, Marker, Style Color Marker Line Styles

y: yellow + Plus sign - : continuous line

m:magenta o Circle : : dotted line

b:blue * Asterisk -. : dash-dot line

r:red . Point -- : dash-dash line

g:green x Cross

w:white 'square' or sSquare

k: black 'diamond' or dDiamond

^ Upward-pointing triangle

v Downward-pointing triangle

> Right-pointing triangle

< Left-pointing triangle

' pentagram' or pFive-pointed star

(pentagram)

' hexagram' or hSix-pointed star

(hexagram)

+ Plus sign

o Circle

* Asterisk

Page 16: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB legend

To distinguish between plots on the graph, the legend command is used. This

command has the form:

legend(’string1’,’string2’,’string3’,...)

It places a box on the plot, with the curve type for first curve labeled with the text

string ’string1’ , the second curved labeled with the text string ’string2’ , and so on.

The position of the curve can be controlled by adding a final integer position

argument pos to the legend command.

Values of pos and the corresponding positions are:

0 = Automatic “best” placement (least conflict with data)

1 = Upper right-hand corner (default)

2 = Upper left-hand corner

3 = Lower left-hand corner

4 = Lower right-hand corner

-1 = To the right of the plot

Page 17: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

x=[0:pi/10:4*pi];

y1=sin(x);

y2=cos(x);

plot(x,y1,'b.:',x,y2,'b<-')

xlabel('Radians');

ylabel('Function Value');

title('Variation of sinx and its deviation')

legend('sin(x)','cos(x)',-1)

Example 5

Page 18: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

A subsequent plot command will draw a new graph in this same window. The figure

command allows the creation of multiple plot windows, allowing the display of

multiple graphs. The command:

figure(n)

where n is a positive integer, will create a window named “Figure No. n.” Subsequent

plot commands will draw graphs in this window, known as the active window

x=0:pi/30:2*pi;

y1=sin(x);

y2=cos(x);

figure(1),plot(x,y1,'bo:')

figure(2),plot(x,y2,'r*-')

Multiple Figures

Page 19: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

The subplot command allows you to split the graph window into subwindows . The

possible splits are two subwindows (top and bottom or left and right) or four

subwindows (two on top, two on the bottom).

subplot(m,n,p): m by n grid of windows, with p specifying the current plot as the pth

window.

subplot

Page 20: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

subplot(2,2,1)

x=0:pi/30:2*pi;

y1=sin(x);

plot(x,y1)

title('f(x)=sin(x)')

subplot(2,2,2)

y2=cos(x);

plot(x,y2)

title('f(x)=cos(x)')

subplot(2,2,3)

y3=tan(x);

plot(x,y3)

title('f(x)=tan(x)')

subplot(2,2,4)

y4=cot(x);

plot(x,y4)

title('f(x)=cot(x)')

Example 6

Page 21: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

x=0:pi/30:2*pi;

y1=sin(x);

plot(x,y1,'k*:')

hold

y2=exp(2*sin(x));

plot(x,y2,'ro--')

legend('sin(x)','\bf{e}^{2*sin(x)}')

Mutiple plots (hold)

The second way is simply to use hold to keep the current plot on the axes. All

subsequent plots are added to the axes until hold is released, either with hold off, or

just hold, which toggles the hold state.

Page 22: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

Write a MATLAB program that plots the following graphs by using 500 points for x

varying from -2pi to 2pi (-2 π ≤ x ≤ 2 π)

Example 7

Page 23: Computer Programming in MATLAB - Atatürk Üniversitesi

Atatürk University

Plots in MATLAB

Figure 1 Figure 2

Figure 3