2nd order euler method problem
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Jan
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Special Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!