Euler method error and step size code

6 views (last 30 days)
Priyadarshini Challa
Priyadarshini Challa on 13 Jan 2021
Answered: Aashray on 30 Jun 2025
%Program 6.1 Euler?s Method for Solving Initial Value Problems
%Use with ydot.m to evaluate rhs of differential equation
% Input: interval inter, initial value y0, number of steps n
% Output: time t, solution y
% Example usage: euler([0 1],1,10);
function [t,y]=euler2(inter,y0,h)
inter = [0,1];
y0 = 1;
n = 10;
t(1)=inter(1); y(1)=y0;
%step size
h=(inter(2)-inter(1))/n;
for i=1:n
%establising t-axis with equal step size h
t(i+1)=t(i)+h;
y(i+1)=eulerstep(t(i),y(i),h);
end
%Solution of the IVP
yt = @(t) 3.*exp(t.^2/2)-t.^2-2;
close all
plot(t,y)
hold on
fplot(yt,[0 1])
xlabel('t')
ylabel('y')
legend('Approximate solution','Original solution')
%setting up the Euler Method
function y=eulerstep(t,y,h)
%one step of Euler's Method
%Input: current time t, current value y, stepsize h
%Output: approximate solution value at time t
y=y+h.*ydot(t,y);
end
function z=ydot(t,y)
%right-hand side of differential equation
z=t.*y+t.^3;
end
end
I have this code to plot the approximate and exact solution on a graph but I need to add a for loop to plot another graph that shows step size h and error which is difference between exact and approx solution. For some reason, I can't seem to plot it. Please help me plot it using loglog command

Answers (1)

Aashray
Aashray on 30 Jun 2025
I understand that you are trying to visualize how the error behaves with respect to the step size when using the Euler method.
I extended your original code by making the following additions:
  1. A loop over different step sizes (n values like 5, 10, 20, ...).
  2. Computation of the global error at the final time point (t = 1) for each step size.
  3. A log-log plot to visualize the relationship between the step size (h) and the error.
You can find the code below for better understanding of the changes:
clc; clear; close all;
% Define the exact solution function for your reference
yt = @(t) 3.*exp(t.^2/2) - t.^2 - 2;
% Interval and initial condition calculations
inter = [0, 1];
y0 = 1;
% Vector of different n (number of steps)
n_values = [5, 10, 20, 40, 80, 160, 320];
h_values = zeros(size(n_values));
error_values = zeros(size(n_values));
% Now, we'll iterate over different step sizes
for k = 1:length(n_values)
n = n_values(k);
h = (inter(2) - inter(1)) / n;
h_values(k) = h;
% using Euler method
t(1) = inter(1);
y(1) = y0;
for i = 1:n
t(i+1) = t(i) + h;
y(i+1) = y(i) + h * ydot(t(i), y(i));
end
% Compute error at final point
exact = yt(inter(2));
approx = y(end);
error_values(k) = abs(approx - exact);
end
% Plot error vs step size on log-log scale
figure;
loglog(h_values, error_values, 'o-r', 'LineWidth', 2, 'MarkerSize', 8);
grid on;
xlabel('Step size (h)');
ylabel('Error at t = 1');
title('Error vs Step Size (Euler Method)');
legend('Error');
% Plot approximate vs exact solution for the finest step
figure;
plot(t, y, '-', 'DisplayName', 'Euler Approximation');
hold on;
fplot(yt, [inter(1), inter(2)], 'k--', 'DisplayName', 'Exact Solution');
xlabel('t');
ylabel('y');
title('Euler Approximation vs Exact Solution');
legend;
grid on;
function z = ydot(t, y)
z = t .* y + t.^3;
end
I am also attaching the documentation links of the functions used for reference:
  1. loglog”: for plotting data on a logarithmic scale for both x and y axes. https://www.mathworks.com/help/matlab/ref/loglog.html
  2. abs”: for computing the absolute values. https://www.mathworks.com/help/matlab/ref/double.abs.html
  3. legend”: for adding legends to the plots. https://www.mathworks.com/help/matlab/ref/legend.html

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!