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])