Download presentation
Presentation is loading. Please wait.
2
Basic operations in MATLAB Operation Symbol Example Addition+ 5+8 Subtraction- 5-8 Multiplication* 5*8 Division/ 5/8 Power^ 5^8 Lowest Highest 5+3/3*9 Running MATLAB
3
MATLAB CommandExample (number 15.793) format long15.79300000000000 format short e1.5793e+001 format long e1.579300000000000e+001 format hex402f9604189374bc format bank15.79 format ++ format rat5417/343 format short15.7930 Display formats
4
Up to 31 characters Case sensitive Start with a letter Information about variables WHO WHOS Variables
5
MATLAB Environment Command Window Command History Workspace Current Directory
6
FunctionDescription abs(x)Absolute value of x sqrt(x)Square root of x sin(x)sine of x where x is in radians cos(x)cosine of x where x is in radians tan(x)tangent of x where x is in radians ceil(x)Round towards plus infinity floor(x)Round towards minus infinity round(x)Round towards nearest integer exp(x)Exponential of x (e x ) log(x)log to base e of x sign(x)Returns sign of x rem(x,y)Remainder of x/y “pi” is a predefined variable (= 3.14....) Mathematical functions
7
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.(+),.. )
8
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 Matrices and matrix computation
9
Input variable = input(‘text’) r = input(‘ Enter a number ’) Output disp(x), disp(‘the value is ’), disp ( [ ‘ The value is ’ num2str ( x ) ] ) Input-Output Commands
10
plot(x, y), plot(x, y, ’r+’) ColourSymbolLinestyleSymbol YellowyPoint. MagentamCircleo CyancX-markx RedrPlus+ GreengStar* BluebSolid line- WhitewDotted line: BlackkDash-dot line -.-. Dashed line-- 2D Graphics
11
x = -pi :.1 : pi; y = sin(x); plot(x,y) x = -pi :.1 : pi; y = sin(x); ’m*’ plot(x,y,’m*’)
12
fplot(‘function’, [xmin xmax ]) fplot('sin(x.*x)', [0 4]) 2D Graphics
13
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 onadds grid lines on the current axes grid offtakes 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]); 2D Graphics
14
3D Graphics >> t=[0:pi/30:6*pi]; x=t.*cos(t); y=t.*sin(t); z=t; plot3(x,y,z)
15
File New M-file sample.m % roots of the quadratic equation ax 2 +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)]) Script files
16
Operators in Matlab Relational Operators < Less than <= Less than or equal > Greater than >= Greater than or equal == Equal ~= Not Equal Logical Operators ~ not & and | or Priority Increases
17
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 if – else – end
18
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 switch
19
for x=array commands end for num=[6 37 23 -1] disp([num2str(k), ' th element is ', num2str(num)]) k=k+1; end for loops
20
while expression commands end while x>=0 y=y+x; x=x-1; disp(y) end while loops
21
function [output-parameters]=function-name(input-parameters) function [x1,x2] = quadratic(a,b,c) % Finds the roots of the quadratic equation % ax 2 +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) User defined functions
22
function [top,cik,bol,carp] = islem(a,b) top = a+b; cik = a-b; bol = a/b; carp = a*b; end Example (islem.m)
23
function [top,cik,dot] = vectorislem(a,b) top = a+b; cik = a-b; dot = a*b'; end Example (vectorislem.m)
24
Example 1 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().
25
Possible Matlab Code 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: x n / n! sum=sum+term; % calculated term is added to general sum sq=sq*x;% next x n+1 is calculated fact=fact*(k+1);% next (x+1)! is calculated k=k+1; end disp(sum) disp(term)
26
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.
27
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;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.