Physics Question-Building Demolition

6 Ansichten (letzte 30 Tage)
Tyler
Tyler am 2 Okt. 2014
Kommentiert: Tyler am 2 Okt. 2014
Hello, I'm writing a script to simulate building demolition using basic kinematics and Newton's law F=ma (for friction). Right now it is overly simplified, with hopes to add more to it later. I started off with just one "story" of the building being launched by the explosion. After I got that working, I was hoping to add more stories to the building via user input for them to decide how many there would be. A few errors occurred when trying to switch the code over from a single block being launched to several. The first and foremost would be and if/else statement with a nested for loop within the else. I have several of these in the beginning of the code for different variables, so it is imperative I fix the error.
towh=input('How many stories would you like the building to be');
towharr=1:towh;
if towh==2
v=15;
else
v=15*ones(size(towharr));
for i=1:towh
v=v(i)+(i-1)*0.5
end
end
When I tried to manually type the code into the command window, the array is made just like I want. But when the array is taken into the for loop I get a numel error. For some reason, the array seems to be obliterated and set to just one value..15. (Also, to explain the weird towh==2, the building needs to have at least two floors because I have made the assumption that the explosion wipes out the first floor completely.)
Later on in the code I had a for loop with nested if statements after the projectile motion so it would then show the block sliding on the ground, but since there is friction it slows after a couple seconds. Once I switched over to try to do this building demolition via array math, it doesn't work anymore.
I'll attach the code for anyone interested to help me. I'd appreciate any help for these errors I'm getting, and code not working.
Thanks in advance, Tyler
  3 Kommentare
Stephen23
Stephen23 am 2 Okt. 2014
Also note that you should avoid using the variable names i and j for your loops, as these are the names of the inbuilt imaginary unit . Using the names of inbuilt functions and variables is a really bad idea, as it causes all sorts of difficult-to-solve problems later...
You can use which to check for these.
Tyler
Tyler am 2 Okt. 2014
Oh my bad, I was originally going to use F=ma with the force of friction but since there is no acceleration in the x direction I had to change to use these equations in my code.
vx(j)=vx(j)+u*g*m*dt;
xf(j)=vx(j).*dt+xf(j);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 2 Okt. 2014
Bearbeitet: Stephen23 am 2 Okt. 2014
The array v is being obliterated, on each and every iteration of your for loop you are completely reassigning a new value to the variable v. Consider the first iteration: v is predefined as a vector, you take the first element with v(1) (a scalar), do some stuff and then assign this scalar as the the variable v . So v is now a scalar.
You probably want to index into v instead:
for i=1:towh
v(i)=v(i)+(i-1)*0.5;
end
Although to be honest this would be much tidier, faster and bug-free using fully vectorized code.
  1 Kommentar
Tyler
Tyler am 2 Okt. 2014
Bearbeitet: Tyler am 2 Okt. 2014
That makes a lot of sense. I wasn't thinking that it was changing v from a vector to a scalar. Thank you, that was my first hurdle! (I'll also take a look into turning it into a fully vectorized code.) Also, if you don't mind taking a look at the code itself, I know it's a mess in the script, but my for loop for the block sliding once there is no velocity in the y direction isn't working. The block stops once it hits the ground, but instead of sliding it disappears. Also, now that it can accept input for multiple stories, when I do, the block jumps around to each position for every story instead of multiple blocks being plotted and put into motion. It now is one block jumping to each position for every block during the motion.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu General Applications finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by