Numerical Integration using Euler and Rk method

9 views (last 30 days)
I need help with this please. so in the main file i need to difine time velocity and position, second file i need to solve for the derivative and third file i need to solve the equation using euler or RK method. I have attached the math i worked and some of the sudo code i have.
P(t) = 35sin(25t)
%main file
t0= 0;
t1= 1;
v0 = 0;
s0 = 0;
%getder file <------ My main problem is here, im stocked at trying to find the derivative. I work the math on paper
function xdot = getder(t,x)
x =
a = (1/m)*(25*sin(45*t)-k*s0);
m= .5;
k = 75;
xdot = [t;a];
%solve file
function [t,x] = box solve(x0,tlim,dt,solve_type)
if st == 1 %solve type allow operator to
t = (to:dt:tf).'
L = length(t);
x = xeros([L,2])
x(L,:) = xo.'
for n =1:L
x(n+1,:) = x(n,:)+dt*geter(t(n),x(n,:));
elseif st == 2 %solve using RK methods
k1 =
K2 =
k3 =
k4 =

Answers (1)

MULI
MULI on 5 Jun 2025
The issue in your "getder.m" function is that you are using s0 (initial position) instead of the current state. Replace s0 with x(1) to use the current position during simulation.
function xdot = getder(t, x)
m = 0.5;
k = 75;
s = x(1);
v = x(2);
a = (1/m) * (25 * sin(35 * t) - k * s);
xdot = [v; a];
end
This will properly compute the time derivative using the current state x.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!