MATLAB: Runge-kutta

mathematicsrunge kutta

hello i have this equation y''+3y'+5y=1 how can i solve it by programming a runge kutta 4'th order method ? i know how to solve it by using a pen and paper but i can not understand how to programe it please any one can solve to me this problem ? i dont have any idea about how to use ODE and i read the help in matlab but did not understand how to solve this equation please any one can solve this and help me with it ? thanx

Best Answer

Writing a new answer so that I can use markup (MathWorks, please fix this!!). You need a couple of things - first, the time values
t = 0:h:100
You can now work out how many steps you need:
n = numel(t)
Now, we need an array in which to store your results: each column should correspond to a value from your t vector
x = zeros(2, n)
We know the initial condition, so we'll pop that in
x(:, 1) = [0; 0]
Now we need a loop to store the subsequent x values. The basic structure will look like this
for i = 2:n % We've already filled in the first column
k1 = h*f(x(:, i-1));
k2 = ...
k3 = ...
k4 = ...
% Now define the new x vector based on k1, ... , k4
x(:, i) = x(:, i-1) + ...
end
% Plot your results
plot(t, x)
And that should pretty much be all you need