In: Advanced Math
Let M be the integer corresponding to the first letter of your last name. For example, if your last name begins with "A", M=1 and if your last name begins with "Z", M=26.
Let k=1/M and consider the logistic equation dy/dt = k y (M - y).
Construct a single figure including
Print on 8.5x11 paper in landscape format.
First letter of my last name is P.
%%Matlab code for ode solution and direction field
clear all
close all
%First letter of my last name is P
M=16;
k=1/M;
%function to be solved
f=@(t,y) (1/16).*y.*(16-y);
t_ini=-3;
t_end=3;
%Plotting Direction field
figure(1)
dirrfield(f,-3:0.2:3,-M/2:1:3*M/2)
hold on
%all initial condition
y0=[5*M/4 M M/2 0 -M/4];
%all time steps
tinit=0;tend=3; h=0.1;
%plot for different initial velocity
for i=1:length(y0)
[ts,ys]=Euler(f,tinit,y0(i),tend,h);
plot(ts,ys,'b','Linewidth',2)
fprintf('\tUsing Euler methid For y(0)=%f , y(%2.2f)
is %f\n',y0(i),ts(end),ys(end))
end
hold off
title('phase potrait plot')
xlabel('time in sec.')
ylabel('Concentration ')
%%Matlab function for direction filed
function dirrfield(f,tval,yval)
% dirfield(f, t1:dt:t2, y1:dy:y2)
%
% plot direction field for first order ODE y' =
f(t,y)
% using t-values from t1 to t2 with spacing of dt
% using y-values from y1 to t2 with spacing of dy
%
% f is an @ function, or an inline function,
% or the name of an m-file with
quotes.
%
% Example: y' = -y^2 + t
% Show direction field for t in [-1,3], y in [-2,2],
use
% spacing of .2 for both t and y:
%
% f = @(t,y) -y^2+t
% dirfield(f, -1:.2:3, -2:.2:2)
[tm,ym]=meshgrid(tval,yval);
dt = tval(2) - tval(1);
dy = yval(2) - yval(1);
fv = vectorize(f);
if isa(f,'function_handle')
fv = eval(fv);
end
yp=feval(fv,tm,ym);
s = 1./max(1/dt,abs(yp)./dy)*0.75;
h = ishold;
quiver(tval,yval,s,s.*yp,0,'r'); hold on;
%quiver(tval,yval,-s,-s.*yp,0,'r');
if h
hold on
else
hold off
end
axis([tval(1)-dt/2,tval(end)+dt/2,yval(1)-dy/2,yval(end)+dy/2])
end
function [t_euler,y_euler]=Euler(f,tinit,yinit,tend,h)
%Euler method
% h amount of intervals
t=tinit; % initial
t
y=yinit; % initial
y
t_eval=tend; % at what
point we have to evaluate
n=(t_eval-t)/h; % Number of steps
t_euler(1)=t;
y_euler(1)=y;
for i=1:n
%Eular Steps
m=double(f(t,y));
t=t+h;
y=y+h*m;
t_euler(i+1)=t;
y_euler(i+1)=y;
end
end
%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%