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

Post on 29-May-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Physics 241 – Class #9 – Outline

Announcements

Saving figures as imagesPlottingHandle graphicsNumerical derivatives and integrals

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.

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.

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')

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

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.

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

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?

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

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

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

Central derivative

Forward derivative

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

Left Riemann Sum

Left Riemann sum. Overestimates decreasing functions.

Right Riemann Sum

Right Riemann sum. Overestimates increasing functions.

Trapezoid Sum

Trapezoid Sum = Left Sum + Right Sum2

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

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

You do it ...

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

Calculate

Using trapezoid

I(t10)=∫t1

t10

f ( t)dt

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

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 ...

Errors in Numerical Integrals(graphical approach)

Error≃f '( t)Δ t2

2

Error is missing part of triangle

Errors in Numerical Integrals(graphical approach)

Error≃f ' '(t)Δ t3

4

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

2!+...

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

top related