Your first lab exercise is designed to acquaint you with some of the features of MATLAB you'll be using during the semester, and also some of the programming principles you'll be using long afterward. Pay particular attention to details of syntax – very slight changes in what you type can have a large impact on results. Pay attention to subtle meanings in how variables are treated by your mind and the interpreter of MATLAB's programming language.
This assignment is due at the beginning of lecture on Monday, 2 Feb. Your solution should include all codes and plots, as well as written responses where appropriate. Feel free to email me your lab submission.
MATLAB functions are written with the following syntax:
function [output1,output2,...] = functionName(input1,input2,...) % help line 1 % help line 2 % . % . % . – blank line – – line 1 of code – – line 2 of code – – . – – . – – . – returnThe "help line" statements are very useful, in that when you type help functionName at the command line, each of those lines are sent back to the command window. This will come in very handy. Remember that it is extremely important to comment your code throughout for readability and ease of debugging. Variables should have descriptive names (or even short, sensible ones; e.g., v for velocity and t for time). The code should mimic the mathematics and/or algorithm of your problem, again for ease of reading and later troubleshooting. As a rule, don't simplify your expressions – in this course, and in real use, readability is more important than speed of execution. It's only well after you're sure the code functions that you'll start trying fancy techniques to wring speed out of expressions via simplification. Examples of relatively well-written and well-commented code can be found on the course outline as the course progresses.
A particle is acted upon by a constant force.
function vars = constForce(t,vars_o,a)
You may wonder why it's set up this way, since you need both position and velocity. Fortunately, MATLAB is vectorized, so you can store multiple variables in a vector (which, confusingly, is also a variable).
>> x_o = 8; >> v_o = -3; >> a = 4; >> t = 5; >> vars_o = [x_o ; v_o] % note this line isn't terminated by a semicolon >> vars = constForce(t,vars_o,a) % nor is this one
Your results were stored in a vector. This represents a very powerful paradigm in MATLAB. The function calls in MATLAB are common to a large number of programming languages. This, too, is a powerful paradigm and may take some getting used to.
Let's look at some alternate ways to call a function. They can be called directly, or functions can even be represented by variables. In C/C++ this concept is called "pointer to function", but in MATLAB it's referred to as a "function handle".
>> foo = @(t,xv) constForce(t,xv,a) >> vars = constForce(t,vars_o,a) >> varsHandle = foo(t,vars_o) >> vars - varsHandle
Let's use our function now to obtain a range of results and store them in another variable. I'll lead you along with some example code and you'll interpret what happens. Type the commands below at the command line.
>> x_o = 0; v_o = 30; a = -10;
>> t = 0:0.5:7;
>> results = zeros(2,length(t));
>> for k = 1:length(t)
results(:,k) = constForce(t(k),[x_o v_o],a);
end
>> x = results(1,:);
>> v = results(2,:);