Bisection Method Using MATLAB

Bisection method is based on Intermediate Value Theorem which states as,
 Let f (x) be a continuous function on the interval [ab]. If d $ \in$ [f (a), f (b)], then there is a c $ \in$ [ab] such that f (c) = d.

We are using a sub-form  form of this theorem, In our case we are given with two values at which function changes its value form positive to negative and we have to find a value where function is zero or in other words root of the function. We then use a binary search type algorithm that slowly decreases this interval and finally gets solution with some error. 
So if we are given with a interval [xl , xu] as lower and upper limit. Value of function with xl is negative and with  xu is positive then there must be at least one point in between these two values which is  root of the function  or in other words function becomes zero. Function should be real and continuous.

Algorithm:

  1. We check weather function changes its value from positive to negative within given interval, If it changes we proceed other wise we can't find solution.
  2. We find mid point of the interval, xm. 
  3. If function at  xm is negative, it becomes our new xl, if its positive its our new xu and if it happens to be zero, we find our root (It rarely happens). 
  4. We find error by seeing the difference between last and new xm. If error is smaller than provided one, we stops other wise step 2 and 3 repeats.
  5. If we also limit iterations by 1000. Means our program can execute more than 1000 iterations.

MATLAB Code:

%Author: Muhammad Awais
%UET Lahore, Electrical Engineering
%fb/awais12506
function [iteration,xm]=Bisection(xl,xu,f,e)
%Find Root of a equation by method of BiSection
%Input: Lower Limit, Upper Limit, Function, Error Tolerance
%Please Insert f as f=@(x)x.^2+9*x+3
%Output: Root of the equation with given precison
%Exception: Give error if Equation is not convergent or roots are dont give
%postive and negative values of the function

iteration=0;     %To see how many iterations, equation took to solve
xm=xl;
error=1;
if  (f(xl)*f(xu)>0)
    disp('Interval have some error')

else
     while ( abs(error) > abs(e) ||iteration<=1000 )
        iteration=iteration+1;
        xmOld=xm;
        xm=(xl+xu)/2;   %Mid point
        xmNew=xm;
        if f(xl)*f(xm)<0   %if f(xm) is neg, equate xu with xm
            xu=xm;
        else if f(xl)*f(xm)>0 %if f(xm) is pos, equate xl with xm
            xl=xm;
            else            %it means xm is our root
            break
            end
        error=(xmNew-xmOld)/xmNew;   %Error
        end

    end

end
end

YBus building algorithm of Power System using MATLAB

Nodal admittance matrix or Ybus matrix is a matrix that contains admittances of the power system. This matrix can be used to find voltages of buses.
This Ybus matrix can be found easily and then inverted and multiplied with currents to find voltages of buses. Although taking inverse of a large system is computationally not efficient and there are ways to find Zbus directly instead of taking inverse of Ybus.
Ybus is sparse matrix with a lot of zeroes in it. So its a large matrix with a lot of zeroes.

Algorithm used in our code is  of 3 steps.

1.     Circuit is converted into matrix from called data. First column of data contains from and second contains to means nodes to which a admittance is connected. Third and forth contains value of resistance and reactance respectively.
2.     Off-diagonal entries are just net admittances connected to node i and j.
3.     Diagonal entries contains sum of all entries connected to that node. For example Y 3,3 is net of all the entries connected to bus 3.
Code:
%Author: Muhammad Awais
%Electrical Engineering, UET Lahore
%Bus admittance matrix
%Write buss impedence in Z matrix and it will solve rest of the thing
%data matrix

   data=[0   1   0   1j
   0   2   0 0.8j
   1   2   0 0.4j
   1   3   0 0.2j
   2   3   0 0.2j
   3   4   0 0.08j];

 % from  to R   X
%Z=[0   1   0   1j
 %  0   2   0 0.8j
 %  1   2   0 0.4j
  % 1   3   0 0.2j
   %2   3   0 0.2j
   %3   4   0 0.08j];

