Playing with octave (the software)

If you get into trouble, you can usually interrupt Octave by typing Control-C (written C-c for short). C-c gets its name from the fact that you type it by holding down and then pressing . Doing this will normally return you to Octave’s prompt.

 

octave can work as calculator and support following functions:

cos Cosine of an angle (in radians)

sin Sine of an angle (in radians)

tan Tangent of an angle (in radians)

exp Exponential function (ex)

log Natural logarithm (NB this is loge, not log10)

log10 Logarithm to base 10

sinh Hyperbolic sine

cosh Hyperbolic cosine

tanh Hyperbolic tangent

acos Inverse cosine

acosh Inverse hyperbolic cosine

asin Inverse sine

asinh Inverse hyperbolic sine

atan Inverse tangent

atan2 Two-argument form of inverse tangent

atanh Inverse hyperbolic tangent

abs Absolute value

sign Sign of the number (−1 or +1)

round Round to the nearest integer

floor Round down (towards minus infinity)

ceil Round up (towards plus infinity)

fix Round towards zero

rem Remainder after integer division

 

Think of making a program which solves the IUPAC equations. the user writes an inbalanced equation and the program generates the balanced equation.

 

A simple example comes from chemistry and the need to obtain balanced chemical equations. Consider the burning of hydrogen and oxygen to produce water.

H2 + O2 –> H2O

 

The equation above is not accurate. The Law of Conservation of Mass requires that the number of molecules of each type balance on the left- and right-hand sides of the equation. Writing the variable overall reaction with individual equations for hydrogen and oxygen one finds::)

x1*H2 + x2*O2 –> H2O

H: 2*x1 + 0*x2 –> 2 O: 0*x1 + 2*x2 –> 1

 

The solution in Octave is found in just three steps.

octave:

1> A = [ 2, 0; 0, 2 ];

octave:2> b = [ 2; 1 ];

octave:3> x = A \ b

 

now, you can save the

equation =  H2 + O2 –> H2O

 

strsplit(equation,”–>”) will divide the equation into two parts:

 

octave:14> strsplit(equation,”->”)

ans ={ 

[1,1] = H2+O2  

[1,2] = 

[1,3] = H20

}

 

what if the string have spaces.

 

octave:15> eq =” H2 + O2 –> H20″

eq =  H2 + O2 –> H20

octave:21> strsplit(eq,”–>”)

ans ={

[1,1] =  H2 + O2  

[1,2] = 

[1,3] = 

[1,4] =  H20

}

 

this variablity of result with variation of input needs to be tackled.

inidividual elements of matrix can be accessed using the following command:

 

octave:46>

ans(1,1)

ans ={

[1,1] =  H2 + O2

}

 

 

now, you need to know which element exists in the equation.

this can be  easily done:

get the position of alphabets:

octave:52> isalpha(res(1,1))

ans =

{

  [1,1] =

     0   1   0   0   0   0   1   0   0

}

get the position of numbers:

octave:52> isdigit(res(1,1))

ans =

{

  [1,1] =

     0   0  1  0   0   0   0   1   0 

}

 

now, think of putting everything in a function such that you are able to interpret any equation and make it balanced.

this is the target:

before going further, let’s  see an example of function which find the max value in vector. here, a  function can return more than one value. in this case, we need to find the maximum value of an element in vector and further it’s index as well.

 

   function [max, idx] = vmax (v)

idx = 1;

max = v (idx);

for i = 2:length (v)

if (v (i) > max)

max = v (i);

idx = i;

endif

endfor

endfunction

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s