Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear...
Transcript of Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear...
![Page 1: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/1.jpg)
Introduction to
MATLABInstructor:
Azeem Iqbal Lab Instructor
Centre for Experimental Physics Education (CEPE)
Lahore University of Management Sciences
(LUMS)
Lahore, Pakistan
Physlab | www.physlab.org
1
![Page 2: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/2.jpg)
Contents
Introduction to MATLAB
Layout
Basic arithmetic operations
Creating vectors and matrices
Matrix arithmetic
Data manipulation
Introduction to “for” Loops
Graphs and plotting
Introduction to plotting
Multiple plots
Resolution of graph
Curve fitting
Least square curve fitting of linear data
Physlab | www.physlab.org
2
![Page 3: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/3.jpg)
Introduction to MATLAB
MATLAB stands for Matrix Laboratory.
Developed by Cleve Moler from the University of New Mexico in
the late 1970s.
It is a high-performance language for technical computing and
integrates computation, visualization, and programming
environment.
It has sophisticated data structures, contains built-in editing and
debugging tools, and supports object-oriented programming.
MATLAB was first adopted by researchers and practitioners in
control engineering.
It is now also used in education, in particular the teaching of
linear algebra, numerical analysis, and is popular amongst
scientists involved in image processing.
Physlab | www.physlab.org
3
![Page 4: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/4.jpg)
Introduction to MATLABLayout
4
Physlab | www.physlab.org
![Page 5: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/5.jpg)
Layout Physlab | www.physlab.org
5
Working
Directory
.m files
Command
Window Workspace
(Variables
List)
Toolbar
Menubar
Editor
Window
![Page 6: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/6.jpg)
Introduction to MATLABBasic arithmetic operators
6
Physlab | www.physlab.org
![Page 7: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/7.jpg)
Basic Arithmetics
a = 5;
b = 4;
Summation sum = a + b;
Difference diff = a – b;
Product prod = a * b;
Division div = a / b;
Exponent exp = a^2;
Square root sqrt = sqrt(b);
Physlab | www.physlab.org
7
![Page 8: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/8.jpg)
Basic Arithmetics
Concept of precedence:
P E M D A S
Order of
precedence
P = Parentheses 1
E = Exponents 2
M = Multiplication 3
D = Division 4
A = Addition 5
S = Subtraction 6
Physlab | www.physlab.org
8
Which ever
comes first in left
to right order of
equation
![Page 9: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/9.jpg)
Basic Arithmetics
Solve:
6 ÷ 2 (2 + 1)
What is the answer
1 or 9?
The correct answer is “9”
Physlab | www.physlab.org
9
![Page 10: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/10.jpg)
Introduction to MATLABCreating vectors and matrices
10
Physlab | www.physlab.org
![Page 11: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/11.jpg)
Vector and Matrices
Vector
x = [1 2 5 1] Row or Column matrix
Matrix x = [1 2 3 ; 5 1 4 ; 3 2 -1]
x =
1 2 3
5 1 4
3 2 -1
Transpose y = x’ y = 1
2
5
Physlab | www.physlab.org
11
![Page 12: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/12.jpg)
Vector and Matrices
evenlist = [2 4 6 8 10 12 14 16 18];
evenlist2 = 2:2:18;
evenlist3 = [2; 4; 6; 8; 10; 12; 14; 16; 18];
2
4
6
8
10
12
14
16
18
evenlist = evenlist3’;
Physlab | www.physlab.org
12
![Page 13: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/13.jpg)
Matrix arithmetic
a = [2 4 6; 1 3 5; 7 9 11];
2 4 6
1 3 5
7 9 11
Type size(a) to check the size of matrix, in this case it is 3x3 matrix
You may want to extract a few values using:a(2,2)
a(2,:)
a(:,2)
Physlab | www.physlab.org
13
![Page 14: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/14.jpg)
Dot and scalar product
5 8 9 4 6 7
a = 2 4 6 b = 2 1 3
1 3 5 5 3 8
a * b Matrix Multiplication
a .* b Element by Element Multiplication
Physlab | www.physlab.org
14
![Page 15: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/15.jpg)
Data manipulation using “for” Loops
Basic structure:
for (condition)statements
end
Generate the first 15 Fibonacci numbers
f=[1 1];
for k=1:15
f(k+2) = f(k+1) + f(k);
end
Physlab | www.physlab.org
15
![Page 16: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/16.jpg)
Physlab | www.physlab.org
16
Let’s practice! Solve the first exercise …
![Page 17: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/17.jpg)
Contents
Introduction to MATLAB
Layout
Basic arithmetic operations
Creating vectors and matrices
Matrix arithmetic
Extracting elements from matrices
Data manipulation
Introduction to “for” Loops
Graphs and plotting
Introduction to plotting
Multiple plots
Resolution of graph
Curve fitting
Least square curve fitting of linear data
Fitting and plotting with error bars
Physlab | www.physlab.org
17
![Page 18: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/18.jpg)
Introduction to MATLABGraphs and plotting
18
Physlab | www.physlab.org
![Page 19: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/19.jpg)
Graphs and plotting
1. They act as visual aids indicating how
one quantity varies when the other
quantity is changed, often revealing
subtle relationships.
2. Determine slopes and intercepts
3. Compare theoretical predictions and
experimentally observed data.
Physlab | www.physlab.org
19
![Page 20: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/20.jpg)
Graphs and plotting
Physlab | www.physlab.org
20
![Page 21: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/21.jpg)
Sample Dataset Physlab | www.physlab.org
21
Sr# Time (s) Mass (g)
1 0.34 121.4
2 0.74 121.4
3 1.13 121.3
4 1.52 121.2
5 1.92 121.2
6 2.31 121.1
7 2.70 121.1
8 3.10 121.0
9 3.49 121.0
… … …225 89.94 102.8
226 90.33 102.8
![Page 22: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/22.jpg)
Physlab | www.physlab.org
22
![Page 23: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/23.jpg)
Introduction to plotting
Matlab can generate plots of a number of types e.g. Linear plots
Line plots
Logarithmic plots
Bar graphs
Three-dimensional plots
In lab we will primarily work with two-dimensional plots by creating two “vectors” or an independent and dependent quantity.
It is customary to plot independent variable (the “cause”) on horizontal axis and dependent variable (the “effect”) on the vertical axis.
Physlab | www.physlab.org
23
![Page 24: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/24.jpg)
Typical models that fit typical
experimental dataPhyslab | www.physlab.org
24
![Page 25: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/25.jpg)
Physlab | www.physlab.org
25
y = x y = -3x
y = 3.0x – 6.7 y = 3.0x – 6.7
Linear
![Page 26: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/26.jpg)
Physlab | www.physlab.org
26Y = 3x2 - 10
Quadratic
![Page 27: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/27.jpg)
Physlab | www.physlab.org
27
![Page 28: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/28.jpg)
Linearization Physlab | www.physlab.org
28
y= 5e-x
ln(y) = ln(5) - x
![Page 29: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/29.jpg)
Physlab | www.physlab.org
29Superposing Graphs
y = -x
y = -x + 0.005x3
y = -x +0.5x3
y = -x
y = -x + 0.005x3
y = -x +0.5x3
![Page 30: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/30.jpg)
ExamplePhyslab | www.physlab.org
30
Let's consider an example of a stretched string fixed at one end to
a rigid support, is strung over a pulley and a weight of 1.2 kg is
attached at the other end. The string can be set under vibrations
using a mechanical oscillator (woofer) connected to the signal
generator.
The relation of angular velocity (w) with the wave vector (k) is
called the dispersion relation and given by,
![Page 31: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/31.jpg)
Physlab | www.physlab.org
31
y = m x +c
![Page 32: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/32.jpg)
Example
Plot the following experimental data:
Commands:
n=[1 2 3 4 5];
f=[20.82 41.82 61.32 82.32 104.1];
plot(n,f)
xlabel(`Resonance mode (n)')
ylabel(`Frequency (Hz))')
title('Dispersion relation for a bare string')
Physlab | www.physlab.org
32
Resonance mode (n) 1 2 3 4 5
Frequency (Hz) 20.82 41.82 61.32 82.32 104.1
![Page 33: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/33.jpg)
Physlab | www.physlab.org
33
![Page 34: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/34.jpg)
Physlab | www.physlab.org
34
![Page 35: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/35.jpg)
Possible Ways of Plotting
(a) Not acceptable
![Page 36: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/36.jpg)
(b) Barely acceptable
![Page 37: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/37.jpg)
(c) Better
![Page 38: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/38.jpg)
(d) Acceptable, good in all respects
![Page 39: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/39.jpg)
(e) Unnecessary detail or embellishment
![Page 40: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/40.jpg)
(f) Axes too long
![Page 41: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/41.jpg)
(g) Axes tick marks are
inconsistent and clumsy
![Page 42: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/42.jpg)
Resolution of Graph
Suppose we have a sine curve, sampled at interval of 1s for a
duration of 10s, it means there are eleven data points contained
within the sampled duration.
Physlab | www.physlab.org
42
![Page 43: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/43.jpg)
Resolution of Graph
We know from experience that a plot of the sine function should
be smooth.
Why is this discrepancy?
The reason is that we have not sampled enough points. So, we
decrease the sampling interval to 0.1 s and hence, increasing the
number of samples to 101, we recover a smooth sine curve.
Physlab | www.physlab.org
43
![Page 44: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/44.jpg)
Resolution of GraphPhyslab | www.physlab.org
44
![Page 45: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/45.jpg)
Multiple plotting
Let’s define input vectors:
x=0:0.1:4*pi;
y=2*cos(x);
y1=2*sin(x);
figure; plot(x,y,`r-d')
hold on
plot(x,y1,`b-*')
Physlab | www.physlab.org
45
![Page 46: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/46.jpg)
Physlab | www.physlab.org
46
Let’s practice! Solve the second exercise …
![Page 47: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/47.jpg)
Introduction to MATLABCurve Fitting
47
Physlab | www.physlab.org
![Page 48: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/48.jpg)
Contents
Introduction to MATLAB
Layout
Basic arithmetic operations
Creating vectors and matrices
Matrix arithmetic
Extracting elements from matrices
Data manipulation
Introduction to “for” Loops
Graphs and plotting
Introduction to plotting
Multiple plots
Resolution of graph
Curve fitting
Least square curve fitting of data
Fitting and plotting with error bars
Physlab | www.physlab.org
48
![Page 49: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/49.jpg)
ExamplePhyslab | www.physlab.org
49
A student wants to check the resistance of a resistor by
measuring voltage (V) across it and the resulting current
(I) through it and then calculating the resistance through
Ohm’s Law.
Using slope of the relation
𝑉 = 𝑅 𝐼
y = m x + c
![Page 50: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/50.jpg)
Curve Fitting Example
Plot the following experimental data:
Commands:
v=[11.2 13.4 15.1 17.7 19.3];
a=[4.67 5.46 6.28 7.22 8.30 ];
plot(a,v,’o’)
xlabel(`Current (A)')
ylabel(`Voltage (V))')
title(‘Finding the value of a resistance through Ohm’s Law')
Physlab | www.physlab.org
50
Voltage (V) 11.2 13.4 15.1 17.7 19.3
Current (A) 4.67 5.46 6.28 7.22 8.30
![Page 51: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/51.jpg)
Physlab | www.physlab.org
51
![Page 52: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/52.jpg)
Physlab | www.physlab.org
52
![Page 53: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/53.jpg)
Physlab | www.physlab.org
53
![Page 54: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/54.jpg)
Curve Fitting
via lsqcurvefit command
Physlab | www.physlab.org
54
![Page 55: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/55.jpg)
Physlab | www.physlab.org
55
![Page 56: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/56.jpg)
1. Create a function file
Physlab | www.physlab.org
56
![Page 57: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/57.jpg)
2. Call the function via lsqcurvefit
Physlab | www.physlab.org
57
![Page 58: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/58.jpg)
3. The function outputs the
optimized values of parameters
Physlab | www.physlab.org
58
![Page 59: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/59.jpg)
Physlab | www.physlab.org
59
![Page 60: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/60.jpg)
Uncertainties in Voltage
and Current
Voltage (V)Uncertainty due
to resolution (Us)
Uncertainty due
to rating (Ur)
Total Uncertainty
in Voltage (Uv)
11.2 0.03 0.11 0.12
13.4 0.03 0.13 0.14
15.1 0.03 0.15 0.15
17.7 0.03 0.18 0.18
19.3 0.03 0.19 0.20
Current (A)Uncertainty due
to resolution (Us)
Uncertainty due
to rating (Ur)
Total Uncertainty
in Voltage (Iv)
4.67 0.003 0.047 0.047
5.46 0.003 0.055 0.055
6.28 0.003 0.063 0.063
7.22 0.003 0.072 0.072
8.30 0.003 0.083 0.083
Physlab | www.physlab.org
60
![Page 61: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/61.jpg)
Physlab | www.physlab.org
61
![Page 62: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/62.jpg)
What if we want to plot
uncertainties for both axis?
We’ll use the “xyerrorbar” function file
available on Physlab website.
Physlab | www.physlab.org
62
Syntax:
xyerrorbar(x_vector, y_vector, u_x, u_y, ’o’)
![Page 63: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/63.jpg)
Physlab | www.physlab.org
63
![Page 64: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/64.jpg)
Linearizing Plots: Cooling Objects
/
ln( ) ln( )
t bT Ae
tT A
b
![Page 65: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/65.jpg)
/
ln( ) ln( )
t bT Ae
tT A
b
![Page 66: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/66.jpg)
Light Bulb (Power Law)
4
elec
4 4
elec
log( ) log( ) 4 log( )
P AT
T R
P AR CR
P C R
![Page 67: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/67.jpg)
log( ) log( ) 4 log( )P C R
Slope
0.75
![Page 68: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/68.jpg)
When does a model breakdown?
Model 1
Model 2
![Page 69: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/69.jpg)
1
2
Model 1
Model 2
![Page 70: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/70.jpg)
Sample Work from Students of PHY100/200
![Page 71: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/71.jpg)
![Page 72: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/72.jpg)
![Page 73: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/73.jpg)
![Page 74: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/74.jpg)
![Page 75: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/75.jpg)
![Page 76: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/76.jpg)
![Page 77: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/77.jpg)
![Page 78: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/78.jpg)
![Page 79: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/79.jpg)
![Page 80: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/80.jpg)
![Page 81: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/81.jpg)
![Page 82: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/82.jpg)
![Page 83: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/83.jpg)
![Page 84: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/84.jpg)
![Page 85: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/85.jpg)
![Page 86: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/86.jpg)
Physlab | www.physlab.org
86
Let’s practice! Solve last exercise of this session…
![Page 87: Introduction to MATLAB - PhysLab · Matlab can generate plots of a number of types e.g. Linear plots Line plots Logarithmic plots Bar graphs Three-dimensional plots In lab we will](https://reader035.fdocuments.net/reader035/viewer/2022062302/5ed31e21e87f8f56a1275a51/html5/thumbnails/87.jpg)
Introduction to
MATLABEnd of session!
87
Physlab | www.physlab.org