%finding order of matrix in C language way
%o1=max(Z(:,1));
%o2=max(Z(:,2));
%order=(max(o1,o2))
%find number of rows of Z= Toatl number of nodes
%row=length(Z(:,1))

[row,col]=size(data);
order=col

%Change last column into admittance, now last column also inculdes R
for m=1:row
    data(m,4)=1/(data(m,3)+data(m,4));
end

Z2adm=data;

%Yadmittance as a matrixo of zeros first
Y=zeros(order,order);

%finding ybus matrix

%1-for off-digonal vlaues
for i=1:row
    for j=1:order
        %discard source node
        if data(i,1)==0||data(i,2)==0    
           a=0;
         %for off digonal entries
        elseif data(i,1)~=0||data(i,2)~=0
            Y(data(i,1),data(i,2))=-data(i,4);
            Y(data(i,2),data(i,1))=-data(i,4);
        end
        
    end
end
%2-digonal values 
for a=1:order     %for k
    for b=1:row
        if data(b,1)==a ||data(b,2)==a
           
            Y(a,a)=(Y(a,a)+data(b,4));
        end
    end
   
end
Ybus=Y

%To find Z bus
Zbus=inv(Y)

%As Ibus=Ybus*Vbus so we can find too if we know Ibus. As here two currnet
%sources so suppose
Ibus=[1;1;0;0];
Vbus=Ybus\Ibus


Examples:
Example 1: To solve following system using this method

Impedance Matrix:

data=[0   1   0   1j
   0   2   0 0.8j
   1   2   0 0.4j
   1   3   0 0.2j
   2   3   0 0.2j
   3   4   0 0.08j];
Results:
Ybus =
        0 - 8.5000i        0 + 2.5000i        0 + 5.0000i        0         
        0 + 2.5000i        0 - 8.7500i        0 + 5.0000i        0         
        0 + 5.0000i        0 + 5.0000i        0 -22.5000i        0 +12.5000i
        0                  0                  0 +12.5000i        0 -12.5000i
Zbus =
        0 + 0.5000i        0 + 0.4000i        0 + 0.4500i        0 + 0.4500i
        0 + 0.4000i        0 + 0.4800i        0 + 0.4400i        0 + 0.4400i
        0 + 0.4500i        0 + 0.4400i        0 + 0.5450i        0 + 0.5450i
        0 + 0.4500i        0 + 0.4400i        0 + 0.5450i        0 + 0.6250i
Vbus =
        0 + 0.9000i
        0 + 0.8800i
        0 + 0.8900i
        0 + 0.8900i

Example 2:

Impedance Matrix:
data=[0   1   0  0.25j
   0   1   0  -4j
   1   4   0   0.4j
   1   3   0   .1j
   0   2   0    .2j
   0   2   0    -4j
   0   3   0    -4j
   0   3   0.9412  .2353j
   3   4   0      0.2j
   0   4   0     -4j
   0   4   .4706  0.11765j
    ];

Results:
Ybus =
        0 -20.2500i        0 + 4.0000i        0 +10.0000i        0 + 2.5000i
        0 + 4.0000i        0 -15.0000i        0                  0 + 6.2500i
        0 +10.0000i        0             1.0000 -15.0000i        0 + 5.0000i
        0 + 2.5000i        0 + 6.2500i        0 + 5.0000i   2.0000 -13.9998i
Zbus =
   0.0359 + 0.1305i   0.0303 + 0.0718i   0.0481 + 0.1134i   0.0498 + 0.0887i
   0.0303 + 0.0718i   0.0264 + 0.1224i   0.0398 + 0.0744i   0.0439 + 0.0878i
   0.0481 + 0.1134i   0.0398 + 0.0744i   0.0652 + 0.1733i   0.0648 + 0.1061i
   0.0498 + 0.0887i   0.0439 + 0.0878i   0.0648 + 0.1061i   0.0736 + 0.1538i
Vbus =
   0.0662 + 0.2023i
   0.0567 + 0.1941i
   0.0879 + 0.1878i
   0.0937 + 0.1765i




Reference:

