Using F# to solve a standard engineering problem

F# has many interesting features, such as the use of lists, maps, and so forth. There are many blogs and websites dedicated to discussing the computer science side of F#, and for good reason the language supports these types of efforts. However, what about the rest of the computational users, for instance engineering students who are studying physics and want to generalize their homework problems in case they can use them when they are interns or are working after the leave school. After all, the homework that you do does serve a purpose other than getting a grade in class.

Please note that any discussion about using F# in my blogs are simply to illustrate the use of F# and not to make a statement that it is better than any other language or system such as Mathematica or Matlab, in fact F# is designed to work with those products as a friendly kind of language.

To get started, let’s take a look at how one F# can be used a very basic Ohm’s Law problem.

Let's take a look at a simple problem of electrical resistance.

Using Ohm’s law, Voltage= Current divide by Resistance or V=IR. If we know the total resistance and voltage, then current equals voltage divided by total resistance.

Let's use a circuit made up of three resistors that equal 40 ohms, 50 ohms and 60 ohms that are connected to a voltage source  of 3 volts. To determine the current then I would divide the total voltage by the total resistance.

  • The equation would look like: current = voltage/(Resistor1 + Resistor2 + Resistor3).

Let’s write a simple F# program and a C# program to do the calculation:

Both sets of code look pretty much the same, and if we want to use it to solve circuits, we would have to insert the new values every time. In the next blog entry, we will take a look at using Excel for our input system.  It isn't pretty, nor is it practical, but it does give us a basis of a simple application that can be built on over a number of blogs.  Like many academic

F# form of the calculation

(Formatting is correct)

C# form of the calculation

(Formatting is correct)

#light

open System

let v1=3.0

let r1=40.0

let r2=50.0

let r3=60.0

let i1=v1/(r1+r2+r3)

Console.WriteLine(i1)

Console.Read()

using System;

    class SeriesResistance

    {

        static void Main(string[] args)

        {

            double v1 = 3;

            double r1 = 40;

            double r2 = 50;

            double r3 = 60;

            double i1;

            i1 = v1 / (r1 + r2 + r3);

            Console.WriteLine(i1);

            Console.Read();

        }

    }

problems we are starting simple, but since we are talking about real components like resistors that you might buy from Radio Shack or Fry's, there could be variations in values, the battery could vary between 2.50 volts (Ni-Cads) and 3.0 volts (Alkaline), plus the battery will vary with use as well.  How do we model this kind of variations.  How can we improve the input of the resistors, etc. 

If you want to learn more about F# and how to use it to do lists, etc. take a look at the other blogs such as Don Syme, Chris Smith and https://www.hubfs.com. This blog will be utilize for me to figure out how to use F# to solve engineering and scientific problems that non-computer science and computer science students both might be interested in.