ODE Midpoint Method Help
6 views (last 30 days)
Show older comments
Trying to write a function that implements 1-step of ODE Runge Kutta Midpoint Method. Function references another function containing eqaution (not sure if fn calls function correctly).
The function will not solve for k1 and k1 therfore i can not solve for y(i+1).
Please help.
% Implements 1-step of Mid-point Method
function xnew = midpointstep_CGill(tint, x, h, fn)
global ode
syms x(t) dx(t)
h = .01; % Step Size
tint = 0:h:25; % Time interval [mint, maxt]
x(0) == 0; % Inital Condition
dx(0) == 30; % Inital Condition
fn = ode3dp_CGill(ode);
for i = 1:(length(tint)-1)
k1 = fn(x(i),dx(i));
k2 = fn(x(i) + 0.5*h, dx(i) + 0.5*h*k1);
y(i+1) = y(i) + k2*h;
end
end
Calls the following function
% Computes ODE values
function ode = ode3dp_CGill(t,x)
global ode
syms x(t) dx(t) m b k F(t)
F = -sin(4*pi*t);
eq = m*dx(t) + b*x(t) + k*x == F;
eq = subs(eq, [b], [2*(k*m)^(1/2)]);
ode = collect(eq, m)/m;
end
1 Comment
James Tursa
on 13 Mar 2020
What is the ODE you are trying to solve? You've got b*x(t) and k*x in your equation so I am not sure what exactly the ODE is.
Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations 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!