ees lecture 6 a lecture on the use of ees

Upload: ali

Post on 29-Mar-2016

266 views

Category:

Documents


5 download

DESCRIPTION

a lecture on the use of EES. These lectures well written and contributed by Professor Hannes van der Walt at the Swinburne University of Technology in Australia.

TRANSCRIPT

  • ContentsEES advanced tutorial (2 Lectures)Min/Max analysis (Lect 5)Uncertainty analysisCoding conventions (Lect 6)String functions, constantsDiagram windowFunctions, procedures, modules & subprogramsLoopsCurvefittingTableRun#, TableValue, LookupDisk input and output, formatted outputOther important features

  • Coding ConventionsCapitalisation: Use capitals or mixed case for directives such as $UnitSystem, $IfDef (or if you really insist use $IFDEF)Use lowercase (mostly) for variable namesUse mixed case for user defined function names and logical statements such as If, Do, Repeat-Until, DuplicateMost importantly however, be consistent!Spaces:Use spaces liberally either side of operators +, -, /, *, =Indent and align lines inside If, Do, Duplicate and inside functions

  • String FunctionsSee the Help file for String Functions: Concat$Date$EesFileDir$LowerCase$String$StringLenStringPosStringValTime$UpperCase$

  • StringsEES handles strings quite elegantly:Strings are defined between single quotesNames can be defined for strings and the names can be used in functions that need themStrings can be manipulated through the string functions listed beforeCheck out the following example file: Lecture 6.1 StringHandling.eesNote how strings can be supplied from a parametric table and thus vary with each run.0:15

  • StringsString usage example:"String constants can be created. There are also several string handling functions"title$ = 'EES'secondTitle$ = ' is very cool!'combinedTitle1$ = Concat$(title$, ' is cool')combinedTitle2$ = Concat$(title$, secondTitle$)"Strings are also used in several property functions"density_water_1 = Density('water', T=100, P = 100)

    T1 = 100 [C]P1 = 95 [kPa]fluid$ = 'water'density_water_2 = Density(fluid$, T=T1, P = P1)0:20

  • Diagram WindowThe Diagram Window (Ctrl+D) provided EES with a Graphical User Interface that can contain:input fieldsoutput fieldsradio buttonsdrop down boxesGraphic images can by copied from any other graphical package into the EES diagram windowLets reopen the projectile movement example of just before.

  • Diagram WindowAdd input and output fields as shown.Add buttons as shownOf course, when input values are provided in the diagram window, they cannot be provided in the equations window or the parametric table!"Define initial velocity"$IfNot (ParametricTable or MinMax) Apparently one can only have 2 statements in an $If$IfNot DiagramWindowtheta = 45 [deg]Disabled when running Table 1 or Table 2 or the Diagram Window"$EndIf$EndIf

    $IfNot ParametricTable = 'Table 2 - Min/Max Table'$IfNot DiagramWindowu = 30 [m/s]Disabled when running Table 2 or the Diagram Window"$EndIf$EndIfSee Lecture 5.1 - SimpleMinMax.EESSee diagram window examples in the Examples menu

  • FunctionsEES solves the main body of the program as a simultaneous set of equations, and not sequential. That implies that logic cannot be accommodated in the main body and has to be added to a function.For the same reason, loops (apart from Duplicate) cannot be added to the main body and has to be implemented in a function.Functions that the user defines are no different from those chosen from the Options > Function Info menu:

    a = cos(theta)b = sqrt(value)Lecture 6.2a - Functions.EES Lecture 6.2b - Procedures.EES

  • FunctionsFunctions must be declared before (above) the main body of an EES program:

    $TabStops 0.5 cm

    Function Minimum(x, y)if (x < y) then Minimum = xelse Minimum = yendifEnd

    a = 12The main body of the EES program starts hereb = -10x_min = Minimum(a, b)

  • FunctionsLogic can be nested to provide more complex behaviour, for example:

    $TabStops 0.5 cm

    Function Smallest(x, y, z)if ((x < y) and (x < z)) then s = xWe can assign the result to a temporary variable for nowelse if ((y < x) and (y < z)) then s = y else s = z endifendifSmallest = s; But eventually the value is to be returned here! End

    a = 12b = -12c = 22x_smallest = Smallest(a, b, c)

  • ProceduresEES Procedures are very much like EES Functions, except that they allow multiple inputs and outputs. The format of a Procedure is:

    $TabStops 0.5 cm

    Procedure CalcSquareOfTwo(x, y: x_sq, y_sq) x_sq = x * x y_sq = y * yEnd

    a = 12b = -12Call CalcSquareOfTwo(a, b: a_sq, b_sq)

  • Modules & SubprogramsModules and Subprograms can be considered to be stand-alone EES programs that can be called from the main EES program. The format of a Module / Subprogram is similar to that for an internal Procedure.See the Help file as well the Examples. See eesysol15.pdf (www.fchart.com)

  • LoopsLoops are implemented in a function or a procedure as follows: (Lecture 6.3 - Loops.EES)

    $TabStops 0.5 cm

    Function SumOfArray(n, x[1..n])sum = 0i = 1Repeat sum = sum + x[i] i = i + 1Until (i > n)SumOfArray = sumEnd

    c[1] = 12c[2] = 5c[3] = 27c[4] = 9c[5] = 16size = 5sum = SumOfArray(size, c[1..size])See the Help file for: Repeat-UntilLogical operatorsIf(a, b, c, d)If$(a, b, c, d)Logical Operators:< (less than)> (greater than)= (equal)= (greater than or equal)(not equal)and (logical and)or (logical or)Note the shorthand array notation!

  • Curve FittingEES can do some pretty fancy curve fitting for you. (Lecture 6.4a - Curve Fitting.EES)Reopen the linear regression example Lecture 5.2 - Linear regression.EES. Use the Duplicate to just read in the data. Now fit the following curves using the Plots > Curve Fit menu:4th order polynomialExponentialUser specified with the following equation:Y[i] = a0*cos(x[i]) + a1*sin(x[i]) + a2/cos(x[i]) + a3/sin(x[i])

  • Curve FittingEES also provide the curve fit functionality programmatically. For example, after fitting a curve, you want to use the coefficients in your EES program. The syntax is:

    Call CURVEFIT1D(FitType, X[1..n], Y[1..n]: a[1..m], RMS, Bias, R|2, a_stderr[1..m])

    FitType is a string constant or string variable that can be any of the following:

    'LINEAR' {fit is Y[i]=a[0]+a[1]*X[i]}'POLYNOMIAL1' or 'POLY1'{same as linear}'POLYNOMIAL2' or 'POLY2'{fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2}'POLYNOMIAL3' or 'POLY3'{fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2+a[3]*X[i]^3}'POLYNOMIAL4' or 'POLY4'{fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2+a[3]*X[i]^3+a[4]*X[i]^4}'POLYNOMIAL5' or 'POLY5'{fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2+a[3]*X[i]^3+a[4]*X[i]^4+a[5]*X[i]^5}

    'POLYNOMIAL6' or 'POLY6'{fit is Y[i]=a[0]+a[1]*X[i]+a[2]*X[i]^2+a[3]*X[i]^3+a[4]*X[i]^4+a[5]XY[i]^5+a[6]*X[i]^6}'EXPONENTIAL' or 'EXP'{fit is Y[i]=a[0]*exp(a[1]*X[i])}'POWER'{fit is Y[i]=a[0]*X[i]^a[1]}'LOGARITHMIC' or 'LOG'{fit is Y[i]=a[0]+a[1]*ln(X[i])}

  • Curve Fitting ExamplesFollowing is an example on using the programatical curve fitting function (See CURVEFIT1D in the Help file): (Lecture 6.4b - Curve Fitting Function.EES)

    $TabStops 0.5 cmn = 100n is the number of data pointsm is other order of the polynomial from the tableF$ = Concat$('POLY', String$(m))call CurveFit1D(F$, X[1..n], Y[1..n] : a[0..m], rms, bias, R|2, a_err[0..m])

    Duplicate i = 1, nX[i] = ifake data for the exampleY[i] = i + 2.5*X[i]^2 + 0.002*X[i]^4 + random(0,1)*X[i]predicted value of Y from the polynomial fitsYp[i]=a[0] + Sum(a[j]*X[i]^j, j = 1, m)End

  • TableRun#, TableValue, LookupWhen performing a parametric table run, a Min/Max table run or an Uncertainty Propagation Table run, the TableRun# constant gives the run number of the current run and can be used in the main program.TableValue can be used to read a value from a parametric table.Lookup is used to read and write to a lookup table. Writing can only be done in a function or procedure.

  • TableRun#, TableValue, Lookup"Get the run number of the current parametric table run"row = TableRun#

    "Read from parametric table 'Table 1' and then write to 'Table 2'"value_read = TableValue('Table 1', row, ColName')

    "Values can be stored in a lookup table. This must be done in a function or procedure"Function SetLookupTable(row, value)Lookup('Lookup 1', row, ColumnName') = value"This is not really necessary but functions has to return something"SetLookupTable = value End

    Lecture 6.5 - TableRun and TableValue.EES

  • Data Output to Disk$ExportThe $Export directive provides a simple way of writing selected variables to an ASCII file. This file can then be read by a $Import directive, the Open Lookup Table command in EES or by another application, such as a spreadsheet program. If the filename extension is .CSV (comma-separated values), the data will be written as a CSV ASCII text file that is easily recognized by spreadsheet applications and by the $Import directive.

  • Data Output to DiskIn this format, each value is separated by a list separator character. If the filename extension is .TXT, EES will assume that the data should be written with header information in the EES Lookup file format that can be read directly by the Open Lookup Table command. The header information will include the name of the variable, its units, and its display format.

  • Data Output to DiskThe format of the $Export Directive is

    $Export /A /F /C 'FileName', Var1, Var2, X[1..5], ...

    The /A /F /C are options see the Help system for details

  • Data Input from Disk$ImportThe $Import directive provides a simple way of reading selected variables from an ASCII file. The data could be provided from a file written using the $Export directive or another application. The combination of $Export and $Import directives provides a convenient way to transfer information from one EES program to another or to itself.

  • Data Input from DiskThe format of the $Import Directive is:

    $Import 'FileName', Var1, Var2, X[1..5], S$...

    See the Help system for details. Also see the Examples > Directives > $Import and $Export demonstration

    Also see $SaveTable, $OpenLookup, $SaveLookup

  • Formatted Data OutputThe Print command can be used with the assignment statements in Internal Functions and Procedures to output intermediate results. The format of the Print command is

    Print "filename" x, y, z

  • Formatted Data OutputExampleFunction Test(x, y)t = x + yF$ = 'text.txt'i = 0Repeati = i + 1print F$, i, x+i, y+iUntil (i >= t)Test = tEnd

    result = Test(2, 3)Listing of file Text.txt after running the above program.

    134245356467578

  • Other Important FeaturesAdvanced Features2D & 3D interpolationLookup files (instead of lookup tables!)3D PlotsCreate LaTeX/PDF Report (MikTex - http://miktex.org)ERROR & WARNING proceduresEES File TypesDistributable programsLibrary ManagerAnimation (Also plot animation)$TRACE, Residual Window, $SAVETABLE

  • End of Lecture 60:05

    **