0% found this document useful (0 votes)
6 views

Lect10 2d Plotting & Curve Fitting

The document discusses various plotting and curve fitting techniques in MATLAB including subplots, polar plots, logarithmic plots, bar charts, pie charts, linear regression, and polynomial fitting using polyfit and polyval. Examples are provided for each technique.

Uploaded by

Khoa Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lect10 2d Plotting & Curve Fitting

The document discusses various plotting and curve fitting techniques in MATLAB including subplots, polar plots, logarithmic plots, bar charts, pie charts, linear regression, and polynomial fitting using polyfit and polyval. Examples are provided for each technique.

Uploaded by

Khoa Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture 10

2D plotting &
curve fitting
Subplots
Other 2-D Plots
Curve fitting
Plotting and Curve Fitting
 Visualizing trends by plotting large sets of
data from experiments or from computer
calculations helps you interpret the data.
There are numerous grapical tools
available in MATLAB.
 Curve fitting is a powerful way to use a
set of data to find a mathematical model
that approximates the set of data.
Subplots
 Subplot command allows you to put
multiple graphs on one figure window.
subplot(m,n,p) divides figure window
into a grid of m rows and n columns.
Variable p identifies the part of the
window where the plot is placed.

p=1 p=2
p=3 p=4
Examples of subplots
 To graph sin(x) and cos(x) on the same
figure side-by-side do the following: 1 1

x = 0:0.1:2*pi; 0.8 0.8

0.6 0.6
subplot(1,2,1);
0.4 0.4

plot(x, sin(x)); 0.2 0.2

0 0

subplot(1,2,2); -0.2 -0.2

plot(x, cos(x)); -0.4 -0.4

When a figure with a subplot is -0.6 -0.6

open, you must close it before


-0.8 -0.8
opening a new figure in order for the
new figure to display properly. -1
0 2 4 6 8
-1
0 2 4 6 8
Other utilities for 2-D plots
 MATLAB supports several 2-D plotting
utilities:
– Polar plots
– Logarithmic plots
– Bar graphs
– Pie charts
Polar plots
 MATLAB supports tools for plotting data in
polar coordinates. 1
90
1
120 60
theta = 0:0.01:pi; 0.9
0.8

0.8
r = sin(theta); 150
0.6
30
0.7 0.4

plot(theta, r); 0.6 0.2

polar(theta, r); 0.5 180 0

0.4

0.3
210 330

0.2 polar(theta,r) plots a circle

0.1 plot(theta,r) plots a sine curve.


240 300

270
0
0 0.5 1 1.5 2 2.5 3 3.5
Logarithmic plots
 MATLAB has tools for three kinds of logarithmic
plots:
– semilogx
– semilogy
– loglog
 These pltting utilities automatically replace linear
scales with logarithmic scales.
 Logarithmic scales are useful when a variable
ranges over many orders of magnitude.
Example: Logarithmic plots &
subplots 5 5
x 10 x 10

x = 0:0.1:20; 8 8

y = 5*x.^4; 6 6

subplot(2,2,1); 4 4

plot(x,y); 2 2

subplot(2,2,2); 0 0
-2 0 2
0 5 10 15 20 10 10 10
semilogx(x,y);
10 10

subplot(2,2,3); 10 10

semilogy(x,y); 10 5 5
10

subplot(2,2,4); 0 0
10 10
loglog(x,y);
-5 -5
10 10
-2 0 2
0 5 10 15 20 10 10 10
Bar charts
 Bar graphs are useful for reporting data.
 x = [1,3,8,5,4,6]; Horizontal
Vertical Bar
BarGraph
Graphofofx-data
x-data
8

 bar(x); generates 7
6

a vertical bar graph. 6


5

 barh(x); generates 5
4

a horizontal bar graph. 4

3
3

2
2

1
1

0
0 11 2 2 3 3 4 4 5 5 6 67 8
Pie charts
 Pie charts are another useful way of
reporting data.
 pie(x);
Pie Chart of x-data
4%

8/(1+3+8+5+4+6) ≈ 30%
11%
22%

(cyan section)
5/(1+3+8+5+4+6) ≈ 19%
(yellow section)
etc.
30% 15%

19%
Curve fitting
 The simplest way to fit a set of 2D data is
a straight line.
 Linear regression is a method of fitting
data with a straight line.
 Linear regression minimizes the squared
distance between data points and the
equation modeling the data points. This
prevents positive and negative “errors”
from canceling.
Linear approximation by hand
x = [0,1,2,3,4,5]
y = [15,10,9,6,2,0]
 slope ≈ (y2-y1)/(x2-x1) = (0-15) / (5-0) = -
3
 Crosses y axis at 15 (note the point (0,15)
in our data)
 yhand = -3x + 15
 sum_of_squares = sum((y - yhand).^2) = 5
polyfit function
 The polyfit function takes (x, y) data, and
the degree n of a polynomial as input. It
returns the coefficients of the polynomial
of degree n that best fits the data.
 Using our data:
polyfit(x,y,1) ans = [-2.9143 14.2857]
 So, yLR = -2.9143x + 14.2857
 sum_of_squares2 = sum((yLR - y).^2) =
3.3714
Best Fit Comparison
Best Fit by Hand Best Fit by Linear Regression
16 16

14 14

12 12

10 10

8 8

6 6

4 4

2 2

0 0

-2 -2
-2 0 2 4 6 -2 0 2 4 6
Polynomial regression
 Polynomial regression is used to fit a set
of data with a polynomial.
 The polyfit function can be used to find
the best fit polynomial of a specified
degree; the result is the coefficients.
 Warning: Increasing the degree of the best fit
polynomial can create mathematical models that
ay fit the data better, but care must be taken in
your interpretation of the result.
polyval function
 polyfit returns the coefficients of a
polynomial that best fits the data.
 To evaluate the polynomial at any value of
x, use the polyval function.
 polyval requires two inputs: the array of
coefficients and the array of x-values at
the locations the polynomial is to be
evaluated.
Example using polyval
 Referring to the data from this lecture that
we used from the polyfit example:
coef = polyfit(x,y,1)
coef = [-2.9143 14.2857]
fitted_data = polyval(coef,x);

 Now try plotting fitted_data vs. x and yLR


vs. x. Identical graphs are generated.
Exercises
1. Use subplots to create normal and polar plots
of y = cos(x) + sin(x) in the same figure
window for –π ≤ x ≤ π.
2. Use polyfit to find a 3rd degree polynomial
to fit the following set of data:
x = -5:1:4
y = [-506.6, -262.88, -99.43, -36.78, 6.2, 7.11,
16.6, 51, 183, 427.97].
 Plot the best fit curve and the data points on the
same figure.
Summary
 Example usage subplots
 Other 2-D plots: polar and logarithmic
 Bar charts and pie charts
 Curve fitting
– Linear regression concept
– polyfit function
– polyval function

You might also like