Matlab For Numerical Algorithms

Páginas: 7 (1510 palabras) Publicado: 3 de enero de 2013
Matlab for Numerical Algorithms
Vectors
A vector is a one-dimensional array of numbers. A row vector is written horizontally; a column vector is
written vertically.
In Matlab, vectors are defined by writing the components inside square brackets. For row vectors, the
elements are separated by commas or spaces, e.g. [5 3 -2 4 -1 0 2]. The components in column vectors
are separated bysemicolons, e.g. [3;-1;0;2;9].
We can refer to a component of a vector by putting the component number in round brackets. For example,
if v is the vector [5 0 2 -4 1 0 3], then v(5) is the 5th component which is 1.

Matrices
A matrix is a two-dimensional array of numbers. We write these in Matlab inside square brackets, entering
one (horizontal) row at a time, just like a row vector, and we put asemi-colon at the end of each row. For
example, the matrix
−2 5 1
C=
104
is entered into Matlab using C=[-2 5 1;1 0 4].
We can refer to a single row or column of a matrix. To find the second row of matrix C, we write C(2,:),
which gives [1 0 4]. It finds all the components in C whose first subscript is 2. Similarly the third column
of C is referred to as C(:,3).

for loops
These are used torepeat sections of code when the number of repeats is known before the code is run.
Examples include:
• Do 5 iterations. We know in advance that the section will be repeated 5 times.
x=3;
for i=1:5
x=(x+2/x)/2;
disp(x)
end
• Subtract the first row of a matrix from the second row - assume the matrix has n columns. When
this is performed, the value for n will be known - though it won’t beknown when the code is written.
A=[1 5 3 5; 6 4 -2 5;6 0 -5 2];
[m,n]=size(A)
for i=1:n
A(2,i)=A(2,i)-A(1,i);
end
disp(A)

1

while loops
These are used to repeat sections of code when the number of repeats depends on the results found/calculated
in the loop.
Examples include:
• Prompt the user to input a positive integer and keep prompting until they do.
n=input(’Enter a positivenumber ’);
while n=1
x=x*m;
disp(x)
end
In this case we keep multiplying by m until x becomes less than 1. The number of repeats depends
on how many times we need to do this to get below 1.

Functions
Functions are also used to repeat sections of code. They may be used for standard methods that we want
to use often. A function is usually defined by writing it in a separate Matlab file.
Afunction will be of the form
function output parameter(s) = function name ( input parameter(s) )
% Write in here some comments about what the function does
Matlab statements to calculate the output parameters values from the input parameter values.
The output parameters represent the values that you want calculated in the function; the input parameters
represent the values that are available tothe function. If there is more than one output parameter, the
output parameters are written in square brackets.
For example, we could write a Matlab function to find the volume and surface area of a cylinder with
given height and diameter. The information used is the diameter and the height, so these will be the
input parameters. The values to be calculated are the volume and surface area;these will be the output
parameters.
function [vol,area]=mycylinder(d,h)
% calculates the volume (vol) and surface area (area) of
% cylinder whose diameter is d and height h
r=d/2;
vol=pi*r^2*h;
area=2*pi*r^2+2*pi*r*h;
To use the function in a script file or in the Command Window, we give the variable names for the results
in the output parameters, and the values (or variables containing them)in the input parameters. For
example to find the volume and surface area of a cylinder whose diameter is 4 and height 5, we would
write
[v,a]=mycylinder(4,5)
If the diameter and height of the cylinder we are interested in are contained in variables diam and ht, we
write
[v,a]=mycylinder(diam,ht)

2

Suppose we want to make a table of the volume and surface area of cylinders whose...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Numerical Algorithms For Evaluating Sobol
  • A case for multicast algorithms
  • Algorithms for distributed constraint satisfaction
  • Hybrid Algorithms For The Constraint Satisfaction Problem
  • Bioinspired algorithms for internet route optimization
  • Numerical Methods
  • matlab
  • matlab

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS