EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept...

11
EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

Transcript of EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept...

Page 1: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

EGR 115 Introduction to Computing for Engineers

MATLAB Basics 4: Intro to Plotting

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

Page 2: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

Lecture Outline

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Intro to Plotting

Slide 2 of 11

Page 3: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

MATLAB basicsIntro to Plotting: Plot Function

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Presenting data in graphical form is very useful.• It is quite easy to plot data in MATLAB

The format for the “plot” command is:>> plot(indep_var, dep_var)

o Both the independent variable (say, x) and dependent variable (say, y(x)) should be the same size.

>> x = [0:0.1:3]; % Independent variable>> y = round(x); % Dependent variable>> plot(x, y)

0 0.5 1 1.5 2 2.5 30

0.5

1

1.5

2

2.5

3

Dep VarIndep Var

Slide 3 of 11

Page 4: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

MATLAB basicsIntro to Plotting: Plot Function

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Controlling the style of the line plot Changing the line color (to red)

>> plot(x, y, 'r'); % Choices: c, y, m, k, r, g, b

Change the linestyle to dashed>> plot(x, y, '--'); % Dashed linestyle

Change the line marker to ‘+’>> plot(x, y, '+'); % Marker set to ‘+’

Plot with a magenta colored dotted line and square symbols

0 0.5 1 1.5 2 2.5 30

0.5

1

1.5

2

2.5

3

Slide 4 of 11

Page 5: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

MATLAB basicsIntro to Plotting: Plot Function

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Superimposing Multiple Plots>> z = floor(x);>> plot(x, y, 'm:s', x, z, 'r-');>> title('Multiple Plots')>> xlabel('x')>> ylabel('Magnitude')>> legend('round(x)', 'floor(x)');

• Multiple Plot Windows Precede the plot(..) command with the command “figure”>> figure, plot(…)

o This will create a new figure window!!

0 0.5 1 1.5 2 2.5 30

0.5

1

1.5

2

2.5

3Multiple Plots

x

Mag

nitu

de

round(x)

floor(x)

Slide 5 of 11

Page 6: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

MATLAB basicsAdditional Info

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• Additional Information: Compute the dot product between two vectors

o Must be the same length (N, say)

o Example: Given, and , then

o Use MATLAB to compute the above dot product.

1

N

i ii

a b a b

1

1* 1 3*0 2*3 5N

i ii

a b a b

Slide 6 of 11

Page 7: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

MATLAB basicsAdditional Info

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

Computing the Norm (i.e., magnitude) of a vectoro Simply the square root of dot product with itself

>> sqrt(dot(v, v)) % or>> norm(v)

What is the norm of ?o Sqrt(1+4+9+16) = Sqrt(30) = 5.4772

Slide 7 of 11

Page 8: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

MATLAB basicsAdditional Info

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• In Class Problem #1: Given the two vectors and ()

>> v = [4 3 2 2];>> w = [2 5 7 9];

write a script to calculate:

>> c = 2*4 + dot(1./v, w);

>> c = sum(2 + w ./ v);

1

12

N

ii i

c wv

= 18.167

Slide 8 of 11

Page 9: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

MATLAB basicsAdditional Info

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• In Class Problem #2: Plot both and on the same figure.

o The former line should be blue and dottedo The latter should be red and dash-dottedo Add a legend, axis labels, title, and grid.o Plot limits should be [− , ] for the -axis and [−5, 5] for the 𝜋 𝜋 𝑥

-axis.𝑦– How do we set the axis scaling?– How do we add a grid?– Can we use Greek letters (i.e., )?

Slide 9 of 11

Page 10: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

MATLAB basicsAdditional Info

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• In Class Problem #2: Plot both and on the same figure.

-3 -2 -1 0 1 2 3-5

-4

-3

-2

-1

0

1

2

3

4

5A Plot of tan() and d(tan()/dt

(rad)

Mag

nitu

de

tan()

d(tan()/dt

Slide 10 of 11

Page 11: EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.

Next Lecture

Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers

• MATLAB Applications Vector math

Slide 11 of 11