How do I solve these differential equations using a while loop?

2 次查看(过去 30 天)
  1 个评论
Roger Stafford
Roger Stafford 2018-3-11
编辑:Roger Stafford 2018-3-11
If you want to allow delta t to approach zero as a limit, you can solve these equations using one of the ode functions. The first equation, for example, would have the form:
dU/dt = k1-k2*X./((X.^2+Y.^2+Z.^2).^(3/2))
On the other hand if you wish to solve them using delta t as a fixed nonzero value, then do so with a for-loop to provide the iteration, not a while-loop. Just carry out the operations you have given here within the for-loop at each step going from the n-th values to the n+1-st values.

请先登录,再进行评论。

采纳的回答

Abraham Boayue
Abraham Boayue 2018-3-12
i = 1;
while i <= n-2
i = i +1;
% write all your code
% here. This will produce
% the same results as the
% for loop.
end

更多回答(1 个)

Abraham Boayue
Abraham Boayue 2018-3-12
clear variables
close all
% Define parameters
dt = dt;
t = t0:dt:tf;
n = length(t);
m = m;
thx = thx;
thy = thy;
thz = thz;
G = G;
Me = Me;
% Initializations Initial conditions Boundary conditions
u = zeros(1,n); u(1) = u0; u(n) = un;
v = u; v(1) = v0; v (n)= vn;
w = v; w(1) = w0; w(n) = wn;
x = w; x(1) = x0; x(n) = xn;
y = x; y(1) = y0; y(n) = yn;
z = y; z(1) = z0; z(n) = zn;
for i = 2: n-1
u(i+1) = u(i) + (thx/m - G*Me*(x(i)/(x(i)^2 +y(i)^2 +z(i)^2)^(3/2)))*dt;
v(i+1) = v(i) + (thy/m - G*Me*(y(i)/(x(i)^2 +y(i)^2 +z(i)^2)^(3/2)))*dt;
w(i+1) = w(i) + (thz/m - G*Me*(z(i)/(x(i)^2 +y(i)^2 +z(i)^2)^(3/2)))*dt;
x(i) = x(i) + u(i+1)*dt;
y(i) = y(i) + v(i+1)*dt;
z(i) = z(i) + w(i+1)*dt;
end
figure;
plot(t,u,'linewidth',2);
hold on
plot(t,v,'linewidth',2);
plot(t,w,'linewidth',2);
plot(t,x,'linewidth',2);
plot(t,y,'linewidth',2);
plot(t,z,'linewidth',2);
a = ylabel('Pressure');
set(a,'Fontsize',14);
a = xlabel('x');
set(a,'Fontsize',14);
a=title(['Solution to system of ode - dt = ' num2str(dt)]);
legend('u', 'v','w','x','y','z')
xlim([0 1]);
set(a,'Fontsize',16);
grid;
  5 个评论
Christopher Maraj
Christopher Maraj 2018-3-12
The equations are for a project I'm working on about modelling orbits for 6 satellites.

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by