announcements saving figures as images ... -...

26
Physics 241 – Class #9 – Outline Announcements Saving figures as images Plotting Handle graphics Numerical derivatives and integrals

Upload: others

Post on 29-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Physics 241 – Class #9 – Outline

Announcements

Saving figures as imagesPlottingHandle graphicsNumerical derivatives and integrals

Page 2: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

AnnouncementsOffice hours – Have cold … make appointment

Study groups – need help?

HW5 – Read  Ch. 4 and do first 8 end­of­chapter problems. Data files needed to download for problems 4,8,9 are in the HW5 subdirectory on the course website.

Page 3: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Test●Review 10/3●10/5/2012 … in class and lab.●Covers HW 1­­5●Problems like homework problems●Write pseudocode without a computer.●Identify and correct incorrect code.●Refine pseudocode to be roughly correct.●Hand in the paper, then code the solutions on the computer.

●Drop date is 10/31/2012.

Page 4: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Saving figures as images

There are two ways to share data-plots (other than printing them).

1) Make a .fig file. A .fig file contains both the plot and the data used to make the plot. The advantage of it is that it can be edited and customized further by you or the recipient. The disadvantage is that they cannot see the file without loading Matlab, and the fact that it CAN be changed makes it not good for archives and publications.

2) Make a .jpeg, bmp, png, eps (an image). Method 2 is what I am requesting in this course.

>> print(1,'dpng')>> print(3,'deps')>> print(1,'-djpeg','homework2_2.jpeg')>> print(1,'-dbmp','-r300','homework2_2.bmp')

Page 5: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Advanced Plotting

%a20030725_ZvsTfancy.m -- Loads and plots a balloon trajectory.%Demonstrates changing fonts. %("listfonts" command gives you font options)a=load('bflight1_20030725_mod.pos'); time=a(:,4); alt=a(:,3); %Get 3rd and 4th columnsplot(time,alt,'r.',time,alt,'b-','MarkerSize',22,'LineWidth',2);%Plot “alt” with red dots and also with a blue line%Make the dots and line bigger than “default”xlabel('Time (s)','Fontname','Times','FontSize',18);ylabel('Altitude (m)','FontName','Times','FontSize',18);%Label the axes with nice readable fonts.title('Telemetry test flight','FontName','Times','FontSize',22);text(400,1700,'July 25, 2003','FontSize',16)%Add explanatory texta=gca; set(a,'FontName','Arial','FontSize',16)%Change the text size of the tick marks

Page 6: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Handle graphics

