0% found this document useful (0 votes)
4 views2 pages

MT1_cheatsheet (2)

The document is a formula sheet for MATLAB, providing commands for managing input/output, variable generation, logical operations, matrix and vector functions, and plotting data. It also covers root-finding methods, error calculations, and techniques for solving sets of equations, including Cramer, Gauss elimination, and Jacobi methods. Additionally, it outlines methods for solving non-linear systems and includes built-in functions and constants.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

MT1_cheatsheet (2)

The document is a formula sheet for MATLAB, providing commands for managing input/output, variable generation, logical operations, matrix and vector functions, and plotting data. It also covers root-finding methods, error calculations, and techniques for solving sets of equations, including Cramer, Gauss elimination, and Jacobi methods. Additionally, it outlines methods for solving non-linear systems and includes built-in functions and constants.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ITU ChE KMM222E Formula Sheet

Some useful commands Manage input/output Conditionals and loops


clc Clear command window x=input(‘message’) for i=1:n procedure Iterates over procedure
clear Clear system memory disp(x) end incrementing i from 1 to n by 1
ans Last result disp(' text as string ' )
x=xlsread('file_name', 'sheet_name', 'range') while criteria
Variable generation load file_name var1 var2 procedure Iterates over procedure
x=[type vector elements] save file_name var1 var2 end as long as criteria is true(1)
Cells
x=[x1 x2] row vector fid = fopen('file_name’, ‘permission')
x=cell(a,b) a b cell array
x=[x1 ; x2] column vector fprintf(fid,'..text...%g...%g...%f... ',var1,var2,var3) if criteria 1 if criteria 1 is true do procedure 1
x{n,m} access cell n,m
j:k row vector [j,j+1,...,k] fclose(fid) closes the file with id fid procedure1
cell2mat(x) transforms cell to
j:i:k row vector [j,j+i,...,k], format short Displays 4 digits after 0 end
matrix
linspace(a,b,n) n points linearly spaced format long Displays 15 digits after 0
and including a and b num2str(x) Converts the number in x to string if criteria 1 if criteria 1 is true do procedure 1
ones(a,b) a×b matrix of 1 values mat2str(x) Converts the matrix in x to string procedure1
zeros(a,b) a×b matrix of 0 values int2str(x) Converts the integer in x to string else ,else do procedure 2
procedure2
Logical and relational operators Plot data end
Matrix and vector operations/functions && Short-Circuit AND. fig1 = plot(x,y) 2-d line plot
x=[1, 2, 3] 1×3 (Row) vector & AND set(fig1, 'LineWidth', 2) change line width to 2 if criteria 1 if criteria 1 is true do procedure 1
x=[1; 2; 3] 3×1 (Column) vector || Short-Circuit or set(fig1, 'LineStyle', '-') line style (:,--,-.) procedure1
x=[1, 2; 3, 4] 2×2 matrix | or set(fig1, 'Marker', '.') marker type (o,*,+,x) elseif criteria 2 ,else if criteria 2 is true do procedure 2
x(2)=4 change index value nr 2 ~ not set(fig1, 'color', 'red') line color procedure2
x(:) All elements of x (same as x) < less than set(fig1, 'MarkerSize', 10) marker size else , else do procedure 3
x(j:end) j'th to last element of x <= less than equal to set(fig1, 'FontSize', 14) fonts to size 14 procedure3
x(2:5) 2nd to 5th element of x > greater than subplot(a,b,c) for multiple figures in single plot end
x(j,:) all j row elements >= greater than equal to xlabel('\mu name') names x/y/z axis
x(:,j) all j column elements == equal to axis([xmin, xmax, ymin, ymax]) Sets y/x axis limits switch expression
diag(x) diagonal elements of x ~= not equal to case value1,
title('name','fontsize',22) names plot
x.*y Element by element multiplication block of statements
grid on/off adds grid to plot
x./y Element by element division case value2,
legend('x','y') adds legends
x+y Element by element addition block of statements
hold on/off add new plot to existing one
x-y Element by element subtraction ...
close all closes all figures otherwise
A^n normal/Matrix power of A
loglog(x,y) log(y) (base 10) vs log(x) (base 10) block of statements
A.^n Elementwise power of A
semilogx(x,y) y vs log(x) (base 10) end
A' Transpose
semilogy(x,y) log(y) (base 10) vs x
inv(A) Inverse of matrix
bar(x,y) plots bars break terminates execution of for/while loop
size(x) Rows and Columns return resumes excecution
pie(x) pie chart
eye(n) Identity matrix
sort(A) sorts vector from smallest to largest
eig(A) Eigenvalues and eigenvectors Some built in functions/constants
x(x>5)=0 change elements >5 to 0 Programming abs(x) absolute value
x(x>5) list elements >5 myfun1=inline(‘x^2-1’,’x’) pi 3:1415....
find(A>5) Indices of elements >5 myfun1=@(x1,x2) x1+x2; anonymous fct sum(x) sums elements in x
[A,B] concatenates horizontally function [y1,...,yN] = myfun2(x1,...,xM) writing a matlab Standard functions: sqrt(x), log(x), log10(x), exp(x), length(x), max(x), min(x),
[A;B] concatenates vertically fct round(x), sin(x), cos(x),tan(x),tanh(x),atan(x), cot(x), sec(x), csc(x)
fplot(‘myfun1', [limit1,limit2]) x=char('string 1', 'string 2', 'string 3')
Root finding methods
A. Bisection (f(x)=0) B. Regula Falsi (False position) (f(x)=0) C. Successive substitution (F(x)=x) D. Newton-Raphson (f(x)=0)
1-choose x1 and x2 such that f(x1) and f(x2) has opposite signs Given an interval (xl, xu) (l:lower, u: (Fixed point) 𝑥 =F(𝑥 )
𝑖+1 𝑖
2- locate x3=(x1+x2)/2, calculate f(x3) upper) such that f(xl).f(xu) < 0, the point 𝑓(𝑥𝑖 )
3- take x1 or x2 with f(x1) or f(x2) having the opposite sign of xr is found from the intersection of the Convergence criteria 𝐹′ 𝑥 <1 𝑥𝑖+1 = 𝑥𝑖 −
𝑓′(𝑥𝑖 )
f(x3). Then calculate x4 by dividing the distance between x3 line joining the points xl and xu with x
and x1 (or x2). axis. 𝑓(𝑥𝑢)(𝑥𝑙−𝑥𝑢)
xr= xu -
4- repeat the process until reaching the root by εa <ε 𝑓 𝑥𝑙 −𝑓(𝑥𝑢)

E. Secant (f(x)=0) Calculating Error Linear algebra


Absolute relative error % a11 𝑎12 𝑎13 𝑎𝑑𝑗𝐴
𝑓(𝑥𝑖 )(𝑥𝑖 − 𝑥𝑖−1 ) 𝐴−1 = inverse
𝑥𝑖+1 = 𝑥𝑖 − 𝑥𝑖+1 −𝑥𝑖 𝐴 = 𝑎12 𝑎22 𝑎23 |𝐴|
𝑓 𝑥𝑖 − 𝑓(𝑥𝑖−1 ) ε𝑎% = ×100 𝑎13 𝑎23 𝑎33
𝑥𝑖+1 adjA=(cofactorA)T
Absolute relative error 𝑛 𝑛
𝑓(𝑥𝑖 ) 𝐴 =෍ 𝑎𝑖𝑗 𝐴𝑖𝑗 = ෍ 𝑎𝑖𝑗 𝐴𝑖𝑗 determinant
Convergent if
𝑓 𝑥𝑖 −𝑓(𝑥𝑖−1)
<1 𝑥𝑖+1 − 𝑥𝑖 𝑖=1 𝑗=1
ε𝑎 =
𝑥𝑖+1 Aij = (-1)i+jMij Co-factor
Solving sets of equations
A. Cramer: B. Gauss elimination C. Gauss-Jordan D. Gauss-Seidel E. Jacobi
New xi values are used
System: Ax=b 𝑏𝑖 − σ𝑛𝑘=1 𝑎𝑖𝑘 𝑥𝑘 in the next iteration to
𝐷𝑖 Ax = b  Ix=b’’’  x=b’’’ 𝐴 𝑥 = 𝑏 𝑥𝑖 =
𝑥𝑖 = 𝑎𝑖𝑖 determine another x
𝐷 𝑎11 𝑎12 𝑎13 𝑏1 value
(𝑗+1)
D=det A
𝑎21 𝑎22 𝑎23 𝑏2 (𝑗+1) 𝑏𝑖 − σ𝑛𝑘=1,𝑖≠𝑘 𝑎𝑖𝑘 𝑥𝑘 Solving non-linear systems
Di= determinant of matrix 𝑥𝑖 =
𝑎31 𝑎32 𝑎33 𝑏3 𝑎𝑖𝑖 A. Successive substitution Fi(x)=x & initial guesses
obtained by replacing ith
column by vector b I x New xi values are immediately
1 0 0 𝑏′′′1 used in the same iteration to
0 1 0 𝑏′′′2 determine another x value
Partial pivoting: The rows 0 0 1 𝑏′′′3
can be switched so that the [A] in [A]{x}={b} is diagonally
dominant if B. Newton-
largest element is the pivot
[A | b | I] Raphson
element |𝑎𝑖𝑖 |> σ𝑛𝑘=1,𝑖≠𝑘 |𝑎𝑖𝑘 | fi(x)=0 &
Complete pivoting:
for all i initial guesses
columns and rows are [I | x | A-1]
searched for the largest
element and then switched

You might also like