Numerical Integration using Euler and Rk method

조회 수: 2 (최근 30일)
fernando piedra
fernando piedra 2020년 11월 12일
답변: MULI 2025년 6월 5일
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 =

답변 (1개)

MULI
MULI 2025년 6월 5일
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.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by