Vous êtes sur la page 1sur 18

MATLAB COMMANDS

MATRIX OPERATIONS
define matrix a
define matrix b
Sum of matrix a and matrix b
subtract each element of matrix a by 1
product of matrix a & matrix b
element by element multiplication of matrix a & matrix b
division of matrix a & matrix b using left(/) division
division of matrix a & matrix b using right(\) division
transpose of sum of matrix a & matrix b
sum of transpose of matrix a & transpose of matrix b
inverse of matrix a
determinant of matrix a
Eigen vector & Eigen value of matrix a
define random matrix of 3 by 4
define zero matrix of 3 by 3 order
show the diagonal of matrix a as vector
show the upper triangular matrix of a
show the lower triangular matrix of a
define identity matrix of 3 by 3 order

a=[12 45 ; 25 70]
b=[ 34 6 ; 10 35]
a+b
a-1
a*b
a.*b
a/b
a\b

inv(a)
det(a)
eig(a)
rand(3,4)
zeros(3)
diag(a)
triu(a)
tril(a)
eye(3)

Q, 1.

OVERLAY PLOT USING HOLD ON


x=linspace(0,2*pi,100);
y1=x;
y2=sin(x);
y3=exp(-.4*x).*sin(x);
plot(x,y1,'b-.')
hold on
plot(x,y2,'r--')
hold on
plot(x,y3,'ko')
xlabel('x')
ylabel('y1/y2/y3')
title('overlay plot')
grid on

Sol. 1.

Q,2. USING SUBPLOT COMMAND


t=linspace(0,2*pi,100);
y1=t;
y2=sin(t);
y3=cos(t);
y4=exp(-2*t).*sin(10*t);
subplot(2,2,1)
plot(t,y1,'b')
xlabel('t'),ylabel('t)'),title('plot 1'),grid on ,axis square
subplot(2,2,2)
plot(t,y2,'k-.')
xlabel('t'),ylabel('sin(t)'),title('plot 2'),grid on
subplot(2,2,3)
plot(t,y3,'r--')
xlabel('t'),ylabel('cos(t))'),title('plot 3'),grid on
subplot(2,2,4)
plot(t,y4,'m:')
xlabel('t'),ylabel('exp(-2*t).*sin(10*t)'),title('plot 4'),axis([0 3 -1 1])

Sol. 2

Q,3. Create an any matrices and apply all operations on it.


%%Matrix of A
A=[8 7 6;1 6 8; 9 4 6]
%%Matrix of B
B=[7 8 1;6 2 8;4 6 7]
%%SUM OF MATRICES
X=A+B
%%DIFFERENCE OF MATRICES
Y=A-B
%%PRODUCT OF MATRICES
Z=A*B
%%DIVISION OF MATRICES
W=A/B
%%TRANSPOSE OF SUM OF MATRICES
V=(A+B)'
%%INVERSE OF MATRICES
U=A\B
%%EIGEN VALUE OF MATRIX
T=eig(A)
%%IDENTITY MATRIX
M=eye(3)
%%DETERMINANT OF MATRIX
N=det(A)
%%UPPER TRIANGULAR MATRIX
L=triu(A)

%%LOWER TRIANGULAR MATRIX


K=tril(B)
%%DIAGONAL OF MATRIX
P=diag(B)

Q,4. Solve given equation by using suitable matlab commands.

Given : velocity =0.25 m/s, Air flow rate= 65m3/s , Area = 50 m2, efficiency = 95%.
Sol.

Q,5. Solve given equation

Where, k1 = 25 kpa-s/m and k2 = 10^-5 s-1 , Area = 2000m2 , C = 0.015kg/s ,


V= 15m3/s ?

Sol.

ODE 23
Q,6. Solve differential equation by ODE23.
Sol. function fv=funsys(t,y)
fv(1,1)=(2*y(1))+y(2)+(5*y(3))+exp((-2)*t);
fv(2,1)=(-3*y(1))-(2*y(2))-(8*y(3))+(2*exp(-2*t))-(cos(3*t));
fv(3,1)=(3*y(1))+(3*y(2))+(2*y(3))+(cos(3*t));
[t,Y]=ode23(@funsys,[0 2],[1 -1 0]);
plot(t,Y(:,1),'+',t,Y(:,2),'x',t,Y(:,3),'o')

Q,7. Solve given equation using ODE23


1
2

= Y2
= 1000(1-Y1^2)Y2

Y1

Cond: Y1(0)= 0 ; Y2(0) = 1


Sol.

function dy = vdp1000(t,y)
dy = zeros(2,1);

% a column vector

dy(1) = y(2);
dy(2) = 1000*(1 - y(1)^2)*y(2) - y(1);
[T,Y] = ode23 (@vdp1000,[0 3000],[2 0]);
plot(T,Y(:,1),'-o')

ODE45
Q,8 Solve
function dy=Dg(t,y)
dy = zeros(3,1); % a column vector
dy(1) = y(2) * y(3);
dy(2) = (-y(1)) * y(3);
dy(3) = (-0.51) * y(1) * y(2);
[T,Y] = ode45(@Dg,[0 12],[0 1 1]);
plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')
grid on
xlabel('TIME')
ylabel('Function value')
title('ODE45').

Q,9. Solve:
function out1 = vdp1(t,y)
[t,y] = ode45('vdp1',[0 20],[2 0]);
out1 = [y(2); (1-y(1)^2)*y(2) - y(1)];
plot(t,y(:,1),'-',t,y(:,2),'-.')
xlabel('Time Interval')
ylabel('out')
title('vdp1')
grid on

Q,10. Find by Taylor series method, the value of y at x=0.1 and x= 0.2
dy/dx = 1- (2*x*y ) , y(0) = 0
Sol . function dydx=pro1(x,y)
y0=0;
t0=0;
tend=.2;
%solving using ode45
[t,y] = ode45(@pro1,[t0:.1:tend],y0);
dydx=1-2*x*y;
[t,y]

BOUNDARY VALUE PROBLEM (bvp4c)


Q,11. Solve
function bvp
solinit = bvpinit(linspace(0,1,5),[1 0]);
sol = bvp4c(@twoode,@twobc,solinit);
x = linspace(0,1);
y = deval(sol,x);
plot(x,y(1,:));
grid on
function dydx = twoode(x,y)
dydx = [ y(2)
4.547 * y(1)];
function res = twobc(ya,yb)
res = [ ya(1) - 1
yb(2) - 0.4737 * yb(1)];

Q,12. Solve
function bvp3
solinit = bvpinit(linspace(0,1,5),[1 0]);
sol = bvp4c(@twoode,@twobc,solinit);
x = linspace(0,1);
y = deval(sol,x);
plot(x,y(1,:))
grid on
function dydx = twoode(x,y)
dydx = [ y(2)
1^(2)*y(1)*exp((20*0.1*(1-y(1)))/(1+0.1*(1-y(1)))) ];
function res = twobc(ya,yb)
res = [ ya(2)
yb(1) - 1];

Q,13. Solve
function BVP5
solinit = bvpinit(linspace(0,1,5),[0 1]);
sol = bvp4c( @twoode, @twobc, solinit);
x = linspace( 0,1);
y = deval(sol, x);
plot(y,x(1,:))
grid on
function dydx = twoode(x,y)
dydx = [ y(2)
(-p*(y(2))^2)/(1+p*y(1)) ];
function dydx = twobc(ya, yb)
dydx = [ ya(1)
yb(1)-1];

Vous aimerez peut-être aussi