How to make a function that uses Runge-Kutta Method
Show older comments
Hello, I am trying to create a function that can take in a function and solve it using Runge-Kutta's method. For example, I should be able to input dy/dx = x+y , y(0) = 1 and get an answer from the funtion. I've been working with this equation for a while, I just cannnot figure out how to format this into a function. Here is what I have.
function [OutputX,OutputY] = FunctionBeta_Executor(h,x,y,FunctionA)
h=1.5; % step size
x = 0:h:3; % Calculates upto y(3)
y = zeros(1,length(x));
y(1) = 5; % initial condition
FunctionA = F_xy; % change the function as you desire
for i=1:(length(x)-1)
i=1:(length(x)-1) % calculation loop
k1 = F_xy(x(i),y(i));
k2 = F_xy(x(i)+0.5*h,y(i)+0.5*h*k1);
k3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k2));
k4 = F_xy((x(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k1+2*k2+2*k3+k4)*h; % main equation
end
end
% For example, FunctionA might be dy/dx = x+y , with inital conditions y(0)=1.
Accepted Answer
More Answers (0)
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!