math lab Flashcards
(51 cards)
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/var name
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
matrix
TO DISPLAY COLUMN VECTOR
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 ]
%same number of elements need to be present
1 2 3 4
4 5 6 7
7 8 9 10
disp(a)
elementary math built
trigonometry function
other functions
sqrt(x), n*nthroot(x,n) =>
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)
inverse
asin(x), asind(x), acos(x), acosd(x)
other function
round(x) (nearest integer), fix(x)=> Round towards 0, => chops fraction part to whole part
ceil(x) => round towards + infinity
ceil(27/2) => 14
floor(x) => rounds ‘-‘ infinity
floor(27/2) => 13
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;
z=exp(a)sin(x)+10sqrt(y)
z=2.829039804
special type of arrays
four types of arrays are:
zeros(), +> zero array zeros(5) => 5 by 5
eye(x,y) => 1 diagonal towards left to right
ones(x,y), => pure ones
rand() => random num in range ex:
rand(3,5) (range def (0,1))
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 group of statements that together perform a task.
functions are defined in separate files.
Functions can accept more than one input arguments and
may return more than one output arguments.
function_name =>same as file name
(without the m.extension)
function [sum,prod]= my name (a,b,c)
sum = a+b+c;
prod=abc
end
command window
» funcnam(20,21,22)
ans =63
>[s,p]=funcname(20,21,22)
s = 63, p =9240
%s,p are just vars can be anything, even a,b
for loop syntax
while loop syntax
all conditional statements
repeat a statement 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
decides whether a particular block of code
has to be executed or not, based on Boolean
condition. condition is true, it executes
if-else
» number = 25
if number<20
fprintf(‘The number is greater than 20’)
else
fprintf(‘The number is not less than 20’)
end
if-elseif-elseif-else-end
number of end statements depend on the number of if statements
switch mat lab
is used to test for the equality against a set of known values
grade = ‘B’;
switch(grade) case 'A' fprintf('Excellent!\n'); case 'B' fprintf('Well done\n'); case 'C' fprintf('Well done\n'); case 'D' fprintf('You passed\n'); case 'F' fprintf('Better try again\n'); otherwise fprintf('Invalid grade\n'); end
plot y=x^2
The script file is as follows:
»_space; x = [-100:20:100]; 20
is increments, and the others are the range
» y = x.^2; [Here the “dot” is necessary]
»_space; plot(x, y)
- sinx:»_space; t = [0:0.1:2*pi]
» a = sin(t);
» plot(t,a)
3D graph examples
3D surface map for the function
𝑧=𝑥𝑒^(−(𝑥^2+𝑦^2 ) )
[x,y] = meshgrid(-2:.0.2:2);
z = x.* exp(-x.^2 - y.^2);
surf(x,y,z)
surf to create a surface plot
mesh grid points over the domain of the function(the range of value(horizontal and vertical lines on the 3D-shape)
another 3d graph examples
𝑧=sinx+cosy
[x,y] = mesh grid(-2:.2:2);
%default coordinates
»_space; z=sin(x)+cos(y);
»_space; surf(x,y,z)
surf to create a surface plot
mesh grid points over the domain of the function(the range of values)
xlabel and y label commands for x, y axis for graphs
> > x = [0:0.01:10];
»_space; y = sin(x);
»_space; plot(x, y), xlabel(‘x’), ylabel(‘Sin(x)’), title(‘Sin(x) Graph’)
polar coordinates
polarplot(theta,rho).
theta=>angle in radians rho=> radius value for each point/height of sin cos wave combo
> > theta = 0:0.01:2pi;
rho = sin(2theta).cos(2theta);
polarplot(theta,rho)
another example of polar coordinates
Plot the curve 𝑟=1−𝑠𝑖𝑛𝜃
> > theta = 0:0.01:2*pi;
rho = 1 - sin(theta);
polar(theta, rho)
polarplot(theta,rho).
theta=>angle in radians rho=> radius value for each point/height of sin cos wave combo
parametric curve
x=sin(t); y=cos(t); z=t
fplot3(xt,yt,zt), whichplots the parametric curve xt=x(t), yt=y(t), and zt=z(t) over the default interval−5<𝑡<5
default parameter range[-5 5].
> > syms t
xt = sin(t);
yt = cos(t);
zt = t;
fplot3(xt,yt,zt[-5,5])