Download presentation

Presentation is loading. Please wait.

1
Matlab Basics IEF 217a: Lecture 2 Fall 2002 Sigmon and Davis, chapters 1-7

2
Loading Software Matlab Follow matlab instructions for your own machine Downloads We will eventually be downloading various matlab programs and data from the website

3
Matlab Basics Math Operations Variables Vectors Functions Fancy subscripts Program files

4
Math Operations Super calculator +,-,*,/,^, ()

5
Variables x = 1 1+x x = 10 y = 20 x*y The importance of ; (no printing) x = 1;

6
Vectors In matlab variables can be vectors Vector = list of numbers x = [1 5 2 1 5]; 2*x x+1

7
Generating Vectors : operator 1:10 1:2:10 60:-1:35 x = 1:5;

8
Parts of Vectors x(2) x(10) x(b) x(10:15), x(20:end)

9
Vector Operations (dot operators) x = [1 2 3]; y = [ 2 1 3]; x.* y x./ y x.^ y

10
Matrices x = [1 2 3; 4 5 6]; x = 1 2 3 4 5 6 rows = 2, columns = 3

11
Parts of Matrices x = 1 2 3 4 5 6 x(row, column) x(2,1) = 4 x(2,:) = 4 5 6 x(1,1:2) = 1 2

12
Functions Scalar functions: f(x) log(x), exp(x), sqrt(x) sin(x), cos(x), tan(x) Vector functions sum(x), mean(x), median(x)

13
More Functions plot(x) plot(x,’*’); Most important function!!!! Help help functionname help plot

14
Programs (mfiles) Execute commands in a file filename.m Edit using matlab editor (colors) Examples:

15
Conditions >, =, <=, ==, ~= x > 5 x < 10

16
If conditions if (x>1) x = x/2; end

17
If/else if (x<0) x = x+1; else x = -1*x; end

18
For loops for j = [1 2 3] x(j) = j+1; end

19
For Loops for j = 1:10 x(j) = 2^j; end also x = 2.^(1:10);

20
For Loops Useful for dealing with arrays Costly in terms of computer time

21
While Loops x = 20; y = 0; while (x>3) y = y+sqrt(x); x = x-5; end

22
If’s With Vectors if (a==b) disp(‘a equals b’) end What if a and b are vectors? True if every element of a equals every element of b

23
If’s With Vectors if (a~=b) disp(‘a not equals b’) end True if every element of a is not equal

24
Any and All if any(a==b) disp(‘at least one equal’) end if all(a==b) disp(‘all are equal’) end For matrices this gets more complicated

25
Fancy Subscripts (find function) x = [5 2 –3 –4 1]; y = x(find(x<0); all x values greater than zero put in y k = find(x<0); k = subscripts k = [ 3 4];

26
Program Files Filename.m test.m type: test

27
Important: Comment Lines Lines starting with % are comments x = 1; y = 2; z = x*y; % this really is not doing very much

28
User Functions Define your own functions Very important and powerful

29
Function file (positives.m) function count = positives(x) % usage: count = positives(x) % x = vector % count = number greater than zero count = sum(x>0);

30
Fancy function stuff Multiple returns function [ count, count2] = positives(x) count = sum(x>0); count2 = sum(x>2);

31
Fancy function stuff Calling functions as strings: z = feval(‘log’,5)

32
Getting and Storing Results diary filename (all results to filename) diary off (stop)

33
Load load x.dat File x.dat = 1 2 3 4 5 6 Same thing in matrix x x file must be matrix

34
Save save (saves everything to file matlab.mat) load (restores all settings) useful for taking a break, and starting where you left off

35
Save save filename x y z (save to filename.mat variables x, y, z) save filename x y z –ascii (save in ascii format)

36
Summary We will see many more of these matlab related commands as we make use of them

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google