Sample M File

Advanced Mathematical Methods for Civil Engineering Applications Wonsiri Punurai, PhD Department of Civil Engineering Room 6391, EG Building 3 Faculty of Engineering, Mahidol University Personal Web: www.egmu.net/~civil/wonsiri MATLAB 103 MATLAB M-files (script files) MATLAB Mat-files (binary files) Importing Data into /Exporting Data from MATLAB Displaying output on the command window Input and Output MATLAB M-files Semi-interactive mode – commands can be saved, refined and reused in MATLAB editor as needed. These files end in “.m” in all platforms. Any word processor can also be used to edit and save files as text. Good for many problem types (ranging from small simple to large and complex models). Open built-in editor, select from menu: File > New > M-file Sample M-files - I Try this example, In MATLAB command window, type This single line defines two matrices (a and b) and computes their product (c). This can be done in the same fashion in m-file format. – Open new m-file using editor. – Now type the following % A sample file to compute a product of two matrices - Save the file as sample.m Go back to Command Window and type sample to execute file. Sample M-files - II Try another example, In MATLAB command window, type Sample M-files - III Simple trigonometry plot Plot of simple trigonometric functions 1.5 sin(t) cos(t) sin(t)*cos(t) 1 0.5 0 -0.5 -1 -1.5 0 1 2 3 4 5 6 θ (radius) Function m-files Example function m-files MATLAB Mat-files These files are convenient to store information that needs to be reused. These binary files end in “.mat”. Use the “load” and “save” commands at the MATLAB command line. They are platform independent. Sample MAT-files From previous example, we have created and executed m-file called sample.m % A sample file to compute a product of two matrices Type whos in the command window, ones will see a, b, c matrices. Let say we want to save these matrices (or variables) for later. Type save sample.mat Now try typing clear and then type load sample.mat Type whos again and you will see matrices a,b and c in workspace. Importing Data into MATLAB Suppose that we have a data file (called road_elevation_centerline.txt) containing road elevation data at center line. The information provided includes: 1) column 1 = station no. (ranging from 1 to 5) 2) column 2 = elevation at each station 1 2 3 4 5 12.49 35.02 5.45 28.29 9 Importing Data (I) Method 1 – in MATLAB command window, use load command >> load road_elevation_centerline.txt An array name road_elevation_centerline which has dimensions 5x2 was just being loaded. We know that first and second column corresponding to station # and elevation at centerline respectively. One can produce two new array variables station no = road_elevation_centerline (:,1) ; center_elev = road_elevation_centerline (:,2) ; Importing Data (II) Method 2 – To import data go to the Editor Window Select Import from the File pull-down menu Importing Data (III) Method 3 – use a function called dlmread >> road_elevation_centerline = dlmread(‘road_elevation_centerline.txt’) >> station no = road_elevation_centerline(:,1); >> center_elev = road_elevation_centerline(:,2); Exporting data from MATLAB Save data into a file in any specific format. Use a function called dlmwrite to specify any delimiters needed. >> load road_elevation_centerline.txt >> dlmwrite(‘road_elevation_centerline1.txt’,ans) >> save road_elevation_centerline2.txt Displaying output Use function ‘disp’ to display to output screen. Typically used in conjunction with ‘num2str’ to convert numerical to string variables Input and Output Text Output System of linear equations BASIC System of linear equations (3 unknowns) In mathematics, a system of linear equations is a collection of linear equations involving the same set of variables. 4 x1 + 3 x2 + 4 x3 = 35 4 x1 + 6 x2 + 8 x3 = 22 3 x1 + 6 x2 + 6 x3 = 40 is a system of three equations in a three variables x1, x2, x3. Solution of linear equations (by hand-I) A solution to a linear system is an assignment of numbers to the variables such that all the equation are simultaneously satisfied. Given 4 x1 + 3 x2 + 4 x3 = 35 4 x1 + 6 x2 + 8 x3 = 22 3 x1 + 6 x2 + 6 x3 = 40 In the matrix form, we have Ax =b ⎡4 3 4⎤ ⎡ x1 ⎤ ⎡35⎤ ⎢4 6 8 ⎥ ⎢ x ⎥ = ⎢22⎥ ⎥⎢ 2 ⎥ ⎢ ⎥ ⎢ ⎢3 6 6⎥ ⎢ x3 ⎥ ⎢40⎥ ⎦⎣ ⎦ ⎣ ⎦ ⎣ 0.5 -0.25 0 To obtain [x], one must compute det(A) and A-1 -72-192-72 ⎡ 4 3 4⎤ 4 3 = 24 det(A) = ⎢4 6 8 ⎥ 4 6 ⎢ ⎥ ⎢3 6 6⎥ 3 6 144+72+96 ⎣ ⎦ 0 -0.25 -0.5 0.625 0.667 -0.5 Solution of linear equations (by hand-II) From Ax =b Multiply both sides by A-1 A-1 Ax = A-1 b I*x = A-1 b A product of A-1b gives x − 0.25 0 ⎤ ⎡35⎤ ⎡ x1 ⎤ ⎡ 0.5 ⎢x ⎥ = ⎢ 0 − 0.5 0.667 ⎥ ⎢22⎥ 2⎥ ⎢ ⎢ ⎥⎢ ⎥ ⎢ x3 ⎥ ⎢− 0.25 0.625 − 0.5 ⎥ ⎢40⎥ ⎣ ⎦ ⎣ ⎦⎣ ⎦ ⎡ x1 ⎤ ⎡ 12 ⎤ ⎢ x ⎥ = ⎢15.667 ⎥ ⎢ 2⎥ ⎢ ⎥ ⎢ x3 ⎥ ⎢ − 15 ⎥ ⎣ ⎦ ⎣ ⎦ Solution of linear equations (by MATLAB – I) x = inv(a)*b; x= 12.0000 15.6667 -15.0000 Solution of linear equations (by MATLAB – II) Problems in relation to CE work Example: The design of a steel truss Structural analysis in which the equations of equilibrium are applied at each node and written in terms of the unknown element internal forces [f] and known externally applied load [P]. Both terms are related by [B][f] = [P] where [B], in this case, represents statics matrix. System of linear equations (n unknowns) Example: Member forces in truss 1. Write down matrices [B], [f], and [P] at each node. 2. Solve for [f] matrix for each node.. Solution: Member forces in truss (I) F3 cos 60 = F2 F3 sin 60 = 750 ⎡− 1 cos 60⎤ ⎡ F2 ⎤ ⎡ 0 ⎤ = ⎢0 1 ⎥ ⎢ F3 ⎥ ⎢750⎥ ⎣ ⎦⎣ ⎦ ⎣ ⎦ ⎡cos 30 − cos 60⎤ ⎡ F1 ⎤ ⎡ 0 ⎤ ⎢ sin 30 sin 60 ⎥ ⎢ F ⎥ = ⎢1000⎥ ⎣ ⎦⎣ 3 ⎦ ⎣ ⎦ ⎡ F1 ⎤ ⎡ 500.000 ⎤ ⎢ F ⎥ = ⎢433.0127 ⎥ ⎢ 2⎥ ⎢ ⎥ ⎢ F3 ⎥ ⎢866.0254 ⎥ ⎣ ⎦ ⎣ ⎦ ⎡− cos 30 1⎤ ⎡ F1 ⎤ ⎡ 0 ⎤ ⎢ sin 30 0⎥ ⎢ F ⎥ = ⎢250⎥ ⎣ ⎦⎣ 2 ⎦ ⎣ ⎦ ⎡cos 60 − 1⎤ ⎡ F3 ⎤ ⎡ 0 ⎤ ⎢ sin 60 0 ⎥ ⎢ F ⎥ = ⎢750⎥ ⎣ ⎦⎣ 2 ⎦ ⎣ ⎦ What we just did is so called “Matrix Formulation” Requirements for a solution Later

