chap03classlecture

# chap03classlecture - Chapter 3 Programming with MATLAB...

• Notes
• 78
• 100% (1) 1 out of 1 people found this document helpful

This preview shows pages 1–12. 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.

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.

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.

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.

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.

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
feval - evaluate function specified by string function y = my_func(x) % function 1/x^3 y = x.^(-3); function

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

This is the end of the preview. Sign up to access the rest of the document.
• Fall '08
• EDGE
• matlab, ASCII, John Doe, Euler

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern