简体   繁体   English

下标分配尺寸不匹配错误

[英]Subscripted Assignment Dimension Mismatch Error

I'm trying to model the collisions of a ball and its x and y position over a certain period of time in a 500 by 500 box. 我正在尝试在500 x 500的盒子中模拟球及其x和y位置在特定时间内的碰撞。 Whenever I try to run the script for different time steps( h ), I keep getting the error: 每当我尝试以不同的时间步长( h )运行脚本时,都会不断出现错误:

Subscripted assignment dimension mismatch. 下标分配尺寸不匹配。

global h
h_vector=[0.1 0.01 0.001];
t=0:h:500;

for j=1:length(h_vector)
h=h_vector(j);
[xout2,yout2]=walls_euler_method2e;
xout3(j,:)=xout2;
yout3(j,:)=yout2;
end
xout3;
yout3;

function [xout2,yout2]=walls_euler_method2e
global h
f1=5;%dx/dt
f2=-3;%dy/dt
x(1)=0;
y(1)=0;
t=0:h:500;
r=5;%radius of ball
hit_vertical_wall_left_first=0;
hit_horizontal_wall_down_first=0;
vertical_wall_left=250;
vertical_wall_right=-250;
horizontal_wall_up=250;
horizontal_wall_down=-250;
for i=2:length(t)
x(i)=x(i-1)+h.*f1;
y(i)=y(i-1)+h.*f2;
if x(i)==vertical_wall_left-r
    f1=-f1;
hit_vertical_wall_left_first=1;
elseif x(1)==vertical_wall_right+r&&hit_vertical_wall_left_first==1;
    f1=-f1;
else
    fl=f1;

 if y(i)==horizontal_wall_down+r
     f2=-f2;
     hit_horizontal_wall_down_first=1;

 elseif y(i)==horizontal_wall_up-r&&hit_horizontal_wall_down_first==1;
      f2=-f2;
 else
     f2=f2;
 end
end
end

xout2=x;
yout2=y;

Simple fix: use cell arrays: 简单修复:使用单元格数组:

xout3{j} = xout2;
yout3{j} = yout2;

You define t in steps of h , and since h becomes smaller, t increases in length, thus also your xout2 . 您以h为步长定义t ,并且由于h变小,所以t的长度增加,因此xout2xout2 Cell arrays allow for dissimilar sized matrices, and you call them with curly braces instead of round ones. 单元格数组允许使用大小不同的矩阵,您可以使用花括号而不是圆形括号来称呼它们。

Note that it is bad practise to use i or j as a variable . 请注意,将ij用作变量不好的做法 Also try and refrain from using global variables, it is better to pass your variables to the actual function, see the modified code: 还要避免使用global变量,最好将变量传递给实际函数,请参见修改后的代码:

function [xout2,yout2]=walls_euler_method2e(h)
f1=5;%dx/dt
f2=-3;%dy/dt
x(1)=0;
y(1)=0;
t=0:h:500;
r=5;%radius of ball
hit_vertical_wall_left_first=0;
hit_horizontal_wall_down_first=0;
vertical_wall_left=250;
vertical_wall_right=-250;
horizontal_wall_up=250;
horizontal_wall_down=-250;
for ii=2:length(t)
    x(ii)=x(ii-1)+h.*f1;
    y(ii)=y(ii-1)+h.*f2;
    if x(i)==vertical_wall_left-r
        f1=-f1;
        hit_vertical_wall_left_first=1;
    elseif x(1)==vertical_wall_right+r&&hit_vertical_wall_left_first==1;
        f1=-f1;
    else
        fl=f1;

        if y(i)==horizontal_wall_down+r
            f2=-f2;
            hit_horizontal_wall_down_first=1;

        elseif y(ii)==horizontal_wall_up-r&&hit_horizontal_wall_down_first==1;
            f2=-f2;
        else
            f2=f2;
        end
    end
end

xout2=x;
yout2=y;
end

Script: 脚本:

h_vector=[0.1 0.01 0.001];
for jj=1:length(h_vector)
    t = 0:h_vector(jj):500;
    [xout2,yout2]=walls_euler_method2e(h_vector(jj));
    xout3{jj}=xout2;
    yout3{jj}=yout2;
end

Results in: 结果是:

xout3 = 
    [1x5001 double]    [1x50001 double]    [1x500001 double]
yout3 = 
    [1x5001 double]    [1x50001 double]    [1x500001 double]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM