Convert higher order to first order system
10 views (last 30 days)
Show older comments
I'm trying to solve the higher order ode by using RK4 method. Here is the code I have so far.
function [y] = rk4_high_ode(a,t0,n,h,f)
%f = @(t,y) 2*y(2)-y(3)+2*y(4);
% Do I need to define y(2) derivative first?
F=@(t,y)[y(2:end);f]; % Convert the higher order to the 1st order system
% update time
k1=F(t(i) ,y(i) );
k2=F(t(i)+h ,y(i)+h*k1 );
And this is the test program:
% test y=sin(t)
% y^(4) = 2*y'-y"+2*y^(3)
t0 = 0.1; n = 100; h = 1e-2;
a = [sin(t0) cos(t0) -sin(t0) -cos(t0)]';
f = @(t,y) 2*y(2)-y(3)+2*y(4);
y = rk4_high_ode(a,t0,n,h,f);
ye = sin(t0+n*h);
error2 = abs(y-ye)
There is an error " Undefined function or variable 'y'". Does that mean I need to define y(2) first? or something else.
Torsten on 17 May 2017
You will have to define
f=@(t,y) [y(2) y(3) y(4) 2*y(2)-y(3)+2*y(4)];
Additionally note that "rk4_high_ode" must be modified because at the moment, it is only capable of solving a single ODE.
Find more on Programming 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!