Question 3 The exponential function e has the unique property that its derivativ
ID: 3282059 • Letter: Q
Question
Question 3 The exponential function e has the unique property that its derivative is the same function as itself Indeed, this is one way to define the exponential function and Euler's number e. In this part, use Euler's method and Runge-Kutta method (2nd and 4th order) to estimate e from the fact that e y(1) if y (x) is the solution of the initial value problem dx and y(O)1 a) Apply n steps of each of these three methods to estimate e, for n-1,2,...10. (That is, use a step 0; and size of h-) For example, for n - 1 for Euler's method, you will use just 1 step to reach x1 fromx since dy o) 1, Euler's method would would take you to y(1) 2 in one step, which would be your estimate of e (not a good estimate). Plot the estimates of e by each method with respect to n on the same plot and add a legend to the figure to show which is which method. How quickly do they converge to the correct value of e? b) Do the same for n 1,2,,100 and plot. Has Euler's method converged to the correct value of e yet? c) Find the number of steps necessary for each method to estimate e within a maximum error of 0.001Explanation / Answer
a) MATLAB code for each method is listed below
clc;
clear all;
n=[1:1:10];
%% Runge-Kutta-4th order Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
k1=y(i);
k2=y(i)+0.5*k1;
k3=y(i)+0.5*k2;
k4=y(i)+k3;
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4)*h(j);
end;
e(j)=y(length(t));
end;
plot(n,e);hold on;
%% Runge-Kutta-2nd order Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
k1=y(i);
k2=y(i)+0.5*k1;
y(i+1)=y(i)+k2*h(j);
end;
e(j)=y(length(t));
end;
plot(n,e,'r');
%% Explicit Euler Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
y(i+1)=y(i)+y(i)*h(j);
end;
e(j)=y(length(t));
end;
plot(n,e,'m');
xlabel('n');
ylabel('e');
legend('R-K 4th order','R-K 2nd order','Explicit Euler');
(b)
clc;
clear all;
n=[1:1:100];
%% Runge-Kutta-4th order Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
k1=y(i);
k2=y(i)+0.5*k1;
k3=y(i)+0.5*k2;
k4=y(i)+k3;
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4)*h(j);
end;
e(j)=y(length(t));
end;
plot(n,e);hold on;
%% Runge-Kutta-2nd order Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
k1=y(i);
k2=y(i)+0.5*k1;
y(i+1)=y(i)+k2*h(j);
end;
e(j)=y(length(t));
end;
plot(n,e,'r');
%% Explicit Euler Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
y(i+1)=y(i)+y(i)*h(j);
end;
e(j)=y(length(t));
end;
plot(n,e,'m');
xlabel('n');
ylabel('e');
legend('R-K 4th order','R-K 2nd order','Explicit Euler');
(c)
clc;
clear all;
n=[1:1:100];
%% Runge-Kutta-4th order Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
k1=y(i)
k2=y(i)+0.5*k1
k3=y(i)+0.5*k2
k4=y(i)+k3
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4)*h(j);
end;
e(j)=y(length(t));
Error(j)=abs(e(j)-exp(1));
if Error(j)<=0.001
disp('Number of steps in RK 4th order for error within 0.001 is'); j
end;
end;
plot(n,e);hold on;
%% Runge-Kutta-2nd order Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
k1=y(i);
k2=y(i)+0.5*k1;
y(i+1)=y(i)+k2*h(j);
end;
e(j)=y(length(t));
Error(j)=abs(e(j)-exp(1));
if Error(j)<=0.001
disp('Number of steps in RK 2nd order for error within 0.001 is'); j
end;
end;
plot(n,e,'r');
%% Explicit Euler Method
for j=1:length(n)
y(1)=1;
h(j)=1/n(j);
t=[0:h(j):1];
for i=1:length(t)
y(i+1)=y(i)+y(i)*h(j);
end;
e(j)=y(length(t));
Error(j)=abs(e(j)-exp(1));
if Error(j)<=0.001
disp('Number of steps in Euler method for error within 0.001 is'); j
end;
end;
plot(n,e,'m');
xlabel('n');
ylabel('e');
legend('R-K 4th order','R-K 2nd order','Explicit Euler');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.