going from explicit Euler code to midpoint method.

8 次查看(过去 30 天)
I am using the current code to solve an ode for euler method:
%EXPLICIT EULER
clear;
%INPUT VARIABLES
a = 2; %FIRST VALUE OF X
b = 6; %SECOND VALUE OF X
N = 4; %INTERVAL SIZE
y_initial = -6.5; % INTIAL VALUE OF Y
%FUNCTION TO SOLVE ODE USING EULER METHOD
[x,y] = ode_euler(@Fun_ODE, a,b,N,y_initial);
%PLOTTING
a = 2;
b = 6;
xp = a:0.1:b;
yp = 3./xp-2*xp.^2;
figure
hold on
plot(x,y, '--r')
plot(xp,yp,'k')
xlabel('x'); ylabel('y');
legend("Euler solution", "Exact solution");
function [x,y] = ode_euler(ODE,a,b,N,y_initial)
% OUTPUT VARIABLES
% x = A vector with the x coordinate of the solution points
% y = A vector with the y coordinate of the solution points
x(1) = a; y(1) = y_initial;
h = (b-a)/N;
for i = 1:N
x(i+1) = x(i) + h;
y(i+1) = y(i) + ODE(x(i),y(i))*h;
end
end
function dydx = Fun_ODE(x,y)
dydx = -(y/x)-6*x;
end
i know somewhere in the function part of the code i need to add a way to find the derivative at the midpoint in order to get midpoint curve and plot it but im not sure where to add it or how i can change the code i have now to get it.

回答(1 个)

Nipun
Nipun 2024-4-16
Hi Aleily,
I understand that you intend to modify your existing code to implement the "midpoint" Euler method, also known as the "modified" Euler method.
To plot the solution along with the Explicit Euler function and exact solutions, I recommend adjusting the "ode_euler" function. The midpoint method involves taking an Euler step to estimate the midpoint, then using the derivative at the midpoint to take another whole step.
For your reference, I am attaching the modified code that supports the Midpoint method and plots the solution:
% EXPLICIT EULER AND MIDPOINT METHOD
clear;
% INPUT VARIABLES
a = 2; % FIRST VALUE OF X
b = 6; % SECOND VALUE OF X
N = 4; % INTERVAL SIZE
y_initial = -6.5; % INITIAL VALUE OF Y
% FUNCTION TO SOLVE ODE USING EULER METHOD
[x_euler,y_euler] = ode_euler(@Fun_ODE, a, b, N, y_initial);
% FUNCTION TO SOLVE ODE USING MIDPOINT METHOD
[x_midpoint,y_midpoint] = ode_midpoint(@Fun_ODE, a, b, N, y_initial);
% PLOTTING
xp = a:0.1:b;
yp = 3./xp - 2*xp.^2; % Exact solution
figure
hold on
plot(x_euler, y_euler, '--r', 'LineWidth', 1.5)
plot(x_midpoint, y_midpoint, '--b', 'LineWidth', 1.5)
plot(xp, yp, 'k', 'LineWidth', 1)
xlabel('x'); ylabel('y');
legend("Euler solution", "Midpoint solution", "Exact solution");
title('Comparison of Euler, Midpoint, and Exact Solutions');
grid on
% Euler method function
function [x,y] = ode_euler(ODE, a, b, N, y_initial)
x(1) = a; y(1) = y_initial;
h = (b-a)/N;
for i = 1:N
x(i+1) = x(i) + h;
y(i+1) = y(i) + ODE(x(i),y(i))*h;
end
end
% Midpoint method function
function [x,y] = ode_midpoint(ODE, a, b, N, y_initial)
x(1) = a; y(1) = y_initial;
h = (b-a)/N;
for i = 1:N
x(i+1) = x(i) + h;
% First, take a half-step
y_half = y(i) + ODE(x(i), y(i)) * (h/2);
% Then, use the derivative at the midpoint to take a full step
y(i+1) = y(i) + ODE(x(i) + h/2, y_half) * h;
end
end
% ODE function
function dydx = Fun_ODE(x, y)
dydx = -(y/x) - 6*x;
end
To summarize changes in the new code, a new function "ode_midpoint" is introduced. This function implements the Midpoint method. The main differences from the initial approach are as under:
  1. A half-step forward is taken using the Euler method to estimate the midpoint.
  2. The derivative at this midpoint ("y_half") is then used to take a full step from the original point.
Additionally, the plotting section is updated to include the solution from the Midpoint method. This allows for a direct comparison between the Explicit Euler method, the Midpoint method, and the exact solution.
Hope this helps.
Regards,
Nipun

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by