{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chap03classlecture

# chap03classlecture - Chapter 3 Programming with MATLAB...

This preview shows pages 1–11. Sign up to view the full content.

Chapter 3 Chapter 3 Programming with Programming with MATLAB MATLAB

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

View Full Document
Homework Grading Policy Homework Grading Policy Include the general form of the formula  to be used Include the initial step Give units Be consistent with rounding Retain at least three significant digits   Justify answers
Newton’s Second Law Euler’s method To obtain good accuracy, it is necessary to use many small steps Extremely laborious and time-consuming to implement by hand Numerical Solution Numerical Solution 2 d 2 d v m c g dt dv v c mg dt dv m - = - = ) ( ) ( ) ( ) ( i 1 i 2 i d i 1 i t t t v m c g t v t v - - + = + +

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

View Full Document
M-Files: Scripts and Functions M-Files: Scripts and Functions You can create and save code in text files using MATLAB Editor/Debugger or other text editors (called m-files since the ending must be .m) M-file is an ASCII text file similar to FORTRAN or C source codes ( computer programs) A script can be executed by typing the file name, or using the “run” command Difference between scripts and functions Difference between scripts and functions Scripts share variables with the main workspace Functions do not
Script Files Script Files Script file – a series of MATLAB commands saved on a file, can be executed by typing the file name in the Command Window invoking the menu selections in the Edit Window: Debug, Run Create a script file using menu selection: File, New, M-file

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

View Full Document
Compute the velocity of a free-falling bungee jumper at a specific time Open the editor with: File, New, M-file Save the file as bungee_jumper.m Type bungee_jumper in command window Script File – Bungee Jumper Script File – Bungee Jumper g = 9.81; m = 68.1 ; t = 20; cd = 0.25; v = sqrt(g*m/cd) * tanh(sqrt(g*cd/m)*t) » bungee_jumper v = 51.6416 Type the name of the script file
Function File Function File Function file: M-file that starts with the word function Function can accept input arguments and return outputs Analogous to user-defined functions in programming languages such as Fortran, C, … Save the function file as function_name.m User help function in command window for additional information

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

View Full Document
Functions Functions One variable function y = function_name(input arguments) More than one output variables function [y, z] = function_name(input arguments) Examples: function y = my_func (x) y = x.^3 + 3*x.^2 -5 * x +2 ; function area = integral (f, a, b) ya = feval (f, a); yb = feval(f, b); area = (b-a)*(ya+yb)/2;
function t = trap_ex(a, b) t = (b - a) * (a^(-3) + b^(-3)) / 2; Approximate the integral of f(x) = 1/x 3 using basic trapezoid rule Filename: trap_ex .m » y = trap_ex (1, 3) y = 1.0370 ) ( 3 3 b a 3 b 1 a 1 2 a b dx x 1 y + - 2245 = a b

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

View Full Document
Script File for Integral Script File for Integral 1. Save integral (f, a, b) in script file integral.m 2. Save function my_func(x) in script my_func.m 3. Run script file >> area = integral(‘my_func’, 1, 10) >> area = integral(‘my_func’, 3, 6) [ ] ) b ( f ) a ( f 2 a b dx ) x ( f b a + - = area a b f(x) x
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 78

chap03classlecture - Chapter 3 Programming with MATLAB...

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

View Full Document
Ask a homework question - tutors are online