I got an error when attempting to plot errors E1 and E2. I used hold on command but still I got the same error
function RungeKuttaMethod%example of Runge-Kutta method of order 4.
%y' = y - t^2 + 1.
a = 0; %time interval we are solving over
b = 2;N = 4; %number of steps
t = zeros(1, N); %preallocate memory for these variables - this is preferred but is not required to work%
w = zeros(1, N);w(1) = 0.5; %initial value of w.
h = (b - a)/N; %step size
t(1) = a; %initial vlaue for time.
syms QF = @(t, y) y - t^2 + 1; %thing we are solving.
clcfprintf ('x(n) \t \t \t y \t \t \t \t y(n) \t \t \t E1(n) \n') % data table header
for i = 1:N K1 = h*(F(t(i), w(i))); %this is the RungKutta Method.
K2 = h*(F(t(i) + 0.5*h, w(i) + 0.5*K1));K3 = h*(F(t(i) + 0.5*h, w(i) + 0.5*K2));K4 = h*(F(t(i) + h, w(i) + K3));w(i+1) = w(i) + (K1 + 2*K2 + 2*K3 + K4)/6; t(i+1) = a + i*h;Q(i+1) = t(i+1)^2 + 2*t(i+1) +1 -(0.5)*exp(t(i+1)); %actualy solution we ca use to compare between both methods%
E1(i+1)= abs(w(i+1) - Q(i+1));fprintf('%f \t \t %f \t \t %f \t \t%f \t \t \n',t(i+1),Q(i+1),w(i+1), E1 (i+1))end% plot(t, E1)
N = 40;t = zeros(1, N); %preallocate memory for these variables - this is preferred but is not required to work%w = zeros(1, N);w(1) = 0.5; %initial value of w.h = (b - a)/N; %step sizet(1) = a; %initial vlaue for time.syms QF = @(t, y) y - t^2 + 1;fprintf ('x(n) \t \t \t y \t \t \t \t y(n) \t \t \t E2(n) \n') % data table headerfor i = 1:N K1 = h*(F(t(i), w(i))); %this is the RungKutta Method.K2 = h*(F(t(i) + 0.5*h, w(i) + 0.5*K1));K3 = h*(F(t(i) + 0.5*h, w(i) + 0.5*K2));K4 = h*(F(t(i) + h, w(i) + K3));w(i+1) = w(i) + (K1 + 2*K2 + 2*K3 + K4)/6; t(i+1) = a + i*h;Q(i+1) = t(i+1)^2 + 2*t(i+1) +1 -(0.5)*exp(t(i+1)); %actualy solution we ca use to compare between both methods%E2(i+1)= abs(w(i+1) - Q(i+1));fprintf('%f \t \t %f \t \t %f \t \t%f \t \t \n',t(i+1),Q(i+1),w(i+1), E2 (i+1))endhold onplot(t, E1, t, E2)
Best Answer