03-Programming with Matlab

03-Programming with Matlab - 3: Programming with MATLAB...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
3: Programming with MATLAB 3: Programming with MATLAB
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sep 19, 2006 22:01 2 Chapter Objectives Learn how to write M-file programs to implement numerical methods: Create well-documented M-files in the edit window and invoke them from command window. Set-up M-files so they interactively prompt users for information and display results in the command window. Write clear and well-documented M-files by employing structured programming constructs to implement logic and repetition. Understand vectorization and its benefits. Understand how functions can be passed to M-files.
Background image of page 2
Sep 19, 2006 22:01 3 Example Problem Bungee jumper from Ch. 1 d v d t = g c d m v 2 (1.8) v t  = gm c d tanh g c d m t (1.9) v i 1 = v i d v i d t t = v i g c d m v i 2 t
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sep 19, 2006 22:01 4 3.1: M-files M-files contain a series of Matlab statements that can be executed “all at once”. Text files ( not word processor files) that have a ' .m ' extension and are locatable via Matlab's s earch path . Two varieties: script files and function files. script files usually store commands that will be re-executed, usually without change. primary purpose is to display a result in the command window, or to produce a plot. function files implement functions that behave much like the built-in functions of Matlab. primary purpose is to define a function that can be used in Matlab expressions. functions usually do not explicitly display any result. many (most?) of the Matlab built-in functions are in fact function m-files supplied in Matlab libraries. they are a way to extend Matlab to have new capabilities.
Background image of page 4
Sep 19, 2006 22:01 5 3.1.1: Script M-Files Problem: develop a script to find the velocity at a specific time: Select Matlab menu item: File / New / M-File : Enter this text: g = 9.81; m = 68.1; t = 12; cd = 0.25; v = sqrt(g * m / cd) * tanh(sqrt(g * cd / m) * t) Save the file as scriptdemo.m In the command window, type: >> scriptdemo The result will be displayed: v = 50.6175
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sep 19, 2006 22:01 6 Script Files – Observations script file computes result for one value of t only. can change value assigned to any of the variables and re-run the script. If script is to be used primarily to compute v for specific values of t , it should also display the value of t . script should also be documented with comments. As a minimum : % Evaluate Eqn 1.9 for specific values of t t = 12 % time, s g = 9.81; % accel. due to gravity, m/s/s m = 68.1; % mass of jumper, kg cd = 0.25; % drag coeff., kg/m v = sqrt(g * m / cd) * tanh(sqrt(g * cd / m) * t) % eqn 1.9 now, the display in the command window will be: t = 12 v = 50.6175 which is an improvement, but function m-files are even better.
Background image of page 6
Sep 19, 2006 22:01 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 04/14/2010.

Page1 / 47

03-Programming with Matlab - 3: Programming with MATLAB...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online