How do I use Eulers Explicit method to solve a system of first-order ODEs
12 views (last 30 days)
Show older comments
Hello, I have created a system of first order ODEs from the higher order initial value problem, but now I cannot figure out how to use Matlab to find the solution using Eulers explicit method. I have already used Eulers (implicit I think?) and third order runge Kutta as you can see below but I am lost on how to incorporte the 4 initial values and 4 different first order ODEs. Thank you for your help!!! The picture of the problem statement will be on top followed by the system of first-order ODEs I derived, then my Eulers implicit and runge kutta code and finally the code I am trying to work on right now.


if true
%EULER IMPLICIT METHOD
h = 0.1;
x0 = 0;
u0 = 1;
x_end = 2;
x(1)=x0;
u(1)=u0;
%number of steps
n = (x_end-x0)/h;
for k=1:n
uprime(k+1) = -x(k)*u(k) + exp(-(x(k)^2)/2);
x(k+1) = x(k) + h;
u(k+1) = u(k) + uprime(k+1)*h;
end
%transpose x and u into column vectors
xvalues = x';
uvalues = u';
%%THIRD ORDER RANGE KUTTA METHOD
h = 0.1;
x0 = 0;
u0 = 1;
x_end = 2;
x1(1)=x0;
u1(1)=u0;
x1= 0:h:2;
y1= zeros(1,length(x1));
F = @(x,u) -x.*u + exp(-(x.^2)/2);
for i=1:(length(x1)-1)
k1 = F(x1(i),u1(i));
k2 = F(x1(i)+0.5*h,u1(i)+k1);
k3 = F((x1(i)+0.5*h),(u1(i)-k1+2*k2));
u1(i+1) = u1(i) + (1/6)*(k1+4*k2+k3)*h;
end
u1values = u1';
%%PLOT ALL THREE GRAPHS
exact = @(x) exp(-(x.^2)/2).*(x+1);
plot(x,u, '--b');
xlim([0 2])
hold on
fplot(exact,[x0,x_end], 'r');
hold on
plot(x1,u1, '--g');
legend('euler','exact','runge-kutta')
step = xvalues;
implicit = uvalues;
RungeKutta = u1values;
exact = exact(xvalues);
table(step,implicit,RungeKutta,exact)
end
First try:
if true
%%EULER EXPLICIT METHOD
h = 0.1;
x_end = 2;
x0 = 0;
x0_1 = 0;
x0_2 = 0;
x0_3 = 0;
u0 = 1.1;
u0_1 = -4.7;
u0_2 = 7.9;
u0_3 = -14.3;
u1(1)=u0;
u2(1)=u0_1;
u3(1)=u0_2;
u4(1)=u0_3;
x1(1)=x0;
x2(1)=x0_1;
x3(1)=x0_2;
x4(1)=x0_3;
%number of steps
n = (x_end-x0)/h;
for k=1:n
up1(k+1) = -x1(k)*u1(k) + exp(-(x1(k)^2)/2);
up2(k+1) = -x2(k)*u2(k) + exp(-(x2(k)^2)/2);
up3(k+1) = -x3(k)*u3(k) + exp(-(x3(k)^2)/2);
up4(k+1) = -x4(k)*u4(k) + exp(-(x4(k)^2)/2);
x(k+1) = x1(k) + x2(k) + x3(k) + x4(k) + h;
u(k+1) = u1(k) + u2(k) + u3(k) + u4(k) + up1(k+1)*h + up2(k+1)*h + up3(k+1)*h + up4(k+1)*h;
end
%transpose x and u into column vectors
xvalues = x';
uvalues = u';
end
MOST RECENT CODE:
if true
%%EULER EXPLICIT METHOD
f = @(x,u) -x.*u + exp(-(x.^2)/2);
exact = @(x) exp(-2.*x).*(2-(4.81097.*10^(-16)).*(exp(x))-(exp(3.*x))+(0.1.*exp(5.*x)));
h = 0.1;
xRange = [0,1.5];
initial = [1.1, -4.7, 7.9, -14.3];
steps = xRange(2)/h;
x=zeros(steps+1,1);
x(1) = xRange(1);
h = ( xRange(2) - xRange(1) ) / steps;
u = (initial);
for k = 1 : steps
x(k+1) = x(k) + h;
u(k+1,:) = u(k,:) + h * (feval( f, x(k),(u(k,:))'))';
end
xvalues= x'
uvalues= u'
plot(x,u, '--b');
xlim([0 1.5])
hold on
fplot(exact,[0,1.5], '--r');
legend('euler 0.1','exact');
step = xvalues;
implicit = uvalues;
exact = exact(xvalues);
table(step,implicit,exact)
end
2 Comments
Answers (1)
Torsten
on 18 Apr 2018
This should help:
% function definition F=@(t,u)[u(2); u(3); u(4); -6*u(1)-u(2)+7*u(3)+u(4)]; % initial values U(1:4,1) = [1.1; -4.7; 7.9; -14.3]; % Loop update U(1:4,n+1) = U(1:4,n) + dx*F(t(n),U(1:4,n))
Best wishes
Torsten.
2 Comments
Torsten
on 18 Apr 2018
u is just a formal function parameter ; you can replace it by whatever you like.
Best wishes
Torsten.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!