Runge-Kutta method for 2x2 IVP?
Afficher commentaires plus anciens
I have a code written to input a vector into my RK4 file, but I keep getting all kinds of errors, including "Undefined function 'mtimes' for input arguments of type 'cell'." my code is as follows
function [Y, t] = RK42d(f, t0, T, y0, N)
%the input of f and y0 must both be vectors
%composed of two fuctions and their respective
%intial conditions
%the vector Y will return a vector as well
h = (T - t0)/(N - 1); %Calculate and store the step size
Y = zeros(2,N); %Initialize the X and Y vector
t = linspace(t0,T,N); % A vector to store the time values
Y(:,1) = y0; % Start Y vector at the intial values.
for i = 1:(N-1)
k1 = f(t(i),Y(i));
k2 = f{t(i)+0.5*h, Y(i)+0.5*h*k1};
k3 = f{t(i)+0.5*h, Y(i)+0.5*h*k2};
k4 = f{t(i)+h, Y(i)+h*k3};
Y{i+1} = Y{i} + (h/6)*(k1+ 2.*k2 + 2*k3 + k4);
%Update approximation
end
%%%END FILE %%%
I've tried running it using another .m file, using
f = {@(t,y)(y(1) - 4.*y(2)); @(t,y)(-y(1) + y(2))};
y0 = [1;0];
t0 = 0;
T = 1;
N = 11;
RK42d(@(t,y)f, t0, T, y0, N)
but for some reason it won't work. Any tips?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Ordinary Differential Equations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!