math lab Flashcards
when is semicolon used
comment symbol
end of statement and when you want to hide the Matlab output for a expression
x=5; y = x+5 x=5 is not shown
% symbol
Dealing with Matrices and Arrays
2-D and 3-D Plotting and graphics
Linear Algebra
Algebraic Equations
Statistics
Data Analysis
Calculus and Differential Equations
Numerical Calculations
Integration
Transforms
Curve Fitting
Special Functions
MATLAB difference compared to other programs
While other programming languages mostly work with numbers one at a time, MATLAB is designed to operate primarily on whole matrices and arrays.
addition, subtraction, multiplication left and right division, exponentiation
+, -, *, /(5/3) (3\5) same value ^
special variables and constants
ans: most recent answer
eps: floating-point representation
I,j (imaginary unit root of -1)
inf - infinity
NaN (undefined numerical not a number)
pi = pi
what is who command and whos command
displays all the variable names we use
whos : gives size, bytes class,
clear x
clear
clc
print in matlab
it will delete x
deletes all variables
clears all text from the command window
print in matlab
fprintf
Command Window
MATHLAB editor
workspace
command history
command directory
Command Window
the main area where commands can be entered at the command line. It is indicated by the command prompt (»).
MATLAB Editor. This is where you can write, edit, and save your scripts and functions.
commands can be done there and
command window: where all the editing happens variable assignment happens
workspace:
The workspace shows all the variables created and/or imported from files.
command history:
This panel shows or return commands that are entered at the command line.
command directory:
View Folders and m-files
format short
format short e
format long
format bank
format rat
displays numbers with four decimal places
format short
7+10/3+5^1.2
x=17.232
format shorte
the command allows displaying in exponential form with 4 decimal places plus the exponent
and = 2.2922e+01
format long
15 digits after the decimal point
format bank
rounds number to 2 decimal places
format rat
gives closest rational expression resulting from a calculation
format rat
4.678*4.9
= 34177/1491
two types of vectors
row vectors and column vectors
row vector: X =[7,8,9,10]
X= 7 8 9 10
column vector X = [7; 8; 9; 10]
7
8
9
10
matrix
2D array numbers,
A = [1 2 3 4; 4 5 6 7; 7 8 9 10 ]
1 2 3 4
4 5 6 7
7 8 9 10
elementary math built
trigonometry function
other functions
sqrt(x), nthroot(x,n) n here is the root value, 2,3….=> real nth root of a real number 25 has 2 roots
exp(x), abs(x), log(x), log10(x), factorial(x)
trigonometry function
sin(x) => radians, sind(x) => degree
cos(x), cosd(x), tan(x), tand(x)
cot(x), cotd(x)
asin(x), asind(x), acos(x), acosd(x)
other function
round(x) (nearest integer), fix(x)=> Round towards 0, other words, chops fraction part
ceil(x) => round towards positive infinity
ceil(27/2) => 14
floor(x) => rounds minus infinity
rem(x,y) remainder after x/y
example question y=e^a*sin(x) + 10root(y)
if a=5, x=2, y=8
a=5; x=2; y=8;
y=exp(-a)sin(x)+10sqrt(y)
y=2.829039804
special type of arrays
four types of arrays are:
zeros(), +> zero array zeros(5) => 5 by 5
eye() => returns 1 diagonal towards left to right
ones(), => ones(x,y)
rand() => random functions with given range
ex numbers on (0,1)
rand(3,5)
0.8147 0.9134 0.2785 0.9649 0.9572
0.9058 0.6324 0.5469 0.1576 0.4854
0.1270 0.0975 0.9575 0.9706 0.8003
~= operator
logical operators symbol
determine inequality, like a not function
&. |, ~(not)
what are M-files and what is a script file
.m at the end of their name
ASCII text files
two M-files: Script files: and function files
script files:A script file contains a set of valid commands, just like you would enter directly into the Command Window.
how to create a function file matlab
A function is a group of statements that together perform a task.
In MATLAB, functions are defined in separate files.
Functions can accept more than one input arguments and
may return more than one output arguments. The syntax of the function definition line is as follows:
Note that the function_name must be the same as the file name
(without the m.extension) in which the function is written.
function [sum,prod]= my name (a,b,c)
sum = a+b+c;
prod=abc
end
on command window
For,»_space; myname(20,21,22), the output is: ans =63
For,»_space; [s,p]=myname(20,21,22), the output is: s = 63, p =9240
for loop syntax
while loop syntax
all conditional statements
repeat a statement or a group of statements for a fixed number of times.
for m=1:3
num=1/(m+1)
end
OUTPUT:
num = 0.5000
num = 0.3333
num = 0.2500
while loop:
repeatedly executes statements while condition is true.
> > a = 10;
while(a < 15)
fprintf(‘value of a: %d\n’, a);
a = a + 1;
end
%d is a format specifier, d in this case is integer
\n is next line
all conditional statements
if-end; if-else-end; if-elseif-elseif-else-end; switch case.