Question: Matlab (Runge-Kutta Method): Could someone explain what Y = [0 1; -1 ...] (why 0 1; -1) and u = [0; ...] (why 0 and
Matlab (Runge-Kutta Method):
Could someone explain what Y = [0 1; -1 ...] (why 0 1; -1) and u = [0; ...] (why 0 and only one column) mean in the following code?
(refference: https://www.mathworks.com/matlabcentral/answers/705963-evaluating-a-2nd-order-ode-using-the-runge-kutta-method)
====================================================
% ODE: y''(t) = -e^(3t)*y'(t) - y(t) + (5-2e^(-3t))*e^(-2t) +1
% With initial conditions y(0) = 2 ; y'(0) = -2 where 0 a = 0; b = 1; %n = input('Enter the number of intervals:'); n = 100; h = (b-a)/n; y = [2; -2]; t = 0:h:n*h; for i=1:n k1 = f(t(i),y(:,i)); k2 = f(t(i)+0.5*h,y(:,i)+0.5*k1*h); k3 = f(t(i)+h,y(:,i)-k1*h+2*k2*h); y(:,i+1)= y(:,i)+h/6*(k1+4*k2+k3); %approximated value of the ODE using RK3 end exact = exp(-2*t)+1; disp(max(abs(exact-y(1,:)))) plot(t,y(1,:),'r',t,exact,'b--'),grid xlabel('t'),ylabel('y') legend('RK3','Exact') function fty = f(t,y) %matrix form of the 2nd order ODE Y = [0 1; -1 -exp(-3*t)]*y; % HERE u = [0; (5-2*exp(-3*t))*exp(-2*t)+1]; % HERE fty = Y + u; end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
