Mathematica as an 
Engineering Tool

Description

While Mathematica is an infinitely extensible programmable tool, many of the functions that engineers need on a day-to-day basis are just a few keystrokes away. 
Here are a few examples.

Compiled by: Ben W. Friedman
Wolfram Research, Inc.

Log and Log-Log Graphing

Log Plotting is one of many advanced functions that reside in one of the many external packages  that come with Mathematica.. These packages may be loaded into Mathematica as 
their functionality is needed. They also provide a style guide as you begin to write your own packages.

Set Up

Load the package.

Needs["Graphics`Graphics`"]

Define the function to be graphed.

eqn[x_] := 
      Abs[(100/((((I x))^2) + 2 I x + 100))];

Generate point data to be plotted.

data = Table[eqn[x], {x,  .1, 20,  .4}];

Log Plot of a Function

plot1 = 
      LogPlot[eqn[x], {x, 1, 20}, 
        GridLines -> {Automatic, LogGridMinor}, 
        PlotStyle -> Thickness[0.01]]

Log-Log Plot of a Function

plot2 = 
      LogLogPlot[eqn[x], {x, 1, 20}, 
        GridLines -> {LogGridMinor, LogGridMinor}, 
        PlotStyle -> Thickness[0.01]]

Log List Plot of Data

plot3 = 
      LogListPlot[data, GridLines -> {Automatic, LogGridMinor}, 
        PlotStyle -> PointSize[0.015]]

Log-Log List Plot of Data

plot4 = 
      LogLogListPlot[data, GridLines -> {LogGridMinor, LogGridMinor}, 
        PlotStyle -> PointSize[0.015]]

Combining Plots

We can combine these graphs in one cell by using "GraphicsArray"

Show[GraphicsArray[{{plot1, plot2}, {plot3, plot4}}]]

Curve Fitting

Several types of curve fitting can be done by Mathematica with little or no programming.

Set Up

Here are the data we will work with. In practice, data can be generated in many ways or read from an external file.

data = {{1.25, 55.60}, {1.00, 51.00}, {0.75, 46.84}, {0.50, 
          35.89}, {0.40, 29.21}, {0.30, 19.70}, {0.20, 10.00}, {0.10, 
          2.67}};

For linear curve fitting, we will use the Mathematica Fit function. We will use an implementation of the Levenberg-Marqaurdt nonlinear fit algorithm by Pakka Janhunan to do a nonlinear fit. This package is in the public domain and is available by anonymous ftp from the Mathematica files at ftp.ncsa.uiuc.edu in the directory UNIX/Symbolic/Mathematica/packages/JANHUNAN

This is how you would load the package if you had stored it in a directory named "packages".

Needs["packages`NonlinearFit`"]

Linear Fitting

pointplot = 
      ListPlot[data, 
        PlotStyle -> {PointSize[0.02], RGBColor[1, 0, 0]}]

fittedcurve = Fit[data, {x, (x^2), (E^x), 1}, x]

curveplot = 
      Plot[fittedcurve, {x, 0, 2}, 
        PlotStyle -> RGBColor[0, 0, 1]]

Show[pointplot, curveplot]

Non-linear Fitting

The function gaussian defines the measured energy of a laser beam with a Gaussian spatial profile passing through an aperture of radius r, where xi is the peak energy density. This will be our model.

gaussian[{xi_, r_}, x_] = 
      xi N[π] (r^2) (1 - (E^(-((((x/r)))^2))));

Now we are ready to try to fit the experimental data to the Gaussian profile.

NonlinearFit[data, gaussian[{xi, r}, x], {xi, r}, {x}, 
      ProgressTrace -> True]

gaussianplot = 
      Plot[{gaussian[{xi, r}, x] /. %}, {x, 0, 1.5}, 
        PlotStyle -> RGBColor[0,  .4,  .9]]

Show[gaussianplot, pointplot]

Fourier Transforms

The Fourier Transform and Inverse Fourier Transform are basic engineering tools.

General Example

First,  we generate a 300-item		 list containing a periodic signal with random noise added.

fourierdata = 
      Table[N[Sin[((25 2 π n)/300)] + Random[]], {n, 
          300}];

A First Look at the Data

From this plot, the data look random.

ListPlot[fourierdata, PlotJoined -> True, 
      PlotStyle -> RGBColor[ .5, 0,  .1]]

Apply the Fourier Transform