Examples were taken from Power System Analysis - Hadi Saadat

Earth quake


  • Just felt a Earthquake of 6.5 intensity. Some days ago I read that Pakistan is in the most active quake zone. 

          http://www.dawn.com/news/1215636
  • A website for monitoring earthquake is as follows....


  • And here are some precautionary measures....

Kron Reduction using Matlab Code

Kron Reduction
In power system anylasis, kron reduction is a way of reducing buses in Y bus matrix of the power system. It usually used for two reasons
  1.             To remove a bus where current injection is zero to reduce size of matrix thereby effectively making computation easy. Buses with no load or source don't inject currents.
  2.            To use only some buses specific buses instead of whole system to know required voltages only. This way we can get voltages of our interest with less computational power of our computer.

If Y j ,k  is our matrix to be kron reduced and p is number of row and coloum to be reduced then this formula is used to find kron reduced elements.

Matlab Code:
%Author: Muhammad Awais fb/awais12506
%Kron Reduction
%p is row and colum to be removed
%Y is array to be kron reduced

clear all
clc
p=2;
Y=[-16.75i 11.75i 2.5i 2.5i
    11.75i -19.25i 2.5i 5i
    2.5i 2.5i -5.8i 0
    2.5i 5i 0 8.3i];
[row,col]=size(Y);   
Y_new=zeros(row,col);  %Produces 4 by 4 matrixes of zeros

%In this loop all p colum and row is replaced by 0 and other by respective
%value
for k=1 :row
    for l=1 : col
        if k==p || l==p
            Y_new(k,l)=0;
        else 
           Y_new(k,l)=Y(k,l)-((Y(k,p)*Y(p,l))/(Y(p,p)));
           
        end
    end
end
% To remove p row and colum
Y_new(:, p) = [];
Y_new(p, :) = [];
Y_new




Example:

Kron Reduction by calculation

Kron Reduction by our Program:
Y_new =
        0 - 9.5779i        0 + 4.0260i        0 + 5.5519i
        0 + 4.0260i        0 - 5.4753i        0 + 0.6494i
        0 + 5.5519i        0 + 0.6494i        0 + 9.5987i

There was a mistake while entering data so answer was a bit wrong. Mistake is corrected and now also I have written the code in function form ....

MATLAB Code:
%Author: Muhammad Awais fb/awais12506
%Descrption: Function uses Kron Reduction to reomove a column
%Input:Y==> Matrix to be reduced, p==> Row and Colum to be removed
%OutPut: Reduced Matrix
function Y_Reduced=KronReduction(Y,p)
[row,col]=size(Y);
Y_new=zeros(row,col);  %Produces 4 by 4 matrixes of zeros

%In this loop all p colum and row is replaced by 0 and other by respective
%value
for k=1 :row
    for l=1 : col
        if k==p || l==p
            Y_new(k,l)=0;
        else
           Y_new(k,l)=Y(k,l)-((Y(k,p)*Y(p,l))/(Y(p,p)));

        end
    end
end
% To remove p row and colum
Y_new(:, p) = [];
Y_new(p, :) = [];
Y_Reduced=Y_new;
end

Output:


In Search of Water

Imagine you are on a place with no water. What would you do? Obviously, try to find water with all resources possible 'cause its the only way for us to live. The very same principle applies for life. If you want to find life, you will always look for water. In fact water is only trace of life. Life is always present alongside water in one form or other. Why? Actually life may have never begun with out water as scientists think it was water that give essential medium for mixing of organic compounds that results in very simple, uni cellular organisms. And not only this, water also played and important role in evolution of simple life in complex multi cellular life and survival of this life.

Now if we want to discover life outside our planet, we have to stick with this basic principle of water. Mean instead of finding life directly, we search for water. That's what NASA do all the time. They are trying to find life outside the planet earth and they were trying to find water. They sent a lot of non-human missions to mars in order to find life, Yes, the little red planet.




 Recently they sent a well equipped mission named curiosity rover. A non-human, automatic rover full of chemical analytical equipment to study martian surface. And it was not the first of its kind. Actually this was second.

