Monday, March 21, 2016

What do I need to start coding?

Source code is the human-readable version of our program. A constant across programming languages is that source code is just plain text (the text you type is the only content of the file you create). Yes, you can write whatever program  you want by using the notepad application that is included in your computer. If you want to ease your work as programmer, then you may want to use a more specialized software such as a programmer text editor, or an Integrated Development Environment (IDE).

Source code files (commonly referred to as source files) have many different name extensions depending on the programming  files. For example, files whose name end with ".c" are files with C code, files with ".py" extension are used for Python programs, ".js" files contain Javascript code, and ".m" files are MATLAB scripts. Although they are used for different programming languages, they are all plain text files.

Writing source-code using a rich-text editor or a word processor may not work very well. The file that results after saving a text in one of these editors may contain much more information than your text itself. For example, let's write the words "Hello world!" (and make it bold, with size of 20 points, and blue), visualize it in WordPad, and in a plain text editor (notepad).


Observe how the plain text editor "sees" much more content than just the text we typed. A similar thing would happen with any other word processor.

So what the source-code to machine-code would really see, is the version we observe in the notepad. That version may not make any sense to such translator. Therefore, if we want to really know what we are going to pass, we better use a plain text editor.

Programmer text editors are also plain text editors. They just have some extra features added to them. Maybe the most popular ones are line numbering (displaying the line number at the left of the text), and syntax high-lighting (highlighting the special keywords from our programming language with different colors so the code becomes easier to read and navigate).



Integrated Development Environments (IDEs) are a set of programs that have the common purpose of helping us develop our programs. Some examples are:
 - Visual Studio,
 - Eclipse,
 - Code-Blocks,
 - Net-Beans,
 - Wing IDE,
 - Among many, many others...

All IDEs include a plain text editor. In addition, they may also include a project manager, which is used to navigate through our software project files easier than using a regular file explorer. They may have also an integrated shell for us to input commands or displaying the output of our program. There are infinite kinds of tools that may be included in an IDE, but these are the most common.



At the end, deciding what tool to use depends mostly on how comfortable you feel using it. Some people may prefer regular text editors over IDEs, or vice-versa. Most of the tools are free or have a trial version, so you can check what fits your needs and preferences.

PD: I prefer dark backgrounds as they tend to be more confortable to my eye by minimizing the amount of light that the screen emits.

Tuesday, March 8, 2016

Running Matlab from inside Python

I have been programming in Python since I was working for Intel. I am amazed on how friendly is its syntax and how few lines of code can do many things. Later, it was exciting to know that Mathworks (the company that develops Matlab) decided to make its powerful software to be natively compatible with Python. Now it is possible to combine the easy Python syntax with the mathematical power inside Matlab and exchange data between the two (or at least it has become very easy).

This Matlab Engine for Python is available from Matlab 2014b. To install it just open a command shell (it MUST be run as administrator to work properly), and type:

    cd %MATLAB_PATH%\%MATLAB_VERSION%\extern\engines\python
    %PYTHON_PATH%\python.exe setup.py install

The %MATLAB_PATH% variable points to the directory where we have installed Matlab. %MATLAB_VERSION% is, as it says, the Matlab version to use. And %PYTHON_PATH% is the directory where have Python installed. In my case, these variables have the values:

    %MATLAB_PATH% = C:\Program Files\MATLAB
    %MATLAB_VERSION% = R2016a
    %PYTHON_PATH% = C:\Python27

Actually, without defining variables, in my machine it looks like this:



If the "error" word appears after executing these commands, check out to see what is missing. Most of the times the error log has enough information to know the next step for fixing the issue.

Once the engine has been installed, it is ready to go. Let's now run an example to plot a sine wave in Matlab, from Python. Open a Python shell and write...

    import matlab
    eng = matlab.engine.start_matlab()
    x = eng.linspace(0.0,5.0,1000.0)
    y = eng.sin(x)
    eng.figure(nargout=0)
    eng.hold("on",nargout=0)
    eng.box("on",nargout=0)
    eng.line(x, y, nargout=0)

When finished, you should see a Matlab graph pop out. With this, we have enabled the Matlab Engine for Python.



I highly recommend you to check these examples in Matlab's website:

- Install MATLAB Engine API for Python
Install MATLAB Engine API for Python in Nondefault Locations
Start and Stop MATLAB Engine for Python
Connect Python to Running MATLAB Session
Call MATLAB Functions from Python
Call MATLAB Functions Asynchronously from Python
Call User Script and Function from Python
Redirect Standard Output and Error to Python
Use MATLAB Engine Workspace in Python
Use MATLAB Handle Objects in Python
Use MATLAB Arrays in Python
Sort and Plot MATLAB Data from Python
Get Help for MATLAB Functions from Python

And now, let's have fun with a whole world of possibilities!

What are programming languages?

So we got an algorithm or process we pretend to program. What is next? We have to write it in such a way that the hardware understands it (so it can run it!). Here we have a problem: the human-language is not understandable by machines (not by themselves). We need some kind of an interface between the machine and us: a programming language. In the background, programs are always translated to machine code. Such translation is done an auxiliary program.



There are many programming languages out there: Java, C, Pascal, Fortran, Octave, and so on. However, only few of them are popular (used frequently). However, the popularity of each programming language changes with time. Good news are that, once you learn one of them, learning another becomes easier; the third one even easier, and etcetera. They all follow certain patterns: they all have a way to handle variables, for example.

1: Java; 2: C; 3: C++; 4: Python; 5: C#; 6: &; 7: PHP; 8: Javascript; 9: Ruby; 10: Matlab
The 10 most popular programming languages in 2015, by IEEE Spectrum.