The Fast Fourier Transform shows a strong peak at 25 (the frequency of the original periodic signal) and another at 300-25.

ListPlot[Abs[Fourier[fourierdata]], PlotJoined -> True, 
      PlotRange -> All, 
      PlotStyle -> RGBColor[1, 0,  .5]]

FET Picture

by Alfy Riddle
 Macalan Consulting
 Author of: Nodal2.0: A package for ciruit analysis in Mathematica

By combining a few of Mathematica's built-in drawing commands, an engineer can build a precise representation of any system. Here is an example from electrical engineering.

Code

fetPict[] := 
      Block[{Rgs, Rds, Cgs, Cgd, Cds, gm, tau}, 
        Show[Graphics[{Line[{{1, 0}, {- .3, 
                    0}, {- .3,  .1}, {- .3, - .1}}], 
              Line[{{-1, 0}, {- .4, 
                    0}, {- .4,  .1}, {- .4, - .1}}], 
              Line[{{ .8, 
                    0}, { .8, - .2}, { .7, - .2}, { .9, - .2}}], 
              Line[{{ .5, - .5}, { .5, 0}}], 
              Line[{{0, - .5}, { .8, - .5}, { .8, - .3}, { .7, - .3}, { .9, - .3}}], 
              Line[{{- .8, 
                    0}, {- .8, - .2}, {- .7, - .2}, {- .9, - .2}}], 
              Line[{{- .8, - .5}, {- .8, - .3}, {- .7, - .3}, {- .9, - .3}}], 
              Line[{{- .8, - .5}, {0, - .5}, {0, - .65}}], 
              Line[{{ .1, - .45}, {0, - .25}, { .1, - .05}, { .2, - .25}, { .1, - .45}}], Line[{{ .1, - .5}, { .1, 0}}], 
              Polygon[{{ .1, - .4}, { .07, - .3}, { .135, - .3}}], Text[1, {- .95,  .05}], Text[2, { .1, - .65}], 
              Text[3, { .95,  .05}], Text[Rgs, {- .5, -0.38}], 
              Text[Rds, {0.35, - .37}], 
              Text[Cgs, {- .93, - .37}], 
              Text[Cgd, {- .15,  .05}], Text[Cds, { .95, - .37}], 
              Text[gm, {- .1, - .17}], 
              Text[tau, {- .1, - .37}], GrayLevel[ .5], 
              Rectangle[{- .7, - .45}, {- .3, - .55}], 
              Rectangle[{ .45, - .1}, { .55, - .4}]}], 
          AspectRatio -> Automatic]]

Graphic

fetPict[]

Laplace Transforms

Set Up

Load the package.

Needs["Calculus`LaplaceTransform`"]

Define  the function to be used.

Clear[g]

g[s] = (1/((s^2) + 2 xi w0 s + (w0^2)))

Example

g[t] = InverseLaplaceTransform[g[s], s, t]

Plot3D[
      Evaluate[g[t] /. w0 -> 1], {t, 0, 
        10}, {xi,  .2,  .9}, PlotPoints -> 30, 
      ColorFunction -> Hue]

Bode Plots

by Art Low,
 The Leading Edge of Domain Systems Research

Bode Plots are an extremely useful function for control and electrical engineers. Basically consisting of two parametric plots, this function is quite straightforward.

The BodePlot Function

\(BodePlot[f_, s_, lo_, hi_, type_] := 
      Block[{W, v, p}, v = 20; p = 10; 
        magplot = 
          ParametricPlot[{Log[10, W], 
              type\ Log[10, 
                  Abs[f /. s -> I\ W]]}, {W, lo, hi}, PlotRange -> All, AxesLabel -> {Log[W], dB}, 
            DisplayFunction -> Identity]; 
        phaseplot = 
          ParametricPlot[{Log[10, 
                W], \(360\ Arg[f /. s -> I\ W]\)\/\(2\ π\)}, {W, lo, hi}, PlotRange -> All, 
            AxesLabel -> {Log[W], Degrees}, 
            DisplayFunction -> Identity]; 
        Show[GraphicsArray[{{magplot}, {phaseplot}}, 
            DisplayFunction -> $DisplayFunction], Frame -> True, 
          PlotLabel -> "Bode Magnitude & Phase Plots"]]\)

Example

BodePlot[((x + 1)/(((x + 3))^2)), x,  .1, 10, v]