MATLAB Basics
1. Matlab Variables
= Assignment Operator
Variable Names-
1. Start with a letter 2. Contain only letters, numbers and underscores
3. Are case sensitive
; Prevents output from being shown
pi >> Value of pi
ans is used to store a result by default
2. Creating Vectors
Vectors- Lists of numbers
x= [2,3,-1,5,18] Row Vector (Uses Commas)
xcol= [2;3;-1;5;18] Column Vector (Uses Semicolons)
Plot function: plot(x,y)
3. Creating uniformly based vectors colon operators
Basically used in creating lists of regularly spaced numbers
Vector begins with start value. Spacing is constant. Vector ends with the last value in
range.
x=-2:2; (By default spacing of one)
For Column Vector:
Use the apostrophe (‘) to transpose the row vector to the column vector!
xcol= (-2:2)’;
4. Functions
scatter (to visualise data)
imread (import an image)
lu (LU decomposition of the matrix)
Mathematical functions such as sin can take Vectors or Matrices as input
a=min(v) ..minima function for the entries of a vector
[a,I]=min(v) … Gives the location of the minima as well
5. MATLAB as a calculator
6. Calculations with Vectors
To Prevent this error we have to do an element-wise operation
This can be done by using a [dot] before the operator
Example:( ./ .* .^ )
No special operators for addition or subtraction
7. Matrix
Matrix Multiplication
● Logical Operators [&, |, ~]
● Conditional Data Selection
Suppose v=[50,65,45,0.510,941,51]
Now what we do is, we compare the elements of the array to one number
So that is done by
I=v<0.005 .. (some number)
Now, ‘I’ stores the truth values for the condition v<0.005 for each value of v. So ‘I’
is an array of size same as that of v.
To get these values, we simply do:
r=v(I) … Default operator in MATLAB.
Now to change these specific values which are below the threshold, we simply
say v(I)= 8 .. (Some value)
● FOR loop
n = 1:6 basically states how many times the loop will run, after each run, n will
be incremented.
● IF ELSE statement
Important: You should have the end statement after if to mark the end of the statement.
● Creating Multiple Plots
● Annotations
● Solving Linear Equations
Suppose you have a matrix A, b such that
Ax=b
Then the corresponding x matrix can be found by performing a left division on both
sides.
This is specially denoted by,
x = A\b Here the important tool is the backslash \
● Symbolic Calculations in MATLAB
syms command basically states the variables as symbolic variables
solve function takes x as the variable to solve for
The subs function replaces symbolic variables with numbers
vpa (Variable Precision Arithmetic)
● Passing Functions as Inputs
Basically the normal function we are dealing with here is fminsearch.
Now this function by definition will not accept the cos function. So we use fHandle
(Function Handle) to reference our variable
fHandle = @(argument) FunctionDefinition
● Numerically Solving First Order ODEs
● Higher-Order ODEs
Higher-order ODEs can also be solved by ode45 function with some additional efforts
So to solve such equations what we do is convert the Higher-Order ODEs into a System
of First Order ODEs. We start by creating new variables. One for x and subsequently
one for each derivative term excluding the highest derivative. Say we call them x1 and
x2. These are new variables. Then we use the definition of x1 and x2 to reexpress the
original higher-order ODE into an equation in x1 and x2