Hello, I have this section of code I am trying to work with. I need it to be able to accept a matrix form of a second order derrivative. I'll post an example below.
function [derriv_value] = FunctionC(x,y)%Function that contains the derrivative value
derriv_value = [y(2); -9*y(1)+sin(x)]; % y(1) = y , y(2) = v
end
This is my function I am calling into my Runge-Kutta function. It is a second order ODE. I need my Runge-Kutta to be able to accept it, but I am not sure how. I tried altering how the inputs to the equation are formatted but nothing has worked. Here is the Runge-Kutta code.
function [x, yvecb] = MyVec_Function2(F,h,x0,x1,y0,y1)% Note that F function expression is defined via Function Handle: F = @(x, y)(x+y)
% change the function as you desire
% step size (smaller step size gives more accurate solutions)
x = x0:h:x1; % x space
yvecb(:,1)= y0; % Memory allocation
y(y0) = y1; % initial condition
for i=1:(length(x)-1) % i=1:(length(x)-1) % calculation loop
k1 = F(x(i),y(:,i)); k2 = F(x(i)+0.5*h,y(i)+0.5*h*k1); k3 = F((x(i)+0.5*h),(y(:,i)+0.5*h*k2)); k4 = F((x(i)+h),(y(:,i)+k3*h)); y(:,i+1) = y(:,i) + (1/6)*(k1+2*k2+2*k3+k4)*h; % main equation
endfigure, plot(x, y) % To see the solution results
end
Best Answer