Presentation is loading. Please wait.

Presentation is loading. Please wait.

BASIC MATLAB PROGRAMMING

Similar presentations


Presentation on theme: "BASIC MATLAB PROGRAMMING"— Presentation transcript:

1 BASIC MATLAB PROGRAMMING

2 Creating a vector or a matrix
As mentioned before, matrix (and vector) as the basic element of a Matlab program. Matlab does see mattix as other languages see numbers and can perform operations in matrix directly without a for loops. Type : >> x = [ ]; >> x >> who >> whos >> y = [6; 7; 8; 9; 0; 9; 8; 7; 6] (note that is there is a semicolon at the end of the instruction, matlab doesn't expand the result of the instruction) >>y' >> z = [1 2 3; 4 5 6; 7 8 9; 0 1 2]

3 Operators The colon operator:
A way to define quickly some vectors is to use the ":" operator. Type for instance (and observe the results): Type : >> a = 0:10 >> b = -5:10 >> c = 0:2:10 >> d = 10:-2:-5 >> e = 0:0.01:4.2 >> f = -pi:0.01:pi You can see that these instructions are of the form lower limit: increment: upper limit

4 Operators An other way to define elementary vectors that only contain ones and zeros: get help on zeros, ones, randn … Try: >> g = zeros(2,4) >> h = ones(5,3)

5 Arithmetic Operators These are the standard operators. (+ plus, - minus, / divide, * multiply, ^ exponent) Try >> 3^2 >> y' >> x+y' >> x*y >> 3*x

6 Arithmetic Operators The operators ( / , * and ^) have got a matrix counterpart (respectively ./ , .* and .^), which apply the operation element by element. For instance, compare: x*y and x.*y. Type: >> x^2 >> x.^2 >> x.*x >> x*x if you type ">>whos" now, you will see that you have a lot of variables in the workspace. Type ">>clear", and ">>whos" again..

7 Matrix Operators Type A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
sum(A) % displays the sum of the columns of A. >>sum(A) >> sum(sum(A)) % dsiplays the sum of A A’ % Transpose conjugate (if complex number) else transpose. >> A’ >>sum(A’) % Sum of the lines of A (columns of A’). Note that functions can be % combined very easily.

8 Matrix Operators Try A(4,2)? Try A(4,5)?

9 For Loops Do, ima = zeros(100,100); for i = 1:100 for j = 1:100
ima(i,j) = i+j; end WHAT HAPPENS?

10 Plotting with Matlab/Graphics
Matlab contains many powerful functions for easily creating plots of several different types. Try this one: >> x=[0:0.001:10]; >> y=sin(2*x); >> figure(1);plot(x,y);xlabel(‘paksi-x’),ylabel(‘paksi-y’); >> title(‘plot y=sin 2x’);

11 Matlab Scripting files & the Editor/Debugger
You can perform operations in Matlab in 2 ways: Using Matlab Command Window Using Editor/Debugger (.m file) Example of Scripting file: % coordinate_polar % program ini untuk menukar koordinat (x,y) kepada koordinat polar. x = input ('Enter the value of x: '); y = input ('Enter the value of y: '); r = sqrt(x^2+y^2); if x>=0 theta = atan(y/x); else theta = atan(y/x)+pi; end disp('The hypoteneus is: ') disp(r) theta = theta*(180/pi); disp('the angle in degree is: ') disp(theta) Example of Matlab Function File: % program ini adalah untuk mengira luas bulatan. % A = 4 pi r^2. function [A]=luas_bulatan(r) A=4*pi*r^2; fprintf('luas bulatan adalah %f', A);

12 Example: Write a script file using conditional statements to evaluate the following function, assuming that the scalar variable x has a value. The function is for x  0, for 0  x  10, and for x  10. Use for loop to plot the above function over the interval -2  x  12. Properly label the plot. The variable y represents amplitude in volts, and the variable x represents frequency in hertz. Give title for the graph as ‘Amplitude versus frequency’. Answer: x=[-2:0.01:12]; for n=1:length(x) if x(n)<0 y(n)=sqrt(x(n)^2+1); elseif x(n)>=10 y(n)=9*sin(5*x(n)-50)+31; else y(n)=3*x(n)+1; end plot(x,y);grid; xlabel('frequency(hertz)'); ylabel('amplitude(voltz)'); title('amplitude versus frequency');

13 Plot of the amplitude versus frequency


Download ppt "BASIC MATLAB PROGRAMMING"

Similar presentations


Ads by Google