2nd order euler method problem

1 次查看(过去 30 天)
jun seong park
jun seong park 2023-1-17
回答: Jan 2023-1-17
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

回答(1 个)

Jan
Jan 2023-1-17
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.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by