You are on page 1of 3

For 9a:

%Euler's Explicit Method

a = input('Input lower limit a:');


b = input('Input upper limit b:');
h = input('Input step size:');
yo = input('Input initial y value:');

N = 5

%Compute for y(i+1) Using EEM


T = [a:h:b];
Y = zeros(N+1,1);
Y(1) = yo;

for i = 1:N
Y(i+1) = Y(i) + h*(Y(i) + (T(i)).^3);
end

%Plot the graph of y(i+1) and function

xp=a:2:b;
ya = 7*exp(xp) - (xp).^3 - 3*(xp).^2 - 6*(xp) - 6
plot(T,Y,xp,ya)
legend('EEM','Analytical')
hold on

For 9b:

%Given functions
f=@(t) 7*exp(t)-t.^3-3*t.^2-6*t-6;
g={@(t,x) x + t^3, @(t,x) x + t^3 + 3*t^2, @(t,x) x + t^3 + 3*t^2 + 6*t,
@(t,x) x + t^3 + 3*t^2 + 6*t + 6, @(t,x) x + t^3 + 3*t^2 + 6*t + 6};

a = linspace(0,10);
x = linspace(0,10,n+1);
n = 5; %Value of subintervals
h = 2; %Step size (difference bet x values)

t1= [1];

%filling up the array


for k=2:n+1
t1(k) = t1(k-1) + h*g{1}(x(k-1),t1(k-1));
end

%For taylor with order 5


tay5 = [1 0 0 0 0 0]; %Initial values of array

for k = 2:n+1

for m = 1:5
tay5(k) = tay5(k) + g{m}(x(k-1),tay5(k-1))*(h^m)/(factorial(m));
end

end

%Plotting of graph for the three methods

plot(x,tay5,a,f(a),'--',x,t1,':');
xlabel('Interval [a,b]')
legend('Taylor 5','Analytical','EEM or Taylor 1')

For 9c:

a=0;
y0 = 1;
b = 10;
h = 2;
N = 5;

%Modified Euler Method (RK)


T=[a:h:b]
Y = zeros(N+1,1);
Y(1) = y0;

for i=1:N
k1 = Y(i)+(T(i)).^3;
k2 = (Y(i)+k1*h) + (T(i)+h).^3
Y(i+1) = Y(i) + 0.5*h*(k1 + k2)
end

%Midpoint Method (RK)


T = [a:h:b]
W = zeros(N+1,1);
W(1) = y0;
for i=1:N
r1 = W(i)+(T(i)).^3;
r2 = (W(i)+0.5*r1*h) + (T(i)+0.5*h).^3
W(i+1) = W(i) + r2*h
end

xp=a:2:b;
ya = 7*exp(xp) - (xp).^3 - 3*(xp).^2 - 6*(xp) - 6
plot(T,Y,xp,ya,T,W)
legend('Modified Euler','Analytical','Runge-Kutta')

You might also like