0

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. Whenever I try to run the script for different time steps(h), I keep getting the error:

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;
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • On what line do you get the error? It means that you are trying to assign a number of elements to an unequal number of storage spaces, i.e. your left hand side is not equal in side to the right hand side. – Adriaan Nov 01 '15 at 19:39
  • The error happens at: xout3(j,:)=xout2 – scoobydoo9000 Nov 01 '15 at 19:43
  • I think the array `xout2` is shorter or longer than the column width of `xout3`. Check this by doing `size(xout2)` and enlarger or shorten your arrays accordingly – Adriaan Nov 01 '15 at 19:45
  • So xout2 is longer than the column width of xout3m but I don't know how to fix this. For each iteration xout2 and xout3 should have the same column width – scoobydoo9000 Nov 01 '15 at 20:06
  • I suggest moving that global `h` into the function's input variable. And make sure that `xout3` is not already defined with an incompatible size: run `clear all` or at least `clear xout3` before running your code. – Andras Deak -- Слава Україні Nov 01 '15 at 20:14
  • @AndrasDeak I just fixed that, see the answer. – Adriaan Nov 01 '15 at 20:18
  • I tried putting global h as the input variable instead and I used clear xout3 but xout2 is still longer than the column width of xout3 – scoobydoo9000 Nov 01 '15 at 20:36
  • @scoobydoo9000 please see my answer, save the function as `walls_euler_method2e.m` and then run your script (name doesn't matter) exactly as I have written it. Thus two separate files. Yes the column widths are different, as I explained, and circumvented that using cells. – Adriaan Nov 02 '15 at 10:01

1 Answers1

1

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. 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. Also try and refrain from using global variables, it is better to pass your variables to the actual function, see the modified code:

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]
Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75