MATLABFunctionTutorial.pdf

(246 KB) Pobierz
FunctionTutorial
MATLAB for Function Tutorial in Module 8.2
Introduction to Computational Science: Modeling and Simulation for the Sciences
Angela B. Shiflet and George W. ShifletWofford College
© 2010 by Princeton University Press
Download
Download the associated MATLAB file FunctionTutorial.m .
Introduction
In this chapter, we deal with models that are driven by the data. In such a situation, we
have data measurements and wish to obtain a function that roughly goes through a plot of
the data points capturing the trend of the data, or fitting the data . Subsequently, we can
use the function to find estimates at places where data does not exist or to perform further
computations. Moreover, determination of an appropriate fitting function can sometimes
deepen our understanding of the reasons for the pattern of the data.
In this module, we consider several important functions, some which we have
already used. By being familiar with basic functions and function transformations, the
modeler can sometimes more readily fit a function to the data.
Linear Function
The concept of a linear function was essential in our discussions of the derivative and
simulation techniques, such as Euler's Method. Here, we review some of the
characteristics of functions whose graphs are lines.
The command in Quick Review Question 1 plots the graph of the linear function y
= 2 t + 1. This line has y -intercept 1, because y = 1 when t = 0. Thus, the graph crosses
MATLAB Function Tutorial
2
the y -axis when t = 0. With data measurements where t represents time, the y -intercept
indicates the initial data value. The slope of this particular line is 2, which is the
coefficient of t . Consequently, when we go over 1 unit to the right, the graph rises by 2
units.
Definitions
A linear function , whose graph is a straight line, has the following
form:
y = mx + b
The y -intercept , which is b , is the value of y when x = 0, or the place where the line
crosses the y -axis. The slope , m , is the change in y over the change in x . Thus, if
the line goes through points ( x 1 , y 1 ) and ( x 2 , y 2 ), the slope is as follows:
m =
Δ x = y 2 y 1
x 2 x 1
Quick Review Question 1
a. Execute the following MATLAB command, which plots the above function,
f ( t ) = 2 t + 1, from t = -3 to 3:
t = -3:0.1:3;
plot(t, 2*t + 1)
b. By replacing xxxxxx with the appropriate equation, complete the command to
plot f along with the equation of the line with the same slope as f but with y -
intercept 3. Distinguish between the graphs of f and the new function, such as
by color, line thickness, or dashing.
plot(t, 2*t + 1, t, xxxxxx, '--')
Δ y
403292210.001.png
 
MATLAB Function Tutorial
3
c. Copy the command from Part b, and change the second function to have a y -
intercept of -3.
d. Describe the effect that changing the y -intercept has on the graph of the line.
e. Copy the command from Part b, and change the second function to have the
same y -intercept as f but slope 3.
f. Copy the command from Part b, and change the second function to have the
same y -intercept as f but slope -3.
g. Describe the effect that changing the slope has on the graph of the line.
Quadratic Function
In Module 2.3 on "Rate of Change" and Module 2.4 on "Fundamental Concepts of
Integral Calculus," we considered a ball thrown upward off a bridge 11 m high with an
initial velocity of 15 m/sec. The function of height of the ball with respect to time is the
following quadratic function:
s ( t ) = -4.9 t 2 + 15 t + 11
The general form of a quadratic function is as follows:
f ( x ) = a 2 x 2 + a 1 x + a 0
where a 2 , a 1 , and a 0 are real numbers. The graph of the ball's height s ( t ) in Figure 2.3.1 of
the "Rate of Change" module is a parabola that is concave down. The next two Quick
Review Questions develop some of the characteristics of quadratic functions.
Definition A quadratic function has the following form:
f ( x ) = a 2 x 2 + a 1 x + a 0
where a 2 , a 1 , and a 0 are real numbers. Its graph is a parabola .
403292210.002.png
MATLAB Function Tutorial
4
Quick Review Question 2
a. Execute the following MATLAB command, which plots the above function,
s ( t ) = -4.9 t 2 + 15 t + 11, from t = -1 to 4 (NOTE that we use .^ for
exponentiation of the sequence for t .):
t = -2:0.1:4;
plot(t, -4.9*t.^2 + 15*t + 11)
b. Give the command to plot s ( t ) and another function with the same shape that
crosses the y -axis at 2. Have the graph of the new function be dashed.
c. Using calculus, determine the time t at which the ball reaches its highest point.
Verify your answer by referring to the graph.
d. What effect does changing the sign of the coefficient of t 2 have on the graph?
Quick Review Question 3 In this question, we consider various transformations on a
function. Use .^ for exponentiation of the sequence for t .
a. Plot t 2 , t 2 + 3, and t 2 - 3 on the same graph, using dashing for the second curve
and dotting for the third.
b. Describe the effect of adding a positive number to a function.
c. Describe the effect of subtracting a positive number from a function.
d. Plot t 2 , ( t + 3) 2 , and ( t - 3) 2 on the same graph, using dashing for the second
curve and dotting for the third.
e. Describe the effect of adding a positive number to the independent variable in
a function.
f. Describe the effect of subtracting a positive number from the independent
variable in a function.
g. Plot t 2 and - t 2 on the same graph, using dashing for the second curve.
MATLAB Function Tutorial
5
h. Describe the effect of multiplying a function by -1.
i. Plot t 2 , 5 t 2 , and 0.2 t 2 on the same graph, using dashing for the second curve
and dotting for the third.
j. Describe the effect of multiplying the function by number greater than 1.
k. Describe the effect of multiplying the function by positive number less than 1.
Polynomial Function
Linear and quadratic functions are polynomial functions of degree 1 and 2, respectively.
The general form of a polynomial function of degree n is as follows:
f ( x ) = a n x n + + a 1 x + a 0
where a n , …, a 1 , and a 0 are real numbers and n is a nonnegative integer. The graph of
such a function with degree greater than 1 consists of alternating hills and valleys. The
quadratic function of degree 2 has one hill or valley. In general, a polynomial of degree n
has at most n - 1 hills and valleys.
Definition A polynomial function of degree n has the following form:
f ( x ) = a n x n + + a 1 x + a 0
where a n , …, a 1 , and a 0 are real numbers and n is a nonnegative integer.
Quick Review Question 4
a. Execute the following MATLAB command that plots the polynomial function
p ( t ) = t 3 - 4 t 2 - t + 4 from t = -2 to 5 with .^ for exponentiation of the sequence
for t :
t = -2:0.1:5;
plot(t, t.^3 - 4*t.^2 - t + 4)
403292210.003.png
Zgłoś jeśli naruszono regulamin