# Lesson: Converting from Matlab to Python

It is not terribly difficult to recreate a MATLAB code in python. Python scripts may be written in any text editor (such as notepad), and run on a linux machine. This lesson will teach you how to translate an fsolve script from Matlab to python. For running the python script, please see the next lesson.

The following is an example of a MATLAB fsolve script that has been translated into python. This figure may be useful in understanding the rest of the lesson.

### Matlab Code

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 % Solution to part 2 of Murphy P4.40 clc clear % unknown vector % x = [ n2 xG,2 xW,2 xCO2,2 xH2,2 xi ] % define vector function containing 4 MBs, 1 AUX equilibrium % relationship, and 1 AUX constraint on mole fractions summing to 1 F =@ (x) [ 1.630 - x(2)*x(1) - x(6); % G MB 8.152 - x(3)*x(1) - 3*x(6); % W MB 0 - x(4)*x(1) + 3*x(6); % CO2 MB 0 - x(5)*x(1) + 7*x(6); % H2 MB x(4)^3*x(5)^7 - 55*x(2)*x(3)^3/(1.2)^6; % AUX 1 x(2) + x(3) + x(4) + x(5) - 1 ]; % AUX 2 % We have defined a set of equations for the six unknowns. Need to % supply an initial guess for each unknown quantity. x0 = [ 15 0.1 0.1 0.2 0.6 3 ]; % Solve options = optimset('display', 'off'); x = fsolve(F, x0, options); fprintf('\n Mole fraction of H2 at exit: %6.4f\n', x(5)) % fractional conversion of glycerine: (in - out)/in nG1 = 1.630; % gmol/h fc = (nG1 - x(2)*x(1))/nG1; fprintf(' Fractional conversion of glycerine: %5.3f\n', fc) 

### Python Code

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 from scipy.optimize import fsolve # unknown vector # x = [ n2 xG,2 xW,2 xCO2,2 xH2,2 xi ] # define vector function containing 4 MBs, 1 AUX equilibrium # relationship, and 1 AUX constraint on mole fractions summing to 1 def F(x): return [ 1.630 - x*x - x, # G MB 8.152 - x*x - 3*x, # W MB 0 - x*x + 3*x, # CO2 MB 0 - x*x + 7*x, # H2 MB x**3*x**7 - 55*x*x**3/(1.2)**6, # AUX 1 x + x + x + x - 1 ] # AUX 2 # We have defined a set of equations for the six unknowns. Need to # supply an initial guess for each unknown quantity. x0 = [ 15, 0.1, 0.1, 0.2, 0.6, 3 ] # Solve x = fsolve(F, x0) print '\n Mole fraction of H2 at exit: %6.4f\n' % x # fractional conversion of glycerine: (in - out)/in nG1 = 1.630 # % gmol/h fc = (nG1 - x*x)/nG1 print ' Fractional conversion of glycerine: %5.3f\n' % fc 

## Task 2: Things in Python but not Matlab

Python imports code at the beginning. This is essential for using fsolve and some other functions. Notice the following line:

### In Python

 1 from scipy.optimize import fsolve 

This line tells python to import fsolve; it is equivalent to telling a student to bring a calculator to class. Python requires this reminder.

## Task 3: Things in Matlab but not Python

Matlab requires clc and clear to empty the workspace. It also uses semicolons to suppress output. Python does not need these things--it is automatically suppressed, and it starts fresh every time the script is run.

### Uneccessary in Python

clc
clear


## Task 4: Things that are different in Python

There are several things that are changed when converting from Matlab to Python. The most basic are:

• Brackets instead of parenthesis for indexing ([] vs ())
• Number signs instead of percent signs for comments (# vs %)
• Indexing starts at zero instead of one (0 vs 1)
• Exponentials are double asterisk instead of up arrow (** vs ^)

### Python

           8.152 - x*x - 3*x,                  # W MB


### Vs Matlab

           8.152 - x(3)*x(1) - 3*x(6);                  % W MB


Function definition in python is slightly different. def is used instead of @, and python requires the return statement. Instead of semicolons, python just uses commas to separate the rows in a matrix. Finally, it is extremely important that the indexing is changed to start at 0 instead of 1!

### Python

 10 11 12 13 14 15 16 def F(x): return [ 1.630 - x*x - x, # G MB 8.152 - x*x - 3*x, # W MB 0 - x*x + 3*x, # CO2 MB 0 - x*x + 7*x, # H2 MB x**3*x**7 - 55*x*x**3/(1.2)**6, # AUX 1 x + x + x + x - 1 ] # AUX 2 

### Vs Matlab

 12 13 14 15 16 17 F =@ (x) [ 1.630 - x(2)*x(1) - x(6); % G MB 8.152 - x(3)*x(1) - 3*x(6); % W MB 0 - x(4)*x(1) + 3*x(6); % CO2 MB 0 - x(5)*x(1) + 7*x(6); % H2 MB x(4)^3*x(5)^7 - 55*x(2)*x(3)^3/(1.2)^6; % AUX 1 x(2) + x(3) + x(4) + x(5) - 1 ]; % AUX 2 

The last difference comes in displaying answers. The fprintf statement in MATLAB and the print statement in python have slightly different syntax. Note the differences below:

### In Python

print ' Fractional conversion of glycerine: %5.3f\n' % fc


### Vs Matlab

fprintf(' Fractional conversion of glycerine: %5.3f\n', fc)