Related docs
Sample file
Views: 0  |  Downloads: 0
Sample file
Views: 1  |  Downloads: 0
Appendix A Sample ABAQUS Input File
Views: 20  |  Downloads: 2
sample
Views: 19  |  Downloads: 0
AMMI A M M I M B A M M
Views: 127  |  Downloads: 1
as of at a m
Views: 9  |  Downloads: 0
sample
Views: 0  |  Downloads: 0
HOW TO FILE
Views: 30  |  Downloads: 3
M-Log-and-Decagon-EM5-logger
Views: 2  |  Downloads: 0
Sample Annual EEO Public File Report Form
Views: 0  |  Downloads: 0
premium docs
Other docs by nicknameD
Secrets Revealed From
Views: 108  |  Downloads: 3
Services Invoice Form
Views: 69  |  Downloads: 1
Savings Interest Calculators
Views: 206  |  Downloads: 0
Set Work Schedule
Views: 207  |  Downloads: 0
Service Invoice Forms
Views: 161  |  Downloads: 7
Semi Annual Compound
Views: 85  |  Downloads: 0
Semi Annual Amortization
Views: 88  |  Downloads: 0
Semi Monthly Calculator
Views: 120  |  Downloads: 0
School Year Templates
Views: 66  |  Downloads: 0
School Year Template
Views: 129  |  Downloads: 1
Search Tracking Form
Views: 62  |  Downloads: 3
Search Free Printable
Views: 266  |  Downloads: 0
School Schedule Template
Views: 456  |  Downloads: 5
School Year Calendar
Views: 69  |  Downloads: 0
School Printable Calendars
Views: 94  |  Downloads: 0