Wednesday, October 12, 2011

Simulating physical events through computer graphics techniques - rectilinear motion

Hi everyone, unfortunately my time is not being enough for making posts as I'd like to do.

Let me share my situation: I'm working at morning and afternoon in a company and giving classes at night in a university.

So, all days of my weeks are full of activities and studies. I wish to set my time better on the following days, because I like to make posts on my blogs and share something of what I know.

In this post, I want to teach how to simulate a physical event using computer graphics techniques, just a comment: one of my disciplines in the university is computer graphics and we are using Scilab for the examples and exercises.

Let's try to simulate some kinematics events, about rectilinear motions.

For a first example, being a punctual object that's moving over a horizontal line (on the floor) and we wish to see this moving event by a top vision.


The movement equation is

position = initial_position + velocity * time_variation


Now, writing the code:


initial_position = [0; 0]; //point that refers to initial position meaning [x_initial_coordinate; y_initial_coordinate]


velocity = [1; 2]; //vector velocity meaning [x_velocity_component; y_velocity_component]


time = [0:10]; //instants that we will create samples for showing on the screen


position = initial_position*ones(1, length(time)) + velocity*time; //this command implements the movement equation, and the matrix ones(1, length(time)) is used for correcting the dimensions and makes the matrices sum possible


for t = 1:length(time), //this loop creates the graph dynamically simulating real motion
 plot(position(1,[1:t]), position(2,[1:t]), '.y'); 
 plot(position(1,t), position(2,t), '.');
 sleep(1000);
end;





This code will show the last point of position with blue color and the other points with yellow color.


Test the code and send me feedback of your feelings.

1 comment:

Anonymous said...

Thanks a lot for posting this example! I'm learning how to use scilab, and it was nice to see how the code worked.
Airam