Info

此问题已关闭。 请重新打开它进行编辑或回答。

Can someone please check my code, not quite sure where im going wrong

2 次查看(过去 30 天)
clc
clear
%--------------------------------------------------------------------------------------------------------%
p = 1.2; %variables given to me in question
g = 9.81;
m = 77;
c = 1;
a1 = .7;
a2 = 50;
t1 = 50;
t2 = 70;
v0 = 50;
h0 = 2.5;
T = .005;
tmid = (t1+t2)/2; %formula gives to find what t mid is
%--------------------------------------------------------------------------------------------------------%
A = @(t) 1/2*(a2-a1)*tanh(10.*((t-tmid)/((t2-t1))))+(1/2*(a2+a1));
T1 = 0:1:120; %used for plotting x axis
D = @(T1)(p*c.*(A(T1)))/2*m; %drag constant
plot(T1, A(T1), 'b-', 50, .7, 'ro', 70, 50, 'ro') %t1 occurs at 50 seconds in, the area is .7 so I used that as a y co-ordinate and 50 as my x, marking that spot with a red circle.
axis([0 120, .6 70]);
xlabel('Time')
ylabel('Area')
title('Area over Time')
%--------------------------------------------------------------------------------------------------------%
x(1) = 0;, vx(1) = v0;, y(1) = 0;, vy(1) = 0; %initial variables used to start the loop
for n=1 %initialize time index
while 1 %forever while loop
if y(n) > h0, break; end % break when ground is reached
tn = (n-1)*T; %nth time instant in seconds
t(n) = tn; %build time vector, needed for plotting
Dn = D(n).*(tn); %drag constant in time tn
v = sqrt(vx(n)^2+vy(n)^2); %problem tells me to write "no need to save V in an array"- not sure what this means
ax(n) = -Dn * vx(n)*v; %horizontal acceleration
v(n) = sqrt(vx(n)^2+vy(n)^2);
ay(n)= -(D(tn)) * vy(n)*v(n)+g; ;
x(n+1) = x(n)+T*vx(n);
vx(n+1)= vx(n)+T*ax(n);
y(n+1)=y(n)+T*vy(n);
vy(n+1)= vy(n)+T*ay(n);
n = n+1; %updates the index to use N+1, so second round will use 2 for N, as N is defined as 1, N= 1+1, whill use 2 for N in next loop and so on.
end
end
%--------------------------------------------------------------------------------------------------------%
Vc_1 = sqrt((2*m*g)/p*c*A(30));
Vc_2 = sqrt((2*m*g)/p*c*A(80));
Vc_3 = sqrt((2*m*g)/p*c*a1);
Vc_4 = sqrt((2*m*g)/p*c*a2);
  8 个评论
Image Analyst
Image Analyst 2020-9-23
Saving the m-file won't prevent the script from throwing an error. If it's not saved, it will actually ask you to save the file before it runs even the first line of the script. So it can't crash before it's saved. However, we're glad its working (for whatever reason) now.

回答(0 个)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by