>> f=gcf  %Gets “handle” for current figure>> get(f) %Gets properties of current figure>> a=gca %Gets “handle” for current axes>> get(a) %Gets properties of current axes>> set(a,'XTick',[1 2 3 5]') %Sets tick marks to [1 2 3 5]Can set any property that you have “gotten”. >> xlab=get(a,'Xlabel') % Gets Xlabel properties on current figureXlabel is child of current axes which are child of current figure.  Figure is parent of axes.

Page 7: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Handle graphics>> a=gcaa=173.0024>> get(a)

ColorOrder = [ (7 by 3) double array]FontAngle = normalFontName = HelveticaFontSize = [10]FontUnits = pointsFontWeight = normalGridLineStyle = :LineStyleOrder = -LineWidth = [0.5]TickLength = [0.01 0.025]TickDir = inTitle = [178.002]XGrid = offXLabel = [175.002]XAxisLocation = bottomXLim = [1000 10000]XTickLabel = 0

2000 4000 6000 8000 10000

Parent = [1]

>> b=get(a,'XLabel')

b =

175.0024

>> get(b)BackgroundColor = noneColor = [0 0 0]Extent = [ (1 by 4) double array]FontAngle = normalFontName = HelveticaFontSize = [10]FontUnits = pointsFontWeight = normalHorizontalAlignment = centerPosition = [5489.63 -1973.68 1.00011]Rotation = [0]Units = dataInterpreter = texVerticalAlignment = cap

Parent = [173.002]Visible = on

Page 8: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Plotting under program control vs. using GUI.

Matlab has a GUI with every figure. It has very useful features like “zoom” and a data cursor.

You might want a GUI while doing exploratory data analysis.

The GUI allows customizing fonts, adding labels, changing Markers.Everything you can do in the GUI you can do under program control.

Programmed figures have the advantage of being easily reproducible. You can get the same look for an entire presentation or publication without individually pointing and clicking every detail on every figure.

More importantly, the script that generates the plot records what you did.Did you filter the data? Omit bad data points?

Page 9: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Numerical derivative – forward derivative

f '(t)=limΔ t →0

f ( t+Δ t)−f ( t)

Δ t¿

Math

Matlab element by element

Spot the bug! There are three in code above ….Advantages: For first point of data set, there is no zeroth point.

len=length(f);D=zeros(1,len);for k=1:len

D(k)=(f(k+1)-f(k))/deltat;deltat=t(k+1)-t(k); k=k+1;

end

Page 10: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers
Page 11: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Numerical derivative – backward derivative

f '(t)= limΔ t →0

f ( t)−f (t−Δ t)Δ t

deltay=diff(y) deltat=diff(t)

Math

Matlab element by element

Array oriented

Advantages: For last point of data set, there is no next point. Also, for real-time data when you don't know the future.

deltay=y(k)-y(k-1)deltat=t(k)-t(k-1)yprime(k)=deltay/deltat

Page 12: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Numerical derivatives – central derivative

f '(t)= limΔ t →0

f ( t+Δ t)−f ( t−Δ t)2Δ t

deltay=y(k+1)-y(k-1)deltat=t(k+1)-t(k-1)yprime(k)=deltay/deltat

Math

Matlab

Advantages: Less “noisy” than other formulae.Symmetrical

Page 13: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Central derivative

Forward derivative

Page 14: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Errors in Numerical derivatives

f ( t+Δ t )=f ( t)+f '( t)Δ t+f ' '(t)Δ t2

2!+f ' ' '(t)

Δ t3

3!+...

Taylor Series

f '(t)=f (t+Δ t)−f (t)

Δ t−f ' '(t)

Δ t2!

−f ' ' '( t)Δ t2

3!+...

Note: Error decreases at step-size decreases

Page 15: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Left Riemann Sum

Left Riemann sum. Overestimates decreasing functions.

Page 16: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers
Page 17: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Right Riemann Sum

Right Riemann sum. Overestimates increasing functions.

Page 18: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Trapezoid Sum

Trapezoid Sum = Left Sum + Right Sum2

Page 19: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

You do it ...

Δ t , f (t1), f (t2), f ( t3)... f (t10)Given

Calculate

Using left riemann sum

I(t10)=∫t1

t10

f ( t)dt

Page 20: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

You do it ...

Δ t , f (t1), f (t2), f ( t3)... f (t10)Given

Calculate

Using right riemann sum

I(t10)=∫t1

t10

f ( t)dt

Page 21: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

You do it ...

Δ t , f (t1), f (t2), f ( t3)... f (t10)Given

Calculate

Using trapezoid

I(t10)=∫t1

t10

f ( t)dt

Page 22: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Numerical Integrals

I(t1)= limΔ t →0

∑t=0

t=t1

f (t)Δ t

Math

MatlabLeft Riemann

I(t1)=∫0

t1

f ( t)dt

I=zeros(1,10); I(1)=0for k=1:10

deltat=t(k+1)-t(k); k=k+1I(k)=I(k-1)+y(k)*deltat

end

Page 23: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

MatlabLeftRiemann

MatlabRight Riemann

MatlabTrapezoid?

I=zeros(1,10); I(1)=0for k=1:10

deltat=t(k+1)-t(k); k=k+1I(k)=I(k-1)+y(k)*deltat

end

You do it ...

Page 24: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Errors in Numerical Integrals(graphical approach)

Error≃f '( t)Δ t2

2

Error is missing part of triangle

Page 25: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Errors in Numerical Integrals(graphical approach)

Error≃f ' '(t)Δ t3

4

f ( t+Δ t )=f ( t)+f '( t)Δ t+f ' '(t)Δ t2

2!+...

Page 26: Announcements Saving figures as images ... - kestrel.nmt.edukestrel.nmt.edu/~rsonnenf/phys241/xxx2012assignments/class_exa… · Test Review 10/3 10/5/2012 … in class and lab. Covers

Errors in Numerical Integrals(analytical)

f ( t+Δ t )=f ( t)+f '( t)Δ t+f ' '(t)Δ t2

2!+f ' ' '(t)

Δ t3

3!+...

Taylor Series

Error=f '( t)Δ t2

2+f ' '(t)

Δ t3

2⋅2!+f ' ' '(t)

Δ t4

2⋅3!+...

Note: Error decreases at step-size decreases