while-loop - 了解我的代码在 Scilab 中的问题
问题描述
我正在做一个抛射运动,我需要在位置 x 和 y 之间为各种角度绘制曲线,但 scilab 只显示一个图。我很困惑。我的代码如下
function[H,R,T]=projectile(m,r,h,c,rho,theta,v0,x0,y0,t0)
g=9.8
A=%pi*r^2
k=c*rho*A/2;
i=1
t(i)=t0
x(i)=x0
y(i)=y0
for j=0:5
thetha=theta+j*15;
vx(i)=v0*cos(thetha*%pi/180);
vy(i)=v0*sin(thetha*%pi/180);
while (y(i)>=0)
v=sqrt(vx(i)^2+vy(i)^2);
t(i+1)=t(i)+h;
vx(i+1)=vx(i)-h*(k*v*vx(i)/m);
vy(i+1)=vy(i)-h*(g+k*v*vy(i)/m);
x(i+1)=x(i)+h*vx(i)
y(i+1)=y(i)+h*vy(i)
i=i+1;
end
plot(x(i),y(i),'.');
end
n=i-1
R=x(n)-x(1);
T=t(n);
H=max(y)
endfunction
解决方案
您应该使用向量来提高代码的紧凑性和可读性。这是我对改进(和工作)代码的提议:
function [H,R,T] = projectile(m,r,h,c,rho,theta0,v0,x0,y0,t0)
g = 9.81
A = %pi*r^2
k = c*rho*A/2;
for theta = theta0 + (0:15:75)
v = v0*[cos(theta*%pi/180); sin(theta*%pi/180)];
t = t0
xy = [x0;y0]
i = 1
while xy(2,i) >= 0
t(i+1) = t(i)+h;
v = v + h*([0;-g] - k*norm(v)*v/m);
xy(:,i+1) = xy(:,i) + h*v;
i = i+1;
end
plot(xy(1,:), xy(2,:));
end
R = xy(1,$) - xy(1,1);
T = t($);
H = max(xy(2,:))
endfunction
clf
[H,R,T] = projectile(1,0.1,0.001,2,1000,5,1,0,0,0)
推荐阅读
- angular - 如何编辑 ng-template 以使用我的 HTML?
- jquery - 复选框位于标签顶部而不是标签左侧
- python - 根据该数组中的值将值插入到 numpy 数组中
- ios - 如何在 ArKit iOS 中从 3D 点转换 2D 点
- javascript - 如何使用循环从另一个函数获取数字
- salesforce - 服务预约进度(色度图)未显示在 Field Service Lightning 的 Field Service 屏幕上的甘特图选项卡中
- r - 如何在 Shiny (R) 中管理渲染表中的列中的小数
- ios - 在引导访问模式下,允许设备运行多个应用程序
- spring - 基本身份验证不适用于多个配置
- git - 无法在 SAP 云詹金斯服务器中克隆 Git