class27 - PHYS 5900 Class 27 (10/30/2009Fri) Zi-Wei Lin...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: PHYS 5900 Class 27 (10/30/2009Fri) Zi-Wei Lin Named Optional Arguments Example: Motion of a Particle in One Dimension For a particle in one dimension with any given expression for the acceleration a and initial values of position x0 and velocity v0 , w rite a function to plot the position, velocity and acceleration versus time in the range of time from 0 to tmax and to display a combination plot. Any selection of these plots should be made possible by using options. Before we go to the example, we need to look at the following: FilterRules[ {opt1, opt2, ...}, Options[f] ] returns a list of options from opt1, opt2, ... that are valid options for f (i.e., removes options from opt1, opt2, ... that are not valid options for f). In[1]:= FilterRules @ 8 myAxesLabel-> False, myPlotRange-> True, Axes-> True, Frame-> False < , Options @ Plot DD Out[1]= 8 Axes fi True, Frame fi False < In the above, user-defined options myAxesLabel and myPlotRange are not valid options for Plot[ ]. Now we define the procedure for the example: In[2]:= ClearAll @ motion1DPlot D In[3]:= H * name the options and specify their default values * L Options @ motion1DPlot D = 8 positionPlot-> True, velocityPlot-> True, accelerationPlot-> True, combinationPlot-> True, positionLabel-> 8 "t H s L ", "x H m L " < , velocityLabel-> 8 "t H s L ", "v H m s L " < , accelerationLabel-> 8 "t H s L ", "a H m s^2 L " < , combinationLabel-> 8 "t H s L ", None << ; The function takes 4 arguments ( a, x0, v0, tmax ) and some options: In[4]:= motion1DPlot @ a_, x0_, v0_, tmax_ , opts___Rule D : = Module @8 H * declare local variables * L sol, combinationCurves = 8< , plotx, plotv, plota, H * determine option values and assign them as initial values to local variables * L position = positionPlot . 8 opts < . Options @ motion1DPlot D , velocity = velocityPlot . 8 opts < . Options @ motion1DPlot D , acceleration = accelerationPlot . 8 opts < . Options @ motion1DPlot D , combination = combinationPlot . 8 opts < . Options @ motion1DPlot D , positionLabelValue = positionLabel . 8 opts < . Options @ motion1DPlot D , velocityLabelValue = velocityLabel . 8 opts < . Options @ motion1DPlot D , accelerationLabelValue = accelerationLabel . 8 opts < . Options @ motion1DPlot D , combinationLabelValue = combinationLabel . 8 opts < . Options @ motion1DPlot D , H * select valid options for Plot and Show and assign them as initial values to local variables * L optPlot = FilterRules @8 opts < , Options @ Plot DD , optShow = FilterRules @8 opts < , Options @ Graphics DD< , H * solve the equation of motion numerically * L sol = NDSolve @8 x ' ' @ t D == a, x @ D == x0, x ' @ D == v0 < , x, 8 t, 0, tmax <D ; H * plot position vs. time * L If @ position, plotx = Plot @ Evaluate @ x @ t D . sol D , 8 t, 0, tmax < , AxesLabel-> positionLabelValue , Evaluate @ optPlot DD ; Print @ plotx D ; AppendTo @ combinationCurves , plotx DD ; H * plot velocity vs. time * L If @ velocity, plotv = Plot @ Evaluate @ x ' @ t D . sol D , 8...
View Full Document

This note was uploaded on 04/25/2010 for the course PHYS 5900 taught by Professor Lin during the Fall '09 term at East Carolina University .

Page1 / 18

class27 - PHYS 5900 Class 27 (10/30/2009Fri) Zi-Wei Lin...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online