Selfie taken by rover on barren martian surface.
Earlier today, NASA has confirmed that a imaging spectrometer on MRO detected signature of hydrated minerals which is considered most strong evidence of flowing water yet. It was found in different location with temperature of -23 degree Celsius or less and it disappeared in cold times.



recurring slope lineae
These dark, narrow, 100 meter-long streaks called recurring slope lineae flowing downhill on Mars are inferred to have been formed by contemporary flowing water. Recently, planetary scientists detected hydrated salts on these slopes at Hale crater, corroborating their original hypothesis that the streaks are indeed formed by liquid water. The blue color seen upslope of the dark streaks are thought not to be related to their formation, but instead are from the presence of the mineral pyroxene. The image is produced by draping an orthorectified (Infrared-Red-Blue/Green(IRB)) false color image (ESP_030570_1440) on a Digital Terrain Model (DTM) of the same site produced by High Resolution Imaging Science Experiment (University of Arizona). Vertical exaggeration is 1.5.
Credits: NASA/JPL/University of Arizona



“Our quest on Mars has been to ‘follow the water,’ in our search for life in the universe, and now we have convincing science that validates what we’ve long suspected,” said John Grunsfeld, astronaut and associate administrator of NASA’s Science Mission Directorate in Washington. “This is a significant development, as it appears to confirm that water -- albeit briny -- is flowing today on the surface of Mars.”
Although it is strongest evidence but it is not first of its sort. It also found calcium perchlorate, a salt in soil which suggests water presence on Mars. This salt actually lowers temperature of water freezing and water can be found in liquid form at even -125 degree Celsius.

And also in December  last year, Curiosity found burps of methane which might show bacterial action on surface as most of methane on earth is produced by living organisms with waste.

By the way don't think it as marketing scheme for coming science fiction o mars as some people might think ;). 



Although neither water nor any kind on life is yet confirmed on mars but one thing we can say for sure is that mars is not as barren and dried as we 

Sources:

Some basic codes for TM4C123 lauchboard using Energia

A basic blinky code:


ARM Cortex M3, Programming

I am studying a course in university name Microprocessor system. We are using ARM Cortex M3 and evaluation board is TM4C123. This is nice board that can be programmed like Arduino using a software named Energia or as any other common ARM code editor like keil code composer. Here I'll share all the codes and other we will cover. Here is list of some materials.

 Books:

      REQUIRED:
  1.     J. Valvano, Introduction to Embedded Systems: Introduction to ARM CORTEX-M  Microcontrollers, 3rd ed., December 2012.

     References:
  1. 1. J. Yiu, The Definitive Guide to the ARM® Cortex-M3, 2nd edition, 2010.
  2. 2. ARM®v7-M Architecture Reference Manual
    http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0403c/index.html

Board:

Tiva™ C Series LaunchPad Evaluation Kit
(ACTIVE) EK-TM4C123GXL


What is PATH in environment variables and how to change it

[Windows]

Every process need to do some tasks like to create temporary files, execute some files, store some data etc. Environment variables are set of values that defines default location for these tasks. So whenever a process requires to create a temp file, it looks to environment variable and see the location given by it. 
Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.[1]

Anroid phone Buttons Dilema

I have a anroid phone, HTC X one. Its very nice mobile and working nice for me. It have 3 physical buttons on the bottom.

Visit of Saritow Spinning Mills Ltd's Power Plant


Visit of Saritow Textile Mills Ltd Power Plant 
Pakistan is facing worst blackouts of the history due to a large difference of production and consumption of power. They are becoming more and more worse with time. These blackouts are specially affecting industries so a lot of industries are installing their own small scale gas or oil fired power plants to meet their energy needs. Although this adds extra cost to purchase and maintain their own plants but still severe black outs encourages and even small industrialists are making their own power plants.
Saritow Spinning Mills Ltd Lahore also has their own 10 MW power plant. Last day I visited this power plant and observe synchronization of plant with WAPDA supply. Here I going to write whatever I have seen.