





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






2-
2-
Basic Mathematical Functions (see p62-64 in textbook) However, since there are hundreds of Matlab functions, a useful tool is Matlab Helpdesk.
Exponential Function
ans = 1
name argument parenthesis
ans = 0.3679 1.0000 2.7183 ( [exp(-1), exp(0), exp(1)] )
The exp function argument can be a scalar, vector or matrix.
4
Three forms of the use of a function in Matlab are:
A Matlab function like a mathematical function is a rule where given a certain input or inputs, the rule tells you how to compute the output value or how to produce an effect (e.g. the plot function produces a figure).
The inputs are called the “arguments” to the function.
As an illustration, a function can be likened to the sequence of instructions on an income tax form(see next two slides).
2-
Example: Plot the sine function using the built-in function “sin”. (see section 6.1)
By default Matlab will connect the points (xi ,yi ) with a straight line. A more general version of the plot command is:
where color is:
cyan magenta yellow red green blue white black c m y r g b w k
2-
vec1 = [-2 ; 3 ; 4] ; vec2 = [-2 3 4] ; sum(vec1) input is a 3 x 1 column vector and output is a scalar.
ans = 5
sum(vec2) input is a 1 x 3 row vector and output is a scalar. ans = 5
Matlab’s built-in functions accept a wide variety of array sizes. Example: The built-in function sum.
(see section 5.3 for examples) 2-
Users can add to Matlab’s built-in functions by “programming” a function. A program in Matlab is either in the form of a script or a function. Programming a function is described in the Matlab book in Chapter 4.2 and scripts are described in Chapter 4.1.
2-
Programming -
2-
2-
Note: These steps correspond to those found in the course C book p. 37.
2-
2-
The function “definition” should list one input variable named fahr and one output variable celsius. Compute the value of the celsius temperature and assign the value to a variable “celsius” The “function” mechanism of Matlab will automatically return the value in the variable celsius when the function is used (called) at the Matlab prompt.
2-
Any sequence of Matlab commands can be put in a file. The file suffix should end with a “.m”. The sequence of commands will be executed (from the top of the file down, command by command).
edit to open the Matlab editor and create a new file. Then type (and save) the following: function celsius = F_to_C(fahr) % This function converts Fahrenheit to Celsius. celsius = (fahr -32)*5/9;
Click “File” and then “Save As” to name the file “F_to_C.m”.
2-
Improved documentation for the F_to_C function.
function celsius = F_to_C(fahr) % function celsius = F_to_C(fahr) % This function converts Fahrenheit to Celsius. % input fahr can be a scalar or vector of temps in degree F % output celsius is a scalar or vector of temps in degree C % Programmer: Tom Gambill % Date: 1/30/ celsius = (fahr -32)*5/9;
2-
Use the fact that height_t = height_0 + velocity_0 time + 1/2 * g * time time, where height_t is the height of the object at any given time (in seconds), g is the acceleration due to gravity, -9.8 m/s^2. velocity_0 is the velocity at time = 0. Therefore to compute the time to impact, set height_t = 0 and solve for time. This equation (after doing some algebra)can be written as:**
0 = height_0 + velocity_0 * time + 1/2 * g * time * time
This is a quadratic formula in terms of the variable time.
2-
Initial values of height_0 and velocity_0 are provided via the arguments when the function is called.
Solve quadratic formula for time
Quadratic formula has two solutions, select the positive value as the result to be returned.
The input arguments to the function consists of scalar values for height_ and velocity_0 and the output is the exact time (in seconds) of contact with the ground. Assumptions: No air resistance and time = 0 when object begins decent. (Also height_0 > 0 implicit)
2-
2-
(see the next slide for ways to “run” or execute the function)
2-
Note: One statement in the body of the function should be of the form, output_variable = expression;
% The line above is called the function definition line.
% A line that begins with a “%” symbol is called a comment.
2-
polynomial. In Matlab polynomials are represented by vectors. e.g. the polynomial, x 2 - 1 is represented by
To compute all the roots (x-values where p(x) = 0) of this polynomial type,