Practical 1;
Aim: Plotting curves sin(X),cos(X),tan(y),log(X) and exp(X),x^2,x^3,x+x^2+exp(z).
Command:(input)
x=(0:pi/100:2*pi);
y=(-pi/2:pi/100:pi/2);
z=(-5:1:5);
p=cos(x);
q=sin(x);
r=tan(y);
s=log(x);
t=exp(z);
u=z.^2;
v=z.^3;
w=z+z.^2+exp(z);
subplot(221);
plot(x,p,'--rs',x,h,'g');
legend('sin(x)','cosine(x)');
title('sin&cosine graph');
xlabel('x');
ylabel('sin(x)');
subplot(222);
plot(y,r,'g');
legend('tan(y)');
title('tan graph ');
xlabel('y');
ylabel('tan(y)');
subplot(223);
plot(x,s);
legend('log(x)');
title('log graph');
xlabel('x');
ylabel('log(x)');
subplot(224);
plot(z,t,'r');
legend('exp(z)');
title('exp graph ');
xlabel('z');
ylabel('exp(z)');
figure;
subplot(311);
stem(z,u,'r');
title('x^2 graph');
xlabel('x');
ylabel('z.^2');
subplot(312);
stem(z,v,'p');
title('x^3 graph');
xlabel('x');
ylabel('z.^3');
subplot(313);
stem(z,w,'b');
title('x+x^2+exp(x) graph');
xlabel('x');
ylabel('x+x^2+exp(x)');
Graph (output);
Practical 1(b); different methods of plotting.
t=-10:0.001:10;
y=heaviside(t);
subplot(311);
plot(t,y)
axis([-10 10 -2 2 ])
xlabel('x(t)');
ylabel('y(t)');
title('unit step function');
z=heaviside(t-2);
subplot(312);
plot(t,z)
axis([-10 10 -2 2 ])
xlabel('x(t)');
ylabel('y(t)');
title('unit step function');
w=heaviside(t+2);
subplot(313);
plot(t,w)
axis([-10 10 -2 2 ])
xlabel('x(t)');
ylabel('y(t)');
title('unit step function');
Practical 2;
By dsolve:
1) dx/dt=-2x,x(1)=-1
syms x(t),a;
x=dsolve('Dx=-2*x','x(1)=-1')
a=[0 5];
ezplot(x,a)
figure;
2) dy/dt=sin2x-ytanx,y(1)=-1
syms y(x),a;
y=dsolve('Dy=sin(2*x)-y*tan(x)','y(1)=-1','x')
a=[0 5];
ezplot(y,a)
3) dy/dt=2y-3,y(0)=1
figure;
syms y(t),a;
y=dsolve('Dy=2*y-3','y(0)=1')
a=[0 5];
ezplot(y,a)
figure;
4) dy/dt=(x+y)2,y(0)=1
syms y(x),a;
y=dsolve('Dy=(x+y)^2','y(0)=1','x')
a=[0 5];
ezplot(y,a)
Practical-3
First order differential equations:
By ode:
1) dx/dt=-2x,x(1)=-1
function dx_dt=EM16(t,x)
dx_dt=(-2*x);
[t,x]=ode45('EM16',[0 5],[1,-1]);
plot(t,x)
xlabel('t');
ylabel('x');
title('first order ode ');
grid on;
2) dy/dt=2y-3,y(0)=1
function dx_dt=EM17(t,y)
dx_dt=(2*y-3);
[t,y]=ode45('EM17',[0 5],[0,1]);
plot(x,y)
xlabel('t');
ylabel('y');
title('first order ode ');
grid on;
(3) dy/dt=sin2x-ytanx,y(1)=-1
function dy_dx=EM18(x,y)
dy_dx=sin(2*x)-y*tan(x);
[x,y]=ode45('EM18',[0 5],[1,-1]);
plot(x,y)
xlabel('x');
ylabel('y');
title('first order ode ');
grid on;
Practical 4:
Second order differential equations:
1) d2x/dt2-3dx/dt+2x=0,x(0)=-1,x'(0)=0
syms x(t),a;
x=dsolve('D2x=3*Dx-2*x','x(0)=-1','Dx(0)=0')
a=[0 5];
ezplot(x,a)
2) d2x/dt2+5dx/dt+6x=0,x(0)=0,x'(0)=15
figure;
syms x(t),a;
x=dsolve('D2x=-5*Dx-6*x','x(0)=0','Dx(0)=15')
a=[0 5];
ezplot(x,a)
3) d2x/dt2+7dx/dt+10x=20,x(0)=5,x'(0)=3
figure;
syms x(t),a;
x=dsolve('D2x=-7*Dx-10*x+20','x(0)=5','Dx(0)=3')
a=[0 5];
ezplot(x,a)
4) (D2-5D+6)y=e4x,y(0)=-1,y'(0)=1
figure;
syms y(x),a;
y=dsolve('D2y-5*Dy+6*y=exp(4*x)','y(0)=0','Dy(0)=-1','x')
a=[0 5];
ezplot(y,a)
5) (D2-4D+13)y=8sin3x,y(0)=1,y'(0)=2
figure;
syms y(x),a;
y=dsolve('D2y-4*Dy+13*y=8*sin(3*x)','y(0)=1','Dy(0)=2','x')
a=[0 5];
ezplot(y,a)
Practical 5:
Using ode45 ;
(1)
d x/dt2+7dx/dt+10x=20,x(0)=5,x'(0)=3
2
function xdot= func(t,x)
xdot(1)=x(2);
xdot(2)= 20-7*x(2)-10*x(1);
xdot=xdot';
end
[t,x]=ode45('func',[0 5],[5 3]);
plot(t,x(:,1));
xlabel('Time(sec)'),ylabel('x(t)'), title ('Second order ode')
(2) d2x/dt2-3dx/dt+2x=0,x(0)=-1,x'(0)=0
function xdot= test(t,x)
xdot(1)=x(2);
xdot(2)= 3*x(2)-2*x(1);
xdot=xdot';
end
[t,x]=ode45('test',[0 5],[-1 0]);
plot(t,x(:,1));
xlabel('Time(sec)'),ylabel('x(t)'), title ('Second order ode')
(3)d2x/dt2+5dx/dt+6x=0,x(0)=0,x'(0)=15
function xdot= test_1(t,x)
xdot(1)=x(2);
xdot(2)= -5*x(2)-6*x(1);
xdot=xdot';
end
[t,x]=ode45('test_1',[0 5],[0 15]);
plot(t,x(:,1));
xlabel('Time(sec)'),ylabel('x(t)'), title ('Second order ode')
4)(D2-5D+6)y=e4x,y(0)=-1,y'(0)=1
function ydot= test_2(x,y)
ydot(1)=y(2);
ydot(2)= exp(4*x)-5*y(2)-6*y(1);
ydot=ydot';
end
[t,x]=ode45('test_2',[0 5],[-1,1]);
plot(t,x(:,1));
xlabel('Time(sec)'),ylabel('y(t)'), title ('Second order ode')
5)(D2-4D+13)y=8sin3x,y(0)=1,y'(0)=2
function ydot= test_3(x,y)
ydot(1)=y(2);
ydot(2)=8*sin(3*x)+4*y(2)-13*y(1);
ydot=ydot';
end
[t,x]=ode45('test_3',[0 5],[1 2]);
plot(t,x(:,1));
xlabel('Time(sec)'),ylabel('y(t)'), title ('Second order ode')
Practical 6;
EULER CAUCHY;
Using dsolve:
1)x2y"-3xy'+3y=0,y(1)=0,y'(1)=-2
figure;
syms y(t),a;
y=dsolve('x^2*D2y-3*x*Dy+3*y=0','y(1)=0','Dy(1)=-2','x')
a=[0 5];
ezplot(y,a)
By ode45;
function xdot= test_6(x,y)
xdot(1)=y(2);
xdot(2)=(3*x*y(2)-3*y(1))/x^2;
xdot=xdot';
end
[x,y]=ode45('test_6',[1 5],[0 -2]);
plot(x,y(:,1));
2)x2y"+4xy'+4y=4x2-6x3 ,y(2)=4,y'(2)=-1
By dsolve;
syms y(x) a;
y=dsolve('x^2*D2y+4*x*Dy+4*y=4*x^2-6*x^3','y(2)=4','Dy(2)=-1','x')
a=[0 5];
ezplot(y,a)
By ode45;
function xdot= test_7(x,y)
xdot(1)=y(2);
xdot(2)=(-4*x*y(2)-4*y(1)+4*x^2-6*x^3)/x^2;
xdot=xdot';
end
[x,y]=ode45('test_7',[1 5],[4 -1]);
plot(x,y(:,1));
LEGENDARY;
1)(x+2)2y"+3(x+2)y'-3y=0,y(0)=0,y'(0)=1
By dsolve;
syms y(x),a;
y=dsolve('(x+2)^2*D2y-3*(x+2)*Dy-3*y=0','y(0)=0','Dy(0)=1','x')
a=[0 5];
ezplot(y,a)
By ode45;
function xdot= test_8(x,y)
xdot(1)=y(2);
xdot(2)=(-3*(x+2)*y(2)-3*y(1))/(x+2)^2;
xdot=xdot';
end
[x,y]=ode45('test_8',[1 5],[0 1]);
plot(x,y(:,1));
2)(x+1)2y"-3(x+1)y'+4y=x2+x+1,y(0)=0,y'(0)=1
By dsolve;
syms y(x),a;
y=dsolve('(x+1)^2*D2y-3*(x+1)*Dy+4*y=x^2+x+1','y(0)=0','Dy(0)=1','x')
a=[0 5];
ezplot(y,a)
By ode45;
function xdot= test_8(x,y)
xdot(1)=y(2);
xdot(2)=(3*(x+1)*y(2)-4*y(1)+x^2+x+1)/(x+1)^2;
xdot=xdot';
end
[x,y]=ode45('test_9',[1 5],[0 1]);
plot(x,y(:,1));