2nd order euler method problem

1 visualización (últimos 30 días)
jun seong park
jun seong park el 17 de En. de 2023
Respondida: Jan el 17 de En. de 2023
my brain is boiling
How to solve initial value problem?
x x ̈ +x ̇ ^2+cost=0, x(0)=√6, x ̇ (0)=0
% euler method 2nd order
clc;
clear;
close all;
h=0.1;
t = 0:h:10;
n = numel(size(t));
x = zeros(size(t));
dxdt = zeros(size(t));
dx2dt2 = zeros(size(t));
x(1) = sqrt(6);
dxdt(1) = 0;
plot(t,x);
for i = 1:n-1
dx2dt2(i+1) = dx2dt2(i) + h*f2(t(i),dx2dt2(i));
dxdt(i+1) = dxdt(i) + h*f1(t(i),dxdt(i));
end
function dx2dt2 = f2(t,x,dxdt)
dx2dt2 = -(dxdt^2 + cos(t))/x;
end
function dxdt = f1(t,x,dx2dt2)
dxdt = sqrt(cost(t)-x*dx2dt2);
end
also i made another code to slove same problem
% euler method 2nd order
clc;
clear;
close all;
h=0.1;
t = 0:h:10;
n = numel(size(t));
x = zeros(size(t));
dxdt = zeros(size(t));
dx2dt2 = zeros(size(t));
x(1) = sqrt(6);
dxdt(1) = 0;
f2 = @(t,x,dxdt) -(dxdt^2 + cos(t))/x;
f1 = @(t,x,dx2dt2) sqrt(cost(t)-x*dx2dt2);
for i = 1:n-1
dx2dt2(i+1) = dx2dt2(i) + h*f2(t(i),dx2dt2(i));
dxdt(i+1) = dxdt(i) + h*f1(t(i),dxdt(i));
end
plot(t,x);
thank you

Respuestas (1)

Jan
Jan el 17 de En. de 2023
You have to convert the 2nd order equation to a system of order 1 at first. Accumulating dx2/dt2 is not meaingful.
The 2nd derivative at the point [t, x, dxdt] is:
dx2dt2 = -(dxdt^2 + cos(t)) / x
You use this acceleration to calculate the velocity, and the velocity to get the position in each time step.

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by