Docsity

E7 Cheat Sheet spring 2023, Cheat Sheet of Matlab skills

Toggle sidebar
bg1
ORDER OF PRECEDENCE
1. ( ) →2. exponents / transpose, unary
+, unary -,negation ~ →3.
Multiplication and division →4.
Addition and subtraction →5. Colon
operator : →6. <, <=, >, >=, ==, ~= →7.
&→8. | →9. && →10. ||
Assignment works right to left
MATHEMATICAL OPERATORS
-rand(n,m) uniform rand. dist. b/w 0
and 1. µ = 0.5.
-randn(n,m) gaussian. dist. µ=0,
variance = 1
-randi(imax,m,n), if only m, size:
m x m
exp = e
log = ln
floor = rounds to -∞
ciel = rounds to ∞
fix = rounds towards 0
round = round to nearest int
Transpose:[1,2;3,4]’ →[1,3;2,4]
RELATIONALOPERATIONS ON
ARRAYS
If Aand Bare arrays of the same size,
when performing element by element
comparisons b/w the 2 arrays return a
LOGICAL arrayof the same size.
LOGICAL OPERATORS
If A and B are scalars (double or
logical), then
A&B is TRUE (1) if A and B are both
nonzero, otherwise
it is FALSE(0). This is the logical AND
operator.
A|B is TRUE (1) if either A or B are
nonzero, otherwise it
is FALSE(0). This is the logical OR
operator.
xor(A,B) is TRUE (1) if one argumentis
0 and the
other is nonzero, otherwise it is FALSE
(0). This is the
logical EXCLUSIVE OR operator.
~A is TRUE if A is 0, and FALSEif A is
nonzero. This is
the logical NEGATIONoperator.
xor(A,B) is TRUE (1) if one argument
is 0 and the other is non-zero,
otherwise it is FALSE(0).
any(L) determines if any element in
the column is nonzero
any(L,2) determines if any element
in the row is non-zero
all(L) determines if all elements in
the column are non-zero
all(L,2) determines if all elements
in the row are non-zero
ANONYMOUS FUNCTIONS
MATLABprovides a means to define
1-line functions, without storing into
files:
h=@(in1,in2,...)
expression;
ie. H1= @(x) 3.7*sin(x);
ARRAYS/MATRICES
- MATLABmatrix convention: (ROW, COLUMN)
- [...] creates a matrix.
linspace(x2, xl, n), evenly generates nevenlyspaced
including x2 and x1.
x1:step:x2
size(A) returns a 1x2 array [# row,# column]
VariableName(Index) = Expression (change value in
vector)
[r(a),r(b)] = r([a,b])or r([1:3]) = r([1,2,3])
-RESHAPE: changes the size of an array but not the values or
order of its data in memory ie. A=[2,1,-2;3,4,5];
B=reshape(A,[3,2])->[2,4;3,-2;1,5]
sum(A,2): returns sums of each row
max(A,[],2): returns max. element from rows
sum(A,1) or sum(A): returns sums of each column
A+Bis the array: A(i,j) + B(i,j), if A and B are arrays
Adding scalar to array is distributive.
find(A) returns a row-vector containing the linear indexesof
the non-zero elements of A.
Width of A and height of B must be same to multiply matrices
If accessing a matrix using a LOGICAL array, a FALSEmeans skip
the element
STRING MANIPULATION
ASCII values: A = 65, a = 97, add 32 from uppercase to lowercase
s=’Reza is the teacher’;
s([1,4,6])->[‘R’,’a’,’i’]
s(end:-1:1) →reverses string
k=strfind(str1,str2), where k is an array of indexesof the
occurrences of the shortest string within the longest string
A=[‘E’]; B=[‘7’]; C=A+B; C would be a double. Use
ASCII values
strcmp(A,B) returns 1logical or 0logicalif string A is == to string B
FUNCTIONS
Simple mathematical functions (sin, atan,log,
exp, sqrt, abs, floor)
(size, sort, sum, max)
(strmatch, upper)
(polyval, roots)
-Variables created within a functionare local and disposable
-you can pass values to input arguments by defining said function
as:
function(x2,y2,z2)
In function (x,y,z)
^does not change values of x, y,z in workspace)
SUBFUNCTIONS
Only callable by other functions inside the file. Their handle can
be passed as an output argument of the primary function.
ADVANCEDFUNCTIONS
F_handle = @functionname, maps the handle to the
function functionname to the variable f_handle and saves
this mapping. Mapping is retained even after the function
function y = f1(x)
y=x+2*exp(-x)-3;
end
—---
F1_handle = @f1
isqual(f1(0), f1_handle(0)
FOR AND WHILE LOOPS
-repeatedly executesa collection of
statements a fixed amount of time
-pausing while loop with tic toc
-tic starts a timer and toc returns
the time elapsed
-eps is the distance from 1 to the
next largest
representable double-precision
number
-eps = 2^(-52)
1 =1+eps/4 1+eps=1+3eps/4
-Definite loops (for loops) are useful
when executing the samegroup of
commands a fixed and given number
of times.
-Conditional loops (while loops) are
useful when executingthe same
group of commands repeatedly
without knowing in advance the
total number of repetitions.
num2str(x) returns ‘x =1’ to 1
and visa versa
Examples
a = [1 2 3 4 2];b =
a>=3;c = a(b), c would be size
1x2
function x =
f(y)
x = y + 3;
end
y =
f(fzero
(@f,
1)) → 0
[4;5;0]+[2,4,6] =[6,8,10;7,9,11;2,4,6]
pf3
pf4
pf5

Partial preview of the text

Download E7 Cheat Sheet spring 2023 and more Cheat Sheet Matlab skills in PDF only on Docsity!

ORDER OF PRECEDENCE

  1. ( ) → 2. exponents / transpose, unary +, unary -, negation ~ →3. Multiplication and division → 4. Addition and subtraction → 5. Colon operator : → 6. <, <=, >, >=, ==, ~= → 7. & → 8. | →9. && → 10. || Assignment works right to left MATHEMATICAL OPERATORS
  • rand(n,m) uniform rand. dist. b/w 0 and 1. μ = 0.5.
  • randn(n,m) gaussian. dist. μ=0, variance = 1
  • randi(imax,m,n), if only m, size: m x m exp = e log = ln floor = rounds to -∞ ciel = rounds to ∞ fix = rounds towards 0 round = round to nearest int Transpose: [1,2;3,4]’ → [1,3;2,4] RELATIONAL OPERATIONS ON ARRAYS If A and B are arrays of the same size, when performing element by element comparisons b/w the 2 arrays return a LOGICAL array of the same size. LOGICAL OPERATORS If A and B are scalars (double or logical), then A&B is TRUE (1) if A and B are both nonzero, otherwise it is FALSE (0). This is the logical AND operator. A|B is TRUE (1) if either A or B are nonzero, otherwise it is FALSE (0). This is the logical OR operator. xor(A,B) is TRUE (1) if one argument is 0 and the other is nonzero, otherwise it is FALSE (0). This is the logical EXCLUSIVE OR operator. ~A is TRUE if A is 0, and FALSE if A is nonzero. This is the logical NEGATION operator. xor(A,B) is TRUE (1) if one argument is 0 and the other is non-zero, otherwise it is FALSE(0). any(L) determines if any element in the column is nonzero any(L,2) determines if any element in the row is non-zero all(L) determines if all elements in the column are non-zero all(L,2) determines if all elements in the row are non-zero ANONYMOUS FUNCTIONS MATLAB provides a means to define 1-line functions, without storing into files: h=@(in1,in2,...) expression; ie. H1= @(x) 3.7*sin(x);

ARRAYS/MATRICES

  • MATLAB matrix convention: (ROW, COLUMN)
  • [...] creates a matrix. linspace(x2, xl, n), evenly generates n evenly spaced including x2 and x1. x1:step:x size(A) returns a 1x2 array [# row, # column] VariableName(Index) = Expression (change value in vector) [r(a),r(b)] = r([a,b])or r([1:3]) = r([1,2,3])
  • RESHAPE: changes the size of an array but not the values or order of its data in memory ie. A=[2,1,-2;3,4,5]; B=reshape(A,[3,2])->[2,4;3,-2;1,5] sum(A,2): returns sums of each row max(A,[],2): returns max. element from rows sum(A,1) or sum(A): returns sums of each column A + B is the array: A(i,j) + B(i,j), if A and B are arrays Adding scalar to array is distributive. find(A) returns a row-vector containing the linear indexes of the non-zero elements of A. Width of A and height of B must be same to multiply matrices If accessing a matrix using a LOGICAL array, a FALSE means skip the element STRING MANIPULATION ASCII values: A = 65, a = 97, add 32 from uppercase to lowercase s=’Reza is the teacher’; s([1,4,6])->[‘R’,’a’,’i’] s(end:-1:1) → reverses string k=strfind(str1,str2), where k is an array of indexes of the occurrences of the shortest string within the longest string A=[‘E’]; B=[‘7’]; C=A+B; C would be a double. Use ASCII values strcmp(A,B) returns 1logical or 0logical if string A is == to string B FUNCTIONS Simple mathematical functions (sin, atan,log, exp, sqrt, abs, floor) (size, sort, sum, max) (strmatch, upper) (polyval, roots)
  • Variables created within a function are local and disposable -you can pass values to input arguments by defining said function as: function(x2,y2,z2) In function (x,y,z) ^does not change values of x, y, z in workspace) SUBFUNCTIONS Only callable by other functions inside the file. Their handle can be passed as an output argument of the primary function. ADVANCED FUNCTIONS F_handle = @functionname, maps the handle to the function functionname to the variable f_handle and saves this mapping. Mapping is retained even after the function function y = f1(x) y=x+2*exp(-x)-3; end —--- F1_handle = @f isqual(f1(0), f1_handle(0)

FOR AND WHILE LOOPS

-repeatedly executes a collection of statements a fixed amount of time -pausing while loop with tic toc -tic starts a timer and toc returns the time elapsed -eps is the distance from 1 to the next largest representable double-precision number -eps = 2^(-52) 1 =1+eps/4 1+eps=1+3eps/ -Definite loops (for loops) are useful when executing the same group of commands a fixed and given number of times. -Conditional loops (while loops) are useful when executing the same group of commands repeatedly without knowing in advance the total number of repetitions. num2str(x) returns ‘x =1’ to 1 and visa versa Examples a = [1 2 3 4 2];b = a>=3;c = a(b), c would be size 1x function x = f(y) x = y + 3; end y = f(fzero (@f, 1)) → 0 [4;5;0]+[2,4,6] =[6,8,10;7,9,11;2,4,6]

the X-axis. STATISTICS [z,x]=hist(X,n), aggregates vector data X into n evenly spaced bins between min(X) and max(X). z = bin freq., x = bin center loc. BAR GRAPH: bar(x,z),xlabel(‘x’),ylabel(‘z’) μ = (^) 𝑁^1 || || if only hist(X) → 10 bins 𝑖= 𝑁 ∑ 𝑋𝑖 σ = (^) 𝑁−1^1 𝑖= 𝑁 ∑ (𝑋𝑖 − μ)^2 x = 46:binwidth:100; [z,x]=hist(X,x) PROBABILITY DENSITY FUNCTION: plot scaled density of each bin Bin density = bin.freq. / binwidth AND PDF = z/(binwidth*sum(bin freq)) Scaled Histogram = Histogram / Total area of histogram Gaussian (Normal) PDF : 𝑝(𝑥) = (^) σ 2π^1 𝑒 , μ = 0, = − (𝑥−μ) 2 2σ (^) σ Within: 1 : 68.3%, 2 : 95.5%, 3 : 99.7%σ σ σ Given a random variable X with PDF p(x) Pr(X ≤ x) = or Pr(a≤X≤ b) = = Pr(X ≤b)-Pr(X≤a) −∞ 𝑥 ∫ 𝑝(𝑦)𝑑𝑦 𝑎 𝑏 ∫ 𝑝(𝑥)𝑑𝑥

CONTINUOUS RANDOM VARIABLES (CRV)

A CRV X takes on a range of real values. Prob. dist. function associated with X: FX(x) = Pr(X ≤ x). PDF pX(x) = dFX(x)/dx ⇒ =Pr(a≤ X ≤ b). μX= 𝑎 𝑏 ∫ 𝑝𝑋(𝑥)𝑑𝑥

. = −∞ ∞ ∫ 𝑥𝑝(𝑥)𝑑𝑥 σ −∞ ∞ ∫ (𝑥 − μ)^2 𝑝(𝑥)𝑑𝑥 Y = aX + b , a new random variable Y - μY=aμX+b σ𝑌 =|a| σ𝑋 LINEAR COMBINATION OF INDEPENDENT R.Var

  • let X and Y be independent R.Var. with μX , μY, σ𝑌, σ𝑋 Z = aX + bY, a, b are constants ⇒ μZ = aμX+bμY, σ𝑍 =(( a σ𝑋 )^2 +(b σ𝑌 )^2 )½ μ changes with addition, subtraction, multiplication, division σ changes with multiplication, division PROBABILITY Experiment : a situation whose outcome depends on chance Sample Space , S = {all possible outcomes}, 𝐸𝑣𝑒𝑛𝑡𝑠 ⊆ 𝑆, Empty subsets : null or impossible events 𝑃𝑟𝑜𝑏𝑎𝑏𝑖𝑙𝑖𝑡𝑦 ∈ [0, 1] → indicates how likely an event is to occur
  • If P(A)= 0 → null event, P(A) = 1 → certain event P(A) = num times event A occurs / num of trials

r = randi(imax,n,m)⇒ an nxm array of UDRI ∈[0,imax] r = randi([imin, imax],[n,m])⇒nxm array of UDRI ∈[imin,imax] 𝐴 ∪ 𝐵 : union of events (both events)

  • if A and B are mutually exclusive: P( 𝐴 ∪ 𝐵) = P(A) + P(B)
  • if A and B NOT M.E. : P( 𝐴 ∪ 𝐵) = P(A) + P(B) - P( 𝐴 ∩ 𝐵) 𝐴 ∩ 𝐵 intersection (overlap of A and B)
  • if A and B are mutually exclusive (no overlap) P( 𝐴 ∩ 𝐵) = 0
  • if A and B are independent: P( 𝐴 ∩ 𝐵) = P(A)*P(B) 𝐴𝐶^ = ~𝐴 complement (everything outside of A) 0 ≤P(A) ≤1, P(S) = 1, P(AC) = 1 - P(A) INTERPOLATION/LINEARIZATION/TAYLOR SERIES Regression: find a function that best fits data Interpolation: find a function that exactly fits data Least-Squares Fit: δ𝑖 = 𝑦𝑖 − 𝑦𝑖 𝑓𝑖𝑡 p=polyfit(X,Y,N), p=coefficients; X,Y=data points, N=order Yi=polyval(p,Xi)solves y evaluated by Xi. By polyfit, Xi = new x-dataset. P are the coefficients for the polynomial. Ynew=interp1(X,Y,Xnew,option), option=nearest, linear, cubic Linear interp: 𝑦 = 𝑎 0 + 𝑎 1 𝑥(for 2 points) Quadratic interp: 𝑦 = 𝑎 0 + 𝑎 1 𝑥 + 𝑎 1 𝑥^2 (for 3 points)
  • Lagrange formula: 𝑃(𝑥) = , 𝑖= 𝑛+ ∑ 𝑦𝑖( 𝑗=1,𝑗≠𝑖 𝑛+ ∏ (𝑥−𝑥𝑗) (𝑥𝑖−𝑥𝑗) ) Nth^ order interp: 𝑦 = 𝑎 0 + 𝑎 1 𝑥 +... + 𝑎𝑛𝑥𝑛(for n+1 points)
  • ALWAYS use order of N–1 for smooth interp of polynomial Spline interp: 𝑦 = 𝑎 0 + 𝑎 1 𝑥 + 𝑎 1 𝑥^2 + 𝑎 3 𝑥^3 (for 4 points, 2 slopes) Linear model: 𝑦 = 𝑎 0 + 𝑎 1 𝑥 +... + 𝑎𝑚𝑥 𝑚 Non-linear model: 𝑧 = 𝑐𝑒𝑏𝑥^ ⇒ 𝑙𝑛𝑧 = 𝑙𝑛𝑐 + 𝑏𝑥, 𝑙𝑛𝑧 = 𝑦 and𝑙𝑛𝑐 = 𝑎 SPLINES AND ROOT-FINDING Termination criteria: |𝑥𝑘+1 − 𝑥𝑘| ≤ 𝑡𝑜𝑙, |𝑥𝑘+1−𝑥𝑘| |𝑥𝑘+1| ≤ 𝑡𝑜𝑙 BISECTION METHOD: 𝑥𝐿 ≤ 𝑥𝑟𝑜𝑜𝑡 ≤ 𝑥𝑅 , define 𝑥 2 = and 𝑥 1 +𝑥𝑅 2 rebracket the root until 𝑓(𝑥 2 ) × 𝑓(𝑥 1 ) < 0, repeat until the criterion is met. Linear convergence rate NEWTON RAPHSON: convergence is quadratic. 𝑥𝑛+1 = 𝑥𝑛 − , 𝑓(𝑥𝑛) 𝑓'(𝑥𝑛)

NUMERICAL DIFFERENTIATION INTEGRATION

𝑦𝑖+1−𝑦𝑖 𝑥𝑖+1−𝑥𝑖^ 𝑓𝑏𝑎𝑐𝑘'(𝑥) =^ 𝑦𝑖−𝑦𝑖− 𝑥𝑖−𝑥𝑖−1^ 𝑓𝑐𝑒𝑛𝑡𝑒𝑟'(𝑥) =^ 𝑦𝑖+1−𝑦𝑖− 𝑥𝑖+1−𝑥𝑖− x=[0:0.1:0.6]’, y=sin(x), derivative=diff(y)./diff(x); MIDPOINT REIMANN: 𝑔(𝑥) = ⇒ cumtrapz(Y) 𝑎 𝑏 ∫ 𝑓(𝑥) ≃ 𝑓(𝑥𝑚)(𝑏 − 𝑎) TRAPEZOID: 𝑏−𝑎2𝑛 [𝑓(𝑥 0 ) + 2𝑓(𝑥 1 ) +... + 2𝑓(𝑥𝑛−1) + 𝑓(𝑥𝑛)]⇒ trapz(Y) SIMPSON’S: 𝑏−𝑎3𝑛 [𝑓(𝑥 0 ) + 4𝑓(𝑥 1 ) + 2𝑓(𝑥 2 ) +... + 4𝑓(𝑥𝑛−1) + 𝑓(𝑥𝑛)] ODEs:^ 𝑑𝑦(𝑡)𝑑𝑡 = 𝑓(𝑡, 𝑦), 𝑦(𝑡 0 ) = 𝑦 0 , 𝑦𝑠(𝑡) which satisfies 𝑦𝑠(𝑡 0 ) = 𝑦 0 , for. MATLAB has 2 ODE Solvers: 𝑑𝑦𝑠(𝑡) 𝑑𝑡 = 𝑓(𝑡, 𝑦𝑠(𝑡))^ 𝑡 ≥ 𝑡 0

  1. ode45:explicit Runge-Kutta(RK) algo. BEST function to apply as first try
  2. ode24:more efficient at crude tolerances in the presence of mod. Stiff [T,Y]=ode45(@odefun,tspan,y0)odefun: func that evaluates RHS of ODE odefun ALWAYS has input argos(t and y), tspan=[t0,tfinal] y0=y(t0) (initial guess), T:column vector of time points,Y :solution arr Y(n,:)=y(T(n))’ PENDULUM: θ" + 𝑔𝐿 𝑠𝑖𝑛(θ) = 0 ⇒ 2 1st^ ODEs: 𝑦 1 = θ, 𝑦 2 = θ' ⇒ EQ 1:𝑦 1 ' = 𝑦 2 and EQ 2: 𝑦 2 ' = −𝑔𝐿 𝑠𝑖𝑛(𝑦 1 ) ⇔ 𝑦' = 𝑓(𝑡, 𝑦) Or 𝑓 1 (𝑡, 𝑦 1 , 𝑦 2 ) = 𝑦 2 and 𝑓 2 (𝑡, 𝑦 1 , 𝑦 2 ) =− 𝑔𝐿 𝑠𝑖𝑛(𝑦 1 ) so 𝑦 = [𝑦 1 ; 𝑦 2 ] and 𝑦' = [𝑦 1 '; 𝑦 2 ']in MATLAB: function ydot = ydot_pend1(y,g1) %gl=g/L ydot = [y(2);-gl*sin(y(1))]; end gl=9.81; ydot_pend = @(t,y) ydot_pend1(y,gl); y0=[pi/4;0]; tspan=[0,5]; [T,Y]=ode45(ydot_pend,tspan,y0); %plot theta subplot(2,1,1);plot(T,Y(:,1)) %plot theta’ subplot(2,1,2);plot(T,Y(:,2))

may not converge. But is faster than bisection 1−𝑥^1 =^ ,^ ,^ ,^ , 𝑛= ∞ ∑ 𝑥𝑛−1^ 𝑒𝑥^ = 𝑛= ∞ ∑ 𝑥 𝑛− (𝑛−1)! 𝑐𝑜𝑠(𝑥) =^ 𝑛= ∞ ∑ (− 1)𝑛−1^ 𝑥 2𝑛− (2𝑛−2)! 𝑠𝑖𝑛(𝑥) =^ 𝑛= ∞ ∑ (− 1)𝑛−1^ 𝑥 2𝑛− (2𝑛−1)! 𝑙𝑛(1 + 𝑥) = , 𝑛= ∞ ∑ (− 1)𝑛−1 𝑥 𝑛𝑛 𝑎𝑟𝑐𝑡𝑎𝑛(𝑥) = 𝑛= ∞ ∑ (− 1)𝑛−1 𝑥 2𝑛−12𝑛− LINEAR ALGEBRA If columns of A = rows of B, you can perform MATRIX multiplication 𝐶(𝐼, 𝐽) = (Ith^ row of A x Jth^ column of B) 𝑘= 𝑚 ∑ 𝐴(𝐼, 𝑘) * 𝐵(𝑘, 𝐽) 𝑥 1 + 𝑥 2 = 5 and − 𝑥 1 + 4𝑥 2 = 4⇒ [1 1; -1; 4][x1;x2] = [5;4] mldivide(A,b) or A\b obtain solution to 𝐴𝑥 = 𝑏, if A is a square matrix If no solution, the matrix is SINGULAR , x =Inf; -Inf If 𝑑𝑒𝑡(𝐴) = 0, matrix is singular. 𝐼𝑚×𝑛 * 𝐴 = 𝐴 and𝐴 * 𝐼𝑚×𝑛 = 𝐴 If A is square and NON-SINGULAR: 𝑥 = 𝐴−1𝑏or x=inv(A)b Columns of A are L.I. when 𝐴𝑥 = 0𝑛𝑥1 ⇔𝑥 = 0𝑛𝑥 rank(A) → number of L.I. columns of A. 𝐴 × 𝐴−1^ = 𝐼. Rank(A) = num columns – number of linear dependent columns + 1 EXISTENCE of Solution: rank([A b]) = rank(A) use x = inv(A)b only if the system is of full rank UNIQUENESS: rank(A) = m, ||𝑣|| = → norm(v) 𝑘= 𝑚 ∑ 𝑣𝑘^2 LEAST SQUARES: find 𝑚 × 1 vector 𝑥^ s.t.𝑐^ = ||𝐴𝑥*^ − 𝑏|| ≤ ||𝐴𝑥 − 𝑏|| 𝑥 , A is an mxn matrix CASES:

= 𝑎𝑟𝑔(𝑚𝑖𝑛𝑥||𝐴𝑥 − 𝑏||) a) LSS = Exact Sol: 𝑐^ = ||𝐴𝑥 − 𝑏|| = 0 ⇔𝑟𝑎𝑛𝑘([𝐴 𝑏]) = 𝑟𝑎𝑛𝑘(𝐴) b) Uniqueness of LSS: 𝑥^ is unique ⇔𝑚 = 𝑟𝑎𝑛𝑘(𝐴) THE BACKSLASH OPERATOR: x=A\b. If A is a square >>p=A\y is Gauss-Jordan If A is not a square, >>p=A\y is Least Squares. When LS solution is NOT unique (n≠m), 𝑚 > 𝑟𝑎𝑛𝑘(𝐴) = 𝑟(# of LI columns of A)

p=A\y, computes a basic LC solution, which has at most 𝑟non-zero elements (GREATEST NUM OF NON0-ZERO ELEMENTS). EXACT SOLUTION DNE if 𝑟 = 𝑚 OR rank([A b]) > rank(A) then ||pinv(A)b|| ≤ ||A\b|| THE PSEUDO INVERSE FUNCTION: pinv(A). >>x=pinv(A)b solves LS solution with smallest norm. A=[1 1;1 1], b=[5 5], x2=pinv(A)*b=[2.5;2.5] 𝐿𝑆𝐸𝑟𝑟𝑜𝑟 = 𝐴 * (𝐴\𝑏) − 𝑏 | 𝐿𝑆𝑁𝑜𝑟𝑚 = 𝑛𝑜𝑟𝑚(𝐴 * (𝐴\𝑏) − 𝑏). FOR ∞NUM. OF SOLUTIONS (// lines):

  1. pinv(A)*B gives a solution with min. Norm.
  2. Add an extra row of zeros: A=[A;0 0],B=[B;0]then A\b to find a solution with max number of zeros. size(A,1) give number of rows for mat.A, size(A,2) gives number of columns mat.A PRACTICE PROBLEMS:

function[Idx,Mid]=bsearch(M,Key) Left=1; Right=numel(M); while Right>Left Mid=floor((Left+Right)/2); if M(Mid)<Key Left=Mid+1; else Right=Mid; end end if M(Left)==Key Idx=Left; else Idx=[]; end end %A=[-2,-1,0,1,2,3,4,5,6,7]; [K,Mid]=myfun1(A,5)->K=8,Mid= function f = myfun1(n) f=add(n,1,2); end function a = add(n,x0,x1) if(n==1) a=x0; else if(n==2) a=x1; else a=add(n-1,x1,x0+x1); end end %y=myfun1(3)-->y= End fh=@(x)0x+1; I11=integral(fh,0,0); I12=integral(fh,0,1); a=[I11,I12];%a=[0,1] x=[4,8,9];y=[2,2,3];%z=x./y=[2, ,3] A=[1,2,3; 2,0,1; 3,0,0]; b=[10;7;9]; x=A\b; y=x(1,1)%y= A=[1,2,3;2,4,6;3,6,9]; B=[A;A]; C=[A;A(1,:)]; D=[A;zeros(1,3)]; E=[A;ones(1,3)]; ra=rank(A); rb=rank(B); rc=rank(C); rd=rank(D); re=rank(E);%re has the greatest numerical value data={'name',['double','char'],[ 4 8 20;3 5 10]} (a) data{1,3}(1,3) (b) data{3}(1,3) (c) data{1,3}(3) %does not access value 20 (d) data{3}(5) (e) data{end}(end-1) function y = myfun1(n) if n== y= else y=10myfun1(n-1) end %computes y=10^n y=fib(6)/fib(7) = 5/ function f = myfun1(list) possible1=list(1); possible2=myfun1(list(2:end)); if possible1>possible f=possible1; else f=possible2; end %error: index exceed mat. dim end tspan=linspace(0,10,101)'; odefun=@(t,x)t; [a,b]=ode45(odefun,tspan,0); [c,d]=size(a);%c=101,b(1)=0,a=tspan x=linspace(0,100,101); dx=diff(x); y=linspace(0,200,101); dy=diff(y); dydx=dy./dx; n=length(dydx); plot(x,dxdy)%dydx not scalar a=[3,1,5,2,1]; da=polyder(a); a1=polyint(da); ia1=polyint(a1); a2=polyder(ia1);%a2 same as a I1=trapz([1,9]); I2=trapz([2,3],[1,9]); I3=trapz([2,2.5],[1,9]); I4=trapz([1,1.5,2.5],[2,4,6]); I5=trapz([2,3,4]);%I2≠ a=[1,-2,-3,0,-1,-2];%a=a(a<0)=[-2, -3,-1,-2] A=[1,2,3;2,0,1;3,0,0]; b=[10;7;9]; x=A\b; y=x(1,1) N=10000;A=5randn(N,1)+2rand(N,1; B=mean(A),%B= r1 = 2 + 3randn(50000,1) r2 = 1 + 5randn(10000,1) a = mean(r1) b = mean(r2) c = std(r1) d = std(r2) %largest numerical value e = a+b m1 = mean(X); m2 = mean(5+X); m3 = mean(3X); s1 = std(X); s2 = std(5+X); %s2 is greater than s1 is NOT true s3 = std(3X); C = {'a';'b'} C = [C {1}] %one of theses doesn’t append 1 C{end+1} = 1 C(end+1) = {1} C = { C{:} 1} C(end+1) = 1 %one of these doesn’t append 1 (a) 1-erf(-1) (b) erf(1/√2)/2+½ %I think this is the answer (c) erfc(-1)/ (d) erfc(−1/√2) (e) erfc(-1/2)/