MIT 16 070 - Introduction to Computers and Programming

Unformatted text preview:

1 16.070 Introduction to Computers and Programming 5 April Recitation 8 Spring 2001 Topics Procedure for the Simulation of Differential Equations (By Example): • Concept • Equations of motion • Continuous State Space • Discrete State Space • Algorithm • Coding • Analysis Procedure for the Simulation of Differential Equations Concept A Mars Lander(/rock/ball) is suspended 500m from the surface of the earth and released. Simulate the dynamics of the problem as the vehicle falls to earth. Only 1-DOF is required: the vertical altitude of the vehicle above the surface of the earth. Assume a constant gravitational acceleration of g=9.81m/s/s. The vehicles mass is 500kg (does this matter?). The vehicle does not thrust and drag is neglected. Write the vehicles state history to a file, in the format: <time> <displacement> <velocity> Figure 1 Lander suspended above the surface of the earth (NO THRUST!) x mg2 Equations of motion Newton’s second law: xmmaF== Initial Conditions: ()()005000==xx Forces acting on the vehicle: mgF−= Complete simplified equation on motion: mgxm−= Continuous State Space The states are similar to the specific of initial conditions that are required: State Vector: ==Χxxxx21 Continuous State Space Representation: )(100010mgm−+Χ=Χ Discrete State Space Approximate dtdx with txxnn∆−+1 then we get the following Discrete State Space result: )(10001021212111mgmxxtxxtxxnnnnnn−+=∆−∆−++ )(0101211211mgmtxxtxxnnnn−∆+∆=++3 Algorithm Variables and constants: mgFtttmtBtAnn−=ΧΧ==∆=∆=∆=+1max02001.000101 Basic Pseudo-code (A flowchart will be added): MAIN MODULE [0] Start [1] Declare all the variables/constants above [2] Open an output file for telemetry [3] Set initial conditions SEPARATE MODULE [4] Write initial states to a file SEPARATE MODULE [5] Increment time (t=t+dt) [6] If t>tmax, goto XX (IMPLEMENTED WITH A WHILE LOOP) [7] Update State (Xn+1=AXn+BF) SEPARATE MODULE [8] Write states to file SEPARATE MODULE [9] Advance the state Xn=Xn+1 [10] Goto [6] [11] Close all open files [12] Exit Code /* Thomas Jones, April 2001 */ /* Recitation 8 */ /* Simulation of a falling lander */ /* (on earth) */ /* -------------------------------- */ /* ALL UNITS ARE STANDARD METRIC */ #define time_step 0.01 #define out_file "rec8_out.txt" #define mass 500.0 #define grav_accel 9.81 #define initial_position 500.0 #define initial_velocity 0.0 #define max_time 10.04 #include <stdio.h> #include <stdlib.h> /* Prototypes */ void Set_Initial_Conditions(double Xn[]); void Write_To_File(double t, double X[],FILE *ofp); void Update_X(double A[][2],double B[],double F,double Xn[],double Xnp1[]); /* Start main function */ int main(void) { /* Variable Declarations and Setup */ /* declare time, mass and gravitational acceleration */ double t=0; /* time */ const double dt=time_step; /* time step */ const double m=mass; /* mass */ const double g=grav_accel; /* gravity acceleration */ /* declare A, B, F, Xn, Xnp1, such that Xnp1 = AXn + BF */ double A[2][2]={1.0, dt, 0.0, 1.0}; double B[2]={0, dt/m}; double Xn[2]={0.0, 0.0}; double Xnp1[2]={0.0,0.0}; double F=-m*g; /* open telemetry output file */ FILE *ofp; printf("Opening Telemetry File...\n"); if ((ofp=fopen(out_file,"w+"))==0) { printf("File open error!\n"); exit(-1); } printf("File Opened...\n"); /* Implementation of Flow Chart /* Main BODY of Code */ /* ---------------------- */ /* !!!!!!!!!!!!!!!!!!!!!! */ printf("Initializing State Variables...\n"); Set_Initial_Conditions(Xn); /* initial conditions */ Write_To_File(t,Xn,ofp); /* save inital telemetry */ printf("Simulating...Please wait...\n"); while(t <= max_time) { t=t+dt; /* increment time */ Update_X(A,B,F,Xn,Xnp1); /* state update */ Write_To_File(t,Xnp1,ofp); /* save telemetry */ Xn[0]=Xnp1[0]; /* advance state */ Xn[1]=Xnp1[1]; } printf("Simulation Complete!\n"); /* !!!!!!!!!!!!!!!!!!!!!! */ /* ---------------------- */ /* close all open files */ printf("Closing Files...\n"); fclose(ofp); printf("Done!\n\n"); return 0; }5 /* ----------------------------------------- */ /* Function sets initial conditions of state */ /* ----------------------------------------- */ void Set_Initial_Conditions(double Xn[]) { Xn[0]=initial_position; Xn[1]=initial_velocity; return; } /* end Set_Initial_Conditions */ /* ------------------------------------------ */ /* Function writes state and time to file ofp */ /* ------------------------------------------ */ void Write_To_File(double t, double X[],FILE *ofp) { fprintf(ofp,"%2.2lf\t%lf\t%lf\n",t,X[0],X[1]); return; } /* end Write_To_File */ /* ------------------------------------------ */ /* Function performs state update */ /* ------------------------------------------ */ /* PS8 requires this state update to be performed by */ /* a general matrix multiplier function, which is quite */ /* different from the code below !!! */ void Update_X(double A[][2],double B[],double F,double Xn[],double Xnp1[]) { Xnp1[0]=A[0][0]*Xn[0]+A[0][1]*Xn[1]+B[0]*F; Xnp1[1]=A[1][0]*Xn[0]+A[1][1]*Xn[1]+B[1]*F; return; } /* end Update_X */6 Analysis An output file called rec8_out.txt contains the telemetry data. Output: Opening Telemetry File... File Opened... Initializing State Variables... Simulating...Please wait... Simulation Complete! Closing Files... Done! Press


View Full Document

MIT 16 070 - Introduction to Computers and Programming

Documents in this Course
optim

optim

20 pages

Load more
Download Introduction to Computers and Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Computers and Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Computers and Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?