mathlab7.pdf

19
II-Other Data Structures Besides numbers, another data type in Matlab is the character data type. Characters are designated by enclosing them in single quotes. >> C = ‘x‘ C = x >> x1=double(c) x1 = 120 >> char(65) ans = A

Transcript of mathlab7.pdf

Page 1: mathlab7.pdf

II-Other Data Structures•Besides numbers, another data type in Matlab is the

character data type.•Characters are designated by enclosing them in single

quotes. >> C = ‘x‘

C = x>> x1=double(c)

x1 =120

•>> char(65)ans = A

Page 2: mathlab7.pdf

Strings•A sequence of characters within single quotes is

referred to as a string.>> D = ‘Training' ;

•A string is a horizontal 1D character array, whose length is the number of characters. The string D above

has eight characters, and is therefore a 1-by-8 array. >> size(D)

ans = 1 8•Individual elements can be referenced as for any other

array, such as D(5) which is element ‘n'.

Page 3: mathlab7.pdf

Cell arrays•Array for which the elements are cells and can

hold other MATLAB arrays of different types.•to point to elements of cell arrayis the use of curly

braces {...}.•Using celldisp function to display cell array

» A(1,1) = {[1 4 3;0 5 8;7 2 9]};» A(1,2) = {'Anne Smith'};» A(2,1) = {3+7i};» A(2,2) = {-pi:pi/10:pi};

Page 4: mathlab7.pdf

Structures•Arrays with named data containers called fields.

» patient.name='John Doe';» patient.billing = 800.00;» patient.Ptest= [79 75 80;110 108 120];

• Also, Build structure arrays using the struct function.patient(1) = struct('name', 'John Doe', ‘billing',800,‘ptest', [79 75

80;110 108 120])

Page 5: mathlab7.pdf

Multidimensional ArraysThe first references array dimension 1, the row.

The second references dimension 2, the column.

The third references dimension 3, The page.

» A = zeros(3,3,4);» A(:,:,2) = ones(3)A(:,:,1) =

0 0 00 0 0 0 0 0

A(:,:,2) =1 1 11 1 11 1 1

A(:,:,3) =0 0 00 0 0 0 0 0

A(:,:,4) =0 0 00 0 0 0 0 0

0 0 0 0 0 0

0 0 0

Page N

Page 1

0 0 0 0

0 0 0 0

0 0 0 0

0 0 0 0

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

1 1 1 1

1 2 3 4

1 3 6 10

1 4 10 20

Page 6: mathlab7.pdf

Script and Function Files

• Script Files• Work as though you typed commands into

MATLAB prompt• Variable are stored in MATLAB workspace

• Function Files• Let you make your own MATLAB Functions• All variables within a function are local• All information must be passed to functions as

parameters• Subfunctions are supported

Page 7: mathlab7.pdf

II-Scripts and Functions•Script-files contain a sequence of Matlab commands

create a file called test1.m that contains these MATLAB commands

%this program is to help me understand the scripts im matlabx=1:0.1:15;

y=10*sin(x)-7*log10(x.^2);

plot(x,y);

grid onExecuted by typing its name

>> test1

Page 8: mathlab7.pdf

Scripts and Functions (cont’d)

Page 9: mathlab7.pdf

Displaying code and getting help

To open and edit the code , use edit command>> edit test1

• To list code, use type command• >> type test1

• The help command displays first consecutive comment lines

• >> help test1

Page 10: mathlab7.pdf

FunctionsFunctions describe subprogramsTake inputs, generate outputsHave local variables (invisible in global workspace)The names of the M-file and of the function should be the same. Comments may be placed above the function header

Page 11: mathlab7.pdf

Basic Parts of a Function M-File

function y = mean (x)% MEAN Average or mean value.% For vectors, MEAN(x) returns the mean value.% For matrices, MEAN(x) is a row vector% containing the mean value of each column.[m,n] = size(x);if m == 1

m = n;endy = sum(x)/m;

Output Arguments Input ArgumentsFunction Name

Online Help

Function Code

47

Page 12: mathlab7.pdf

Function ExampleEvaluate the function f(x,y)

[a b] = myfun(10,20);

===============================

where the M-file myfun.m is

Function [w,z] = myfun(x, y)

z = y*sin(x)+x*cos(y);

W=x+y

Page 13: mathlab7.pdf

VectorizationLoops are slow: Replace loops by vector operations!Memory allocation takes a lot of time: Pre-allocate memory!

Use profile to find code bottlenecks!

Exampletic

x=-250:0.1:250;for ii=1:length(x)

if x(ii)>=0,s(ii)=sqrt(x(ii));

elses(ii)=0;

end;end;

ticx=-250:0.1:250;s=sqrt(x); s(x<0)=0;toc;

Page 14: mathlab7.pdf

Preallocation

This code uses the function ones to preallocate the vectors (r1,r2) created in the for loop.This makes the for loop execute significantly faster.r1 = ones(1,100);

r2=ones(1,100);

for n = 1:100;

x=rand(1,n);

r1(n) = sum(x);

r2(n) = mean(x);

end

Page 15: mathlab7.pdf

Function FunctionsThat is, one function works on another function. The function

functions includeZero finding Optimization

Quadrature Ordinary differential equations

====================================================================

Create an m file for the function test.function y=test(x);

y=7*sin(x)-7*log10(x.^2)========================================

Evaluate this test function at a set of points in the interval 1 <x <20 w = 1:0.1:20;

z = test(w);

Page 16: mathlab7.pdf

Function Functions(cont’d)Then plot the function with

Plot(w,z);The output will be

Page 17: mathlab7.pdf

Function Functions(cont’d)

Find the minimum of the function using(fminsearch)

min = fminsearch(@test,17)

min = 17.3289=================================================

To evaluate the function at the minimium value,

test(min)

ans =

-24.3340

rough guess at the location of the minimum.

Page 18: mathlab7.pdf

To compute the area under the curve in the graph int = quadl(@test,0,1)

int =9.2980

if you search for a zeroz = fzero(@test,2)

you will find one outside the interval z =

2.3212

Function Functions(cont’d)

Page 19: mathlab7.pdf

Thank You