basic operations in matlab operation symbol example addition+ 5+8 subtraction- 5-8 multiplication*...
Post on 20-Dec-2015
232 views
TRANSCRIPT
•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
MATLAB Command Example (number 15.793)
format long 15.79300000000000
format short e 1.5793e+001
format long e 1.579300000000000e+001
format hex 402f9604189374bc
format bank 15.79
format + +
format rat 5417/343
format short 15.7930
Display formats
•Up to 31 characters• Case sensitive• Start with a letter
• Information about variables•WHO•WHOS
Variables
Function Description
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 (ex )
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
• 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.(+),.. )
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 00 0
Matrices and matrix computation
• 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
• plot(x , y), plot(x , y , ’r+’)
Colour Symbol Linestyle Symbol
Yellow y Point .
Magenta m Circle o
Cyan c X-mark x
Red r Plus +
Green g Star *
Blue b Solid line -
White w Dotted line :
Black k Dash-dot line
-.
Dashed line --
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
plotxlabel(‘text’) adds text to the current plot beneath the x-axisylabel(‘text’) adds text to the current plot beside the y-axisgrid on adds grid lines on the current axesgrid off takes them offtext(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
• File New M-file• sample.m
% roots of the quadratic equation ax2+bx+c=0a = 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 rootsdisp(['Roots are ', num2str(x1),' and ',num2str(x2)])
Script files
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
if expression-1commands-1elseif expression-2
commands-2...
elseif expression-(n-1) commands-(n-1)else
commands-nend
A=[1 2; -3 6]; if det(A)>0
Ainv=inv(A); disp(Ainv) end
if – else – end
switch (selector) case label-1
commands-1 case label-2
commands-2 . . .
case label-ncommands-n
otherwise commands-mend
switch (det(a)) case 1 b=a'; disp(b) case 2 b=a*a; disp(b) end
switch
for x=arraycommands
end
for num=[6 37 23 -1] disp([num2str(k), ' th element is ', num2str(num)]) k=k+1; end
for loops
• function [output-parameters]=function-name(input-parameters)
function [x1,x2] = quadratic(a,b,c)% Finds the roots of the quadratic equation% ax2+bx+c=0disc=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
function [top,cik,bol,carp] = islem(a,b)top = a+b;cik = a-b;bol = a/b;carp = a*b;
end
Example (islem.m)
function [top,cik,dot] = vectorislem(a,b)top = a+b;cik = a-b;dot = a*b';
end
Example (vectorislem.m)
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().
...!3!2!1
132
xxx
e x
Possible Matlab Codex = 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; % next xn+1 is calculated fact=fact*(k+1); % next (x+1)! is calculated
k=k+1;enddisp(sum)disp(term)
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'stM = [zeros(1,N) ; eye(N-1) , zeros(N-1,1)] * t; % lower 1'sdM = eye(N) * d; % identitymatrix = sM + tM + dM;