You are on page 1of 4

% Plotting the solution of exponential decay differential equation

% Dn/Dt = -(k+1)N
% with initial conditions n0 = 17^7 and k=3.5

clear screen
format long
k = 3.5;
eqn='Dn+(k+1)*n=0';
inits='n(0)=17^7';

n=dsolve(eqn,inits,'t');
t=linspace(0,3,100);
z=eval(vectorize(n));

plot(t,z);
title('Exponential Decay Problem');
xlabel('t')
ylabel('y')

% Verififying Bolzano-Weierstrass Theorem


% { 0, if n is odd
% an = {
% { 1, if n is even

clear screen
format long

for n=1:50
if mod(n,2)==1
a(n)=0;
else
a(n)=1;
end
end
plot(a(1:50),'b.');
axis([0,50,-1,2])
% Plotting of solution of 2nd order differential equation
% y" + y = 0
% With initial conditions y(0)=0 and y'(0)=0
% In the interval [0,20]

clear screen
format long
eqn='D2y+y=0';
inits='y(0)=0,Dy(0)=0';
y=dsolve(eqn,inits,'x');
x=linspace(0,20,500);
z=eval(vectorize(y));
plot(x,z);
title('Solution of 2nd Order Differential Equation');
xlabel('x');
ylabel('y');

% Convergence/Divergence of infinite series using partial sum

clear screen
format long
sum=0;
for n=1:50
sum = sum + ((n-1)^2)/(2^(n-1));
partialSum(n) = sum;
end
plot(partialSum(1:50));

% Plotting first 50 terms of Fibonacci Sequence


clear screen
format long
F(1)=1; F(2)=1;
for n=3:50;
F(n)=F(n-1)+F(n-2);
end
plot(F,'r.');
title('Plotting first 50 terms of Fibonacci Sequence') ;
% Convergence/Divergence of sequence through plotting
clear screen
format long
f = inline('power(1+power(n,0.5),(5.*n./3))','n');
a = f(1:50); % to create a sequence
plot(a,'o-');
title('Plotting of sequence ')

% System of ODE

function c_cp_predprey
global beta1 alpha2 c1 c2;

beta1=2.0; alpha2=0.7;
c1=0.01; c2=0.003;
tend = 20; %se the end time to run the simulation
u0 = [400; 80]; % set initial conditions as column vector
[tsol, usol] = ode45(@rhs, [0, tend], u0);
Xsol = usol(:, 1);
Ysol = usol(:, 2);
plot(tsol, Xsol, 'b'); hold on;
plot(tsol, Ysol, 'r--'); hold off;
title('Predatory-Prey Model')
xlabel('time t (years)')
ylabel('Population Density')
legend('Prey','Predators')

function udot = rhs(t, u)


global beta1 alpha2 c1 c2;
X = u(1); Y=u(2);
Xdot = beta1*X - c1*X*Y;
Ydot = -alpha2*Y + c2*X*Y;
udot = [Xdot; Ydot];
% Convergence/Divergence of sequence using Ratio test
% n^10/2^n

clear screen
format long
for n=1:200
ratio(n) = 2*(n./(n+1)).^10;
end
plot(ratio(1:200));

END

You might also like