How do I solve these differential equations using a while loop?
이전 댓글 표시

댓글 수: 1
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.
채택된 답변
추가 답변 (1개)
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
Abraham Boayue
2018년 3월 12일
Hey Christopher Here is a model solution to your equations. I could not test the code since you did not provide any parameter. The coding was straight forward using a for loop as Roger suggested. I hope this will help you get started with your coding. Best of lucks!
Christopher Maraj
2018년 3월 12일
Abraham Boayue
2018년 3월 12일
Hey Chris, what are these equations by the way? Do you have access to the actual differential equations? Can you post the values of the parameters as well as the initial and boundary conditions? I would like to see what they represent by running the code.
Christopher Maraj
2018년 3월 12일
Christopher Maraj
2018년 3월 12일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

