Saturday, June 13, 2015

Matlab code for solving linear equations using Gauss elimination process

clc; clear all; close all;
% A = [1 3;2 4];
% b = [5;6];

A=[ 2 1 1; 4 -6 0; -2 7 2 ]; 

b=[ 5;-2;9 ];

[m,n] = size(A);
if m~=n, error('A matrix needs to be square'); end
nb = n+1;  Ab = [A b];   %  Augmented system
fprintf('\n Begin forward elimination with Augmented system;\n'); disp(Ab);


for i =1:n-1    
    pivot = Ab(i,i);
   
    for k=i+1:n
        factor     = - Ab(k,i)/pivot;
        Ab(k,i:nb) = Ab(k,i:nb) - (Ab(k,i)/pivot)*Ab(i,i:nb);  
        fprintf('Multiplication factor is %g\n',factor);
        disp(Ab); 
        
        pause;
    end
    fprintf('\n After elimination in column %d with pivot = %f \n\n',i,pivot);
    disp(Ab);
    pause;
end


x    = zeros(n,1);   % Initializing the x vector to zero
x(n) = Ab(n,nb) /Ab(n,n);
for i= n-1:-1:1
    x(i) = (Ab(i,nb) - Ab(i,i+1:n)*x(i+1:n))/Ab(i,i);
end
x

Sunday, May 24, 2015

Matlab code to solve differential equation D2y-2*Dy+2*y=exp(2*t)*sin(t)

%% Solving differential equation using dsolve function

y=dsolve('D2y-2*Dy+2*y=exp(2*t)*sin(t)','y(0)=-0.4','Dy(0)=-0.6','t');

%%Pretty the y
pretty(y)

%%Plot the solution

ezplot(y,[0 5])


Tuesday, May 19, 2015

Matlab code to solve the 2nd order differential equation y''-2y'-3y=3*t^2+4*t-5

close all;

clc;

%% Solve the 2nd order differential equation y''-2y'-3y=3*t^2+4*t-5

%% y' can be represented using 'D' or diff and y'' can be represented using 'D2y'
%% For 2nd order differential equation provide 2 initial condition


y=dsolve('D2y-2*Dy-3*y=3*t^2+4*t-5','y(0)=1','Dy(0)=2','t')

%% Make Y pretty

pretty(y)

%% Plot the expression at diffrent value of x

%% ezplot-> y is a function with respect to x

%% only plot --> x and y has some values

ezplot(y,[0 50])

Monday, May 18, 2015

MatLab code to Solve the 1st order differential equation dy/dx=e^y-2

close all;
clc;

%% Solve the 1st order differential equation dy/dx=e^y-2

%% dy/dx can be represented using 'D' or diff 

%% y(1)=1 is the initial condition 

y=dsolve('Dy=exp(y)-2','x')

%% Plot the expression at diffrent value of x

%% ezplot-> y is a function with respect to x

%% only plot --> x and y has some values

%%ezplot(y,[0 5])

Sunday, May 17, 2015

MatLab code to Solve the 1st order differential equation dy/dx=3-1/x

close all;

clc;

%% Solve the 1st order differential equation dy/dx=3-1/x
%% dy/dx can be represented using 'D' or diff 
%% y(1)=1 is the initial condition 
%% if y(0) is tried then due to log factor empty system will be resulted

y=dsolve('Dy=3-1/x','y(1)=1','x')

%% Plot the expression at diffrent value of x

%% ezplot-> y is a function with respect to x
%% only plot --> x and y has some values

ezplot(y,[0 5])

Matlab code to Solve the 2nd order differential equation y''-2y'-3y=e^2t

close all;

clc;

%% Solve the 2nd order differential equation y''-2y'-3y=e^2t

%% y' can be represented using 'D' or diff and y'' can be 

%% represented using 'D2y'

%% For 2nd order differential equation provide 2 initial condition

y=dsolve('D2y-2*Dy-3*y=exp(2*t)','y(0)=1','Dy(0)=2','t')

%% Make Y pretty

pretty(y)

%% Plot the expression at diffrent value of x

%% ezplot-> y is a function with respect to x
%% only plot --> x and y has some values

ezplot(y,[0 50])

MatLab code to solve 1st order differential equation dy/dx=2+x^2-y

In First order differential equation, order of the equation is 1




Example Program :

%% Solve the 1st order differential equation dy/dx=2+x^2-y

%% dy/dx can be represented using 'D' or diff 

%% y(0)=1 is the initial condition 

y=dsolve('Dy+y=2+(x^2)','y(0)=5','x')

%% Plot the expression at diffrent value of x

%% ezplot-> y is a function with respect to x

%% only plot --> x and y has some values

ezplot(y,[0 5])

Thursday, May 14, 2015

Hermitian of a complex matrix using MatLab

Herimitian of a complex matrix can be obtained by taking the conjugate transpose of that matrix

Example Program :


%% Generate a complex matrix

A=[i 2 3; 3i+3 3 1+i; i i 7]

%% Hermitian of matrix

B=A'

Conversion of general matrix to logical array using MatLab

If the element is non zero then it will be manipulated as logical one. else it is taken as zero.


Example Program:


%% Generate a Matrix

A=[3 5 6; 4.5 0 6;0 0 1.3]

%% Convert the matrix into the logical array

B=logical (A)

Wednesday, May 13, 2015

Check the class of the variables in MatLab

The variables may be of type float, Int, char etc.  

Example Program : 

%% Check the class of the variables
%% Assign float value to the A

A = 3.055;

%% Display the class of A
class(A)

%% Assign the Int value to B

B=3

%% Display the Class of B
class(B)

%% Assign the char value to c

C='hello'

%% Display the Class of c
class(C)

Data Type check of stored variables in Matlab work space

'whos' command will display the details of all variables in the work space

Example Program:

%% Generate a matrix A

A=[5 4 3; 2 6 3; 8 9 5];

%Get the details about the A

whos;

Toeplitz matrix generation using Matlab

Toeplitz matrix is a matrix in which each descending diagonal from left to right is constant  

Example Program :

%Generate toeplitz matrix of 3x3 consisting of elements 2 5 6

t = toeplitz([2 5 6])

Tuesday, May 12, 2015

Generating Vandermonde Matrix in MatLab

The Vandermonde matrix is a nxn matrix whose row elements are in the form of geometric progression. This matrix has full rank .

Example program :

%% Generate Vandermonde matrix

%% Fundamental Elements 

v = 1:.5:2


A = vander(v)


Logical Array in Matlab

The key word 'true' in MatLab is evaluated to '1', 'false' evaluated to '0'.


Example Program :

%%Create logical array's

x=[true false]

Wednesday, May 6, 2015

Swap Rows and Columns of the matrix using Matlab

In the most of the algorithm swapping of the matrices are very necessary. Example in the Gauss elimination procedure in order to perform the Row operations swapping is important.

Example Program:


%% Close all files and clear screen  %%


close all;
clear all;
clc;


%% Genrate Matrix A

A=[1 2 3; 4 5 6; 7 8 9]

%% Swap the rows of the matrix

Y=A([1,3,2],:)


%% Swap the columns of the matrix

Z=A(:,[1,3,2])