I'm simulating a bird bouncing around a room, and i am trying to have it end the program whe it hits the "window" and "escapes". To start i'm just trying to end the program when it hits in between 5<x<10 and i genuinely can't tell if it's working. Please help.
clc
clear all
close all
%initial x and y
x(1) = 3;
y(1) = 1;
z(1)=2;
%inital velocity x and y
vx(1) = 10;
vy(1) = 5;
vz(1)=2;
l=15;
w=10;
h=4;
%time step
dt = .1;
for j = 1:1000
vx(j+1) = vx(j)+.1/x(j);
vy(j+1) = vy(j);
vz(j+1)=vz(j);
x(j+1) = x(j)+vx(j)*dt;
y(j+1) = y(j)+vy(j)*dt;
z(j+1)=z(j)+vz(j+1)*dt;
if x(j+1)>=l || x(j+1)<=0
vx(j+1)=-vx(j+1);
%reflect
end
if y(j+1)>=w || y(j+1)<=0
vy(j+1)=-vy(j+1);
%reflect
end
if z(j+1)>=h || z(j+1)<=0
vz(j+1) = -vz(j+1);
%reflect
end
%this is the section i am confused on/need help with
if y(j+1)<=0 && x(j+1)>= 5 && x(j+1)<= 10
%this way it is just against the x plane
end
%
end
plot3(x,y,z)

4 Kommentare

After the assignment to escape, use break
After the for j loop, use
if die || escape
j = j - 1;
x = x(1:j);
y = y(1:j);
z = z(1:j);
end
before the plot() call.
it says "die" is an undefined function or variable
Before the for j loop
die = false; escape = false;
I am not sure at the moment why you are using distinct variables for the two cases ? But perhaps you plan later expansion.
You should consider having the test for escape before the tests for reflections.
You should also consider whether the reflections should be altering the velocity before you increment the positions. Otherwise you are not moving the bird back inside the house on the step where the bird would have been outside the house if not for the walls, with you counting on the reversed velocity to move them back inside next step.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 7 Mai 2019

0 Stimmen

As I have understood your problem, this stops your simulation (where you got confused) as soon as :
%this is the section i am confused on/need help with
if y(j+1)<=0 && x(j+1)>= 5 && x(j+1)<= 10
break
else
continue
end

1 Kommentar

"else continue" is unneeded here and has a risk of messing up later code if they happened to add something after the test.

Melden Sie sich an, um zu kommentieren.

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 7 Mai 2019

0 Stimmen

We are talking only about his problem statement code and this problem as is, huh. We are not considering what iffs here. Thus, the proposed correction is applicable for this code alone and works perfectly.

1 Kommentar

The user modified the code since they first posted, which is why my comments refer to "die" and "escape", which were in the original posted code.
The user problem statement talks about "To start I'm just trying", which suggests that the user will be modifying the code afterwards.
I pointed out some difficulties with the user's code in my comments, so the code does not "work perfectly". During the program, the bird will exist at x position 15.1251 on its second bounds, which is out of range with the maximum 15. On the 4th bounce it will be at x position -0.0204526 . On the fifth bounce, it will be at z position -0.2 . (The other bounces are right at the boundary and so not outside the room and so are valid.)
The corrections for position are most likely to take place after the escape is checked, since it is valid for the bird to fly through an open window. If else continue were coded, the tests would be skipped.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by