But why don't we program in machine-language directly? The problem with the machine language is that it is so hard to read and write by humans that it becomes unpractical for coding. Most hardware systems only understand specific combinations of ones and zeroes (binary code), and that is what machine language is. Moreover, the machine language changes with the processor model. Then a program that has been written in machine-language may not work on different machines!

Some programs are easier to translate to machine-code, and therefore, they are harder to understand by humans. Such languages are called "low-level" programming languages. On the other hand we have "high-level" languages, that are easy to understand by humans, but more difficult to translate to machine code.

Programs written in low-level languages tend to run faster than programs written in high-level languages. However, high-level programs tend to run independently of what hardware executes it, while the low level programs should be re-translated or re-coded to run on different machines. But at the end, choosing a programming language or another depends on the application and how it gets impacted by the language characteristics.

Monday, February 29, 2016

What is programming?

My firsts contact with programming made me think of it as a very complex task: something for hackers, and black magicians. However, after getting my first programming homework at school, I had to learn the basics. Fortunately, programming is one of the top topics discussed in the web. So I could find out how to do that homework in a short time. Actually, one may find the answer to almost any question about programming by just asking it to Google. There are many good references and documentation about programming online (stackoverflow.com, codeacademy.com, cprogramming.com, w3schools.com, and etcetera).

The definition of "program" by WordReference.com is: "a sequence of instructions enabling a computer to perform a task; piece of software". However, it is not obvious for a human being that programs like Photoshop, Excel and our favorite video-game are all just a set of instructions. But, surprise: it is exactly what they are. Complex programs, such as the ones of everyday use in our computers, are composed by hundreds, thousands, or even millions of instructions. Even though there are people who spend a lot of time programming, most of these big programs are result of the effort of many people working together (and sometime code generators are involved, too).

"Sequence" is the other important word in the "program" definition. Following the same set of instructions in different order may lead to a very different result. For example, the mathematical operation " 5 + 4 * 3 " involves one sum and one multiplication. Doing one or the other first give different results: "(5 + 4) * 3 = 27" and "5 + (4 * 3) = 17". Similarly, if you are giving directions to a friend to go from his home to yours, only God knows where he could go if he followed your instructions in different order!



So programming is all about giving instructions to the computer. For doing that, we use a set of rules given by a programming language (such rules are also referred to as the language "syntax"). Most instructions are very simple, like: add two numbers, draw a point in the screen, or write a letter into a file. However, the way they are written depends on the programming language, for example: some languages require you end each statement with a semi-colon (;), while others just need a new line to write another instruction. Choosing a programming language is sometimes matter of personal preferences, but sometimes is determined by the goal to achieve: you may not use the same language for doing complex mathematical calculations (you could use Matlab, Octave, R, or even C) and designing an interactive webpage (javascript, php, ruby, among others).

So, at the end, the mindset for programming consists basically on: understanding the goal to achieve, break it apart in small and individual steps, and translating them to the programming language we pretend to use.

Source:
Video lesson: "What is programming" in "Foundations of Programming: Fundamentals", at Lynda.com.

Saturday, February 27, 2016

Electrical engineering and abstraction

Before starting, I would like to recommend a book I really like for studying electronics. This book is "Foundations of Analog and Digital Electronic Circuits" by the professor Anant Agarwal and Jeffrey H. Lang. The book makes difficult concepts simple and it is full of examples and exercises to practice what one has just learn. In the case the content is not clear, one can check out the online course "Circuits and Electronics" (part 1, 2, and 3) available in edx.org. Having said that, let's jump into matter.

While science is used for understanding natural phenomena, engineering is dedicated to use it in a purposeful manner. Through scientific studies, we can abstract experimental data through analysis and simplifications, resulting in mathematical models (equations). Later, engineers can use these models to achieve specific functionalities.

As these models are subject to simplification, they may not apply in all scenarios. For example, the famous "the force equals the mass times the acceleration" (F = ma) may not work for objects moving at speeds comparable to the speed of light. So it is important to question ourselves before using mathematical models to find out if they are applicable for our purpose.

Electrical engineering is the purposeful use of the abstractions made to electromagnetic phenomena. Science describes such phenomenons using Maxwell Equations. Since these models are still very complex for most electrical engineering applications, some extra abstractions are made over them to get simpler models that describe relationships between current and voltage. This allows electrical engineers to work with algebraic equations rather than partial differential equations (phew!).


Each circuit component has its own voltage-current equation to describe its behavior in a circuit. Then, as the circuit is composed by more and more elements, it's whole mathematical description becomes complex (again!). But abstraction comes to save the day: we can apply further simplifications to describe circuit blocks or even entire circuits.

There is no limit for applying abstractions. They can be used to build systems, computers for example, that seem to be very complex if they are described in terms of the natural phenomenons that make it work. However they can be described as simple as a processing unit executing a set of instructions. Just remember the restrictions used to made these simplifications as they may lead to limitations. For example: your computer may not have been designed to work at a temperature of 200 degrees celsius!


Friday, February 26, 2016

The purpose of Capacitive Load

"A short pencil is much better than a long memory". Well, what I am using to write is not precisely a pencil; but the purpose of this site is to capture the technical stuff I want to remember. Hopefully, this will not just help me but others too.

I chose "capacitive load" as the name of this "notebook" for two reasons: I tried to call it "My Technical Me" but the name was not available; and because it refers to the first energy storage element one meets when studying electrical engineering (which happens to by my profession), which is a metaphor of the knowledge storage I pretend to make here.

The content in this blog is not be homogeneous in terms of what topics it covers. However, they all are technical. Some topics one may find are: mathematics, physics, electric circuits, programming, and so on.

Most posts are based on my learning experience, more than in formal sources (although, sometimes I use them to recall where I got some information from).

Having said all this: Welcome to Capacitive Load!