Install LaTeX?

Monday, March 19, 2012

Introduction to Freemat

Freemat is an open source math computing environment with its own programming language, which is very similar to Matlab. Though Matlab has much more robust documentation and access to toolboxes with special programs and features, freemat is a good solution for simple matrix operations and simple plotting functions.

Why use freemat instead of c++?

well, lets take a look at the c-code necessary to plot a sine wave.

Take a look at this code by vegaseat.

The program is fairly elegant and condensed into about 30 functional lines.

To do the same thing in freemat the syntax is as follows.

x = 0:.01:6.28;
y = sin(x);
plot(y);

the result of these three lines of code looks like this:

 In addition to being quicker and more intuitive than the C-code version, freemat comes with plot editor tools which can be used to add titles, keys, and other graph information. This makes it ideal for generating plots which can be used in presentations, posters or research papers.


Why are we discussing a numerical simulation environment in a course on Electronics? Well if you read any of the previous posts on introduction to transistors or introduction to nodal analysis, it is clear that systems of equations arise frequently in electric circuits. For instance the following circuit has unknown voltages in between R1 and R2, also between R2 and R3.





Using Nodal analysis and a naming convention for the voltage between R1 and R2 as V3 and the voltage between R2 and R3 as V4, the equations for those unknown nodes can be written as:

(V3-V1)/100+V3/1000+(V3-V4)/220 = 0
(V4-V3) /220+V4/1000+(V4-V2)/220 = 0
As V1 and V2 are known to be 12V and 9V respectively, the equation can be simplified to:
(0.015545)V3-(0.004545)V4=.12
(-0.004545)V3+(.010091)V4=.04091

This is a system of 2 equations with 2 unknowns, and can be solved by hand, or it can be solved by freemat.

If you have taken a class on matricies, you may remember the inverse matrix operation. Which can be used to solve problems of the form:

Ax=b

If the inverse of A is premultiplied by both sides, the answer x is easily found. To do this in freemat the following commands should be entered.

A = [0.015545 -0.004545; -0.004545 .010091]
b = [.12;.04091]
x = inv(A)*b;

the answer should look like :
x =
   10.2553
    8.6731  
Which is nearly identical to the output from the simulation model (pictured below)



Challenge: If given the circuit below, can you use freemat to determine the current through R3?

This is a good time to use mesh analysis, with the first mesh applying to components V1->R3->R1.
The second mesh applying to components V2->R2->R3.
The matrix for this circuit can be written:
A =
  30 -10
 -10  15
 b =
 15
  7

inv(A)*b = 
    0.8429
    1.0286

meaning that the mesh current through mesh 1 is .8429A clockwise, and mesh 2 is 1.0286 clockwise, meaning the current through R3 = .8429-1.0286 = -.1857A, the negative sign indicating that the current is directed up.

If the spice model is run, the following image results.

It is clear that matrix multiplication is an effective way to solve systems of equations, and the simplicity of using freemat makes it a viable circuit analysis tool.

No comments:

Post a Comment