280 likes | 487 Views
MATLAB. Running MATLAB. Basic operations in MATLAB. Lowest. Highest. 5+3/3*9. Display formats. Variables. Up to 31 characters Case sensitive Start with a letter Information about variables WHO WHOS. Command Window Command History Workspace Current Directory. MATLAB Environment.
E N D
Running MATLAB • Basic operations in MATLAB Lowest Highest 5+3/3*9
Variables • Up to 31 characters • Case sensitive • Start with a letter • Information about variables • WHO • WHOS
Command Window Command History Workspace Current Directory MATLAB Environment
Mathematical functions “pi”is a predefined variable (= 3.14....)
Vectors and vector computation • first_nb : step : last_nb • (y = 2 : 0.5 : 4) y=[2 2.5 3 3.5 4] • *y = 1 : 3 y = [1 2 3] Default step is 1 • linspace(first_nb , last_nb , n_points) • (y = linspace(2 , 4 , 5)) y = [2 2.5 3 3.5 4] • z = [x y] • x=[1 2 3], y=[4 5] z = 1 2 3 4 5 • Accesing elements: x(3), x(2:4), x(1:2:5), x([3 1 4]) • Column vector • x = [1;2;3;4] , x = [1 2 3 4]’ • Array operations (mult.(*), div.(/), add.(+),.. )
Matrices and matrix computation • x = [1 2 3;4 5 6;7 8 9], x = [1:2:7 ; 3:-1:0 ; -1:2] • Special matrices • zeros(3,2) • ones(3,2) • eye(3) (identity matrix) • size(a), length(b) • Matrix operations • det(x), inv(x) • System of linear equations 0 0 0 0 0 0
Input-Output Commands • Input • variable = input(‘text’) r = input(‘ Enter a number ’) • Output • disp(x), disp(‘the value is ’), • disp ( [ ‘ The value is ’ num2str( x ) ] )
2D Graphics • plot(x , y), plot(x , y , ’r+’)
x = -pi:.1:pi; y = sin(x); plot(x,y) x = -pi:.1:pi; y = sin(x); plot(x,y,’m*’)
2D Graphics • fplot(‘function’, [xmin xmax ]) • fplot('sin(x.*x)', [0 4])
2D Graphics • xlabel('x values'), title('exp(-x) and x*x curves') • title(‘text’) writes the text as a title at the top of the current plot • xlabel(‘text’) adds text to the current plot beneath the x-axis • ylabel(‘text’) adds text to the current plot beside the y-axis • grid on adds grid lines on the current axes • grid off takes them off • text(x,y,’text’) adds text to the location identified by the point (x, y) • gtext(‘text’) text is positioned at a location by pressing the mouse • Try to run this code: • plot([5 5 4 3 2],[8 2 1 1 2],’r', [4 6],[8 8],‘y'); xlim([0 10]); ylim([0 10]);
3D Graphics • >> t=[0:pi/30:6*pi]; x=t.*cos(t); y=t.*sin(t); z=t; plot3(x,y,z)
Script files • File New M-file • sample.m • % roots of the quadratic equation ax2+bx+c=0 • a =input('Enter the coefficient a: '); • b=input('Enter the coefficient b: '); • c =input('Enter the coefficient c: '); • disc=b*b-4*a*c; • x1=(-b+sqrt(disc))/(2*a); • x2=(-b - sqrt(disc))/(2*a); • % Displays the roots • disp(['Roots are ',num2str(x1),' and ',num2str(x2)])
Relational Operators <Less than <=Less than or equal > Greater than >=Greater than or equal ==Equal ~=Not Equal Logical Operators ~not &and |or Operators in Matlab Priority Increases
if – else – end • if expression-1 • commands-1 • elseif expression-2 • commands-2 • . • . • . • elseif expression-(n-1) • commands-(n-1) • else • commands-n • end • A=[1 2; -3 6]; • if det(A)>0 • Ainv=inv(A); • disp(Ainv) • end
switch switch (selector) case label-1 commands-1 case label-2 commands-2 . . . case label-n commands-n otherwise commands-m end switch (det(a)) case 1 b=a'; disp(b) case 2 b=a*a; disp(b) end
for loops for x=array commands end for num=[6 37 23 -1] disp([num2str(k), ' th element is ', num2str(num)]) k=k+1; end
while loops while expression commands end while x>=0 y=y+x; x=x-1; disp(y) end
User defined functions • function [output-parameters]=function-name(input-parameters) • function [x1,x2]=quadratic(a,b,c) • %Finds the roots of the quadratic equation • %ax2+bx+c=0 • disc=b*b-4*a*c; • x1=(-b+sqrt(disc))/(2*a); • x2=(-b-sqrt(disc))/(2*a); • >> [a,b] = quadratic (1, -2 ,7)
Example (islem.m) function [top,cik,bol,carp] = islem(a,b) top = a+b; cik = a-b; bol = a/b; carp = a*b; end
Example (vectorislem.m) function [top,cik,dot] = vectorislem(a,b) top = a+b; cik = a-b; dot = a*b'; end
The series ex is given by Write a MATLAB script to find the sum of the series while the value of the current term is greater than to the variable tol. Your program should input x and tol and should output the sum with proper messages. The result should be checked by using the MATLAB function exp(). Example 1
x =input('Enter a value for x of exp(x)'); tol =input('Enter the tolerance'); k=1; sum=1; term=1; fact=1; sq=x; format long; while term>tol term=sq/fact; % a single term is calculated: xn / n! sum=sum+term; % calculated term is added to general sum sq=sq*x; % nextxn+1is calculated fact=fact*(k+1); % next(x+1)! is calculated k=k+1; end disp(sum) disp(term) Possible Matlab Code
Example 2 • Write a MATLAB function to generate and return the matrix in the form • Your function should accept the parameters d, t, s and the size of the square matrix.
Possible Matlab Code function matrix = matrix_ex(d,t,s,N) sM = [zeros(N-1,1),eye(N-1);zeros(1,N)]*s; % superior 1's tM = [zeros(1,N);eye(N-1),zeros(N-1,1)]*t;% lower 1's dM = eye(N)*d; % identity matrix = sM+tM+dM;