An Introduction to Programming and Numerical Methods in MATLAB
468 pág.

An Introduction to Programming and Numerical Methods in MATLAB

DisciplinaMatlab498 materiais2.116 seguidores
Pré-visualização50 páginas
at the answer shows that MATLAB has returned a vector containing
the elements
[a1b1, a2b2, a3b3].
The . indicates to MATLAB to perform the operation term by term and the *
indicates we require a multiplication. We can also do a term by term division
>> a = [1 2 3];
>> b = [4 5 6];
>> a./b
ans =
0.2500 0.4000 0.5000
The result is, as we would expect,[
Example 1.11 We shall create two vectors running from one to six and from
six to one and then demonstrate the use of the dot arithmetical operations:
16 1. Simple Calculations with MATLAB
s = 1:6;
t = 6:-1:1;
This produces the output
>> s+t
ans =
7 7 7 7 7 7
>> s-t
ans =
-5 -3 -1 1 3 5
>> s.*t
ans =
6 10 12 12 10 6
>> s./t
ans =
0.1667 0.4000 0.7500 1.3333 2.5000 6.0000
>> s.\u2c62
ans =
1 4 9 16 25 36
1.5 Setting Up Mathematical Functions 17
>> 1./s
ans =
1.0000 0.5000 0.3333 0.2500 0.2000 0.1667
>> s/2
ans =
0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
>> s+1
ans =
2 3 4 5 6 7
These represent most of the simple operations which we may want to use.
We note that in order for these operations to be viable the vectors need to be
of the same size (unless one of them is a scalar \u2013 as in the last three examples).
1.5 Setting Up Mathematical Functions
Following on from the previous section we discuss how one might evaluate a
function. It is crucial that you understand this section before you proceed.
We revisit the topics introduced in the previous section and discuss the
ways in which you can set up the input to the function
Example 1.12 Set up a vector x which contains the values from zero to one
in steps of one tenth.
This can be done in a variety of ways:
18 1. Simple Calculations with MATLAB
% Firstly just list all the values:
x = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0];
% Use the colon construction
x = 0:0.1:1.0;
% Or use the command linspace
x = linspace(0,1,11);
As noted previously we note that there are eleven values between zero
and one (inclusive) for a step length of one tenth. You may want to try
linspace(0,1,10) and see what values you get.
Each of these methods are equally valid (and more importantly will produce
the same answer) but the latter two are probably preferable, since they are
easily extended to more elements.
We now wish to set up a simple mathematical function, say for instance
y = x2. Initially you may want to type x\u2c62 but this will generate the error
??? Error using ==> \u2c6
Matrix must be square.
This is because this operation is trying to perform the mathematical operation
x× x and this operation is not possible. Instead we need to use y=x.\u2c62 which
>> y = x.\u2c62
y =
Columns 1 through 7
0 0.0100 0.0400 0.0900 0.1600 0.2500 0.3600
Columns 8 through 11
0.4900 0.6400 0.8100 1.0000
Here we see that each element of x has been squared and stored in the array
y. Equivalently we could use y = x.*x;.
Example 1.13 Construct the polynomial y = (x+ 2)2(x3 + 1) for values of x
from minus one to one in steps of 0.1.
1.5 Setting Up Mathematical Functions 19
Here it would be laborious to type out all the elements of the vector so instead
we use the colon construction. We shall also de\ufb01ne f = (x+2) and g = x3 +1,
so that we have the code:	
x = -1:0.1:1;
f = x+2;
g = x.\u2c63+1;
y = (f.\u2c62).*(g);
In the construction of g we have used the dot arithmetic to cube each element
and then add one to it. When constructing y we \ufb01rstly square each element
of f (with f.\u2c62) and then multiply each element of this by the corresponding
element of g.
You should make sure that you are able to understand this example.
Example 1.14 Construct the function y =
x3 + 1
for values of x from one to
two in steps of 0.01.
Here we merely give the solution:	
x = 1:0.01:2;
f = x.\u2c62;
g = x.\u2c63+1;
y = f./g;
(We could have combined the last three lines into the single expression y =
For the moment it may be a good idea to use intermediate functions when
constructing complicated functions.
Example 1.15 Construct the function
y(x) = sin
x cosx
x2 + 3x + 1
for values of x from one to three in steps of 0.02.
Here, again, we use the idea of intermediate functions
20 1. Simple Calculations with MATLAB
x = 1:0.02:3;
f = x.*cos(x);
g = x.\u2c62+3*x+1;
y = sin(f./g);
NB MATLAB will actually calculate f/g and in this case it will return a
scalar value of \u22120.1081. Unfortunately this will not generate an error but it
will mean that the answer is not a vector as we should be expecting.
1.6 Some MATLAB Speci\ufb01c Commands
We shall now introduce a couple of commands which can be used to make cal-
culations where the input can take a variety of forms. The \ufb01rst command is
polyval. This command takes two inputs, namely the coe\ufb03cients of a polyno-
mial and the values at which you want to evaluate it. In the following example
we shall use a cubic but hopefully you will be able to see how this generalises
to polynomials of other orders.
Example 1.16 Evaluate the cubic y = x3 + 3x2 \u2212 x \u2212 1 at the points x =
(1, 2, 3, 4, 5, 6). We provide the solution to this example as a commented code:\ufffd
% Firstly set up the points at which the polynomial
% is to be evaluated
x = 1:6;
% Enter the coefficients of the cubic (note that
% these are entered starting with the
% coefficient of the highest power first
c = [1 3 -1 -1];
% Now perform the evaluation using polyval
y = polyval(c,x)
Note that in this short piece of code everything after the % is treated by MAT-
LAB as a comment and so is ignored. It is good practice to provide brief, but
meaningful, comments at important points within your code.
1.6 Some MATLAB Speci\ufb01c Commands 21
It is important that you remember to enter the coe\ufb03cients of the poly-
nomial starting with the one associated with the highest power and
that zeros are included in the sequence.
Important Point
We might want to plot the results of this calculation and this can be simply
accomplished using the plot command. Consider the following example:
Example 1.17 Plot the polynomial y = x4+x2 \u22121 between x = \u22122 and x = 2
(using \ufb01fty points).	
x = linspace(-2,2,50);
c = [1 0 1 0 -1];
y = polyval(c,x);
This produces the output
\u22122 \u22121.5 \u22121 \u22120.5 0 0.5 1 1.5 2
In the next chapter we shall discuss plotting in more detail and show how plots
can be customised.
There are many other commands which allow us to manipulate polynomials:
perhaps one of the most useful ones is the roots. The polynomial is de\ufb01ned in
the same way as in the previous examples. The input to the routine is simply
these coe\ufb03cients and the output is the roots of the polynomial.
Example 1.18 Find the roots of the polynomial y = x3 \u2212 3x2 + 2x using the
22 1. Simple Calculations with MATLAB
command roots.\ufffd
c = [1 -3 2 0];
r = roots(c)
This returns the answers as zero, two and one.
In fact the converse command also exists, which is poly. This takes the roots
and generates the coe\ufb03cients of the polynomial having those roots (which is
monic, that is the coe\ufb03cient of the highest term is unity).
1.6.1 Looking at Variables and Their Sizes
Before we proceed we mention a couple of useful commands for seeing which
variables are de\ufb01ned. To list the variables which are currently de\ufb01ned we can
use the command whos. This will give a list of the variables which are currently
de\ufb01ned (a shorter output can be obtained by using the command who). This
command can be used to list certain variables only, for instance