2nd order euler method problem
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Jan
on 17 Jan 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.
0 Comments
See Also
Categories
Find more on Special Functions 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!