computational methods Flashcards
what does it mean when dot = 0?
That vectors are orthogonal to each other.
What is the goal of creating a polynomial?
To estimate the relationship between x and y by solving for unknowns so that a line (y) fits the data.
What is the difference between a first and second degree polynomial?
1st degree ansatz is linear y = a0 + a1 * x
Second degree ansatz is y = a0 + a1 * x + a2 * x^2.
A second degree polynomial will give 3 columns in the matrix (coefficient, x, x^2) and 3 unknowns (a0, a1, a2).
What does it mean that an equationsystem is overdetermined?
That you have more equations than unknowns to solve for. This usually happens when trying to fit a polynomial to a dataset. The data does not fit a straight line and there is no unique solution for the equation system. We will instead find the line that give the best estimate of the relationship of the data.
This is done by solving ATAv = ATy
How do you create an equation system and fit a polynomial to a dataset?
Put the coefficients and x-values in one matrix.
- This matrix represents the equation system
a0 + a1 * x1 = y1
a0 + a1 * x2 = y2
ect.
If 2nd degree polynomial:
a0 + (a1 * x1) + (a2 * x2)
Put the unknowns in one vector
Put the y values in one vector.
This usually gives more equations than unknowns and we have to make the best estimate by solving ATAv = ATy.
Then use gaussian elimination (np.linalg.solve(ATA, ATy) to get the values for the unknowns and put them into the ansatz to get the polynomial.
Plug x-values into the polynomial. y = polynomial(x).
What is the least squares method?
How do we solve for the residuals?
A way of finding the best estimate of a relationship between x and y where you minimize the residuals between the polynomial and the datapoints.
In other words to you find the relationship that is the best approximation of the real relationship.
r = y-vector - A-matrix * the values of the unknowns.
What are norms?
The absolute size of vectors and matrices.
What is a minimum norm, 2-norm and maximum norm?
minimum norm is 1-norm v1 + v2 +v3 …ect.
It does not take the “most efficient way”. Highest value of the columns in a matrix.
2-norm is called the euclidean distance and can be calculated with pythagoras. More efficient than minimum norm.
2-norm of v:
v^2 = x^2 + y^2.
vTv.
Maximum norm is the biggest value in the whole matrix (out of all columns and rows).
When calculating norms we always use absolute values.
What is a condition number?
If you have an input error in your input data then it is going to be magnified in the output.
the condition number tells you how much it is going to multiply and is a sort of “warning sign” for how big your error will get.
We do not know the exact size of the difference between the real data and the input you used but we can calculate the norm of the difference.
How do you calculate the relative error of your solution?
relative error = relative error in input data * condition number.
If your input is exact then input machine epsilon instead because in that case you have rounding error dominating.
How can we make the condition number smaller?
By centering and scaling the data.
centering = subtract mean(x)
scaling = k( x - mean(x)) / sd(x)
ansats would be:
y = a0 + a1 * ( x - mean(x)) / sd(x)
How do we get the condition number?
in python use np.linalg.cond(ATA).
Why do we get rounding errors when doing computations in python?
A real number axis is infinite
computer number axis is discrete and finite and is representing infinity.
When you give python a number to store it is stored as the closest existing number on the computer number axis. This gives rounding errors every time we store a number.
What are bits?
Numbers are stored in finite number of bits in binary form base 2 in computer memory.
What is double precision?
64 bits.
sign (+/-) 1 bit.
exponent = 11bits
mantissa = 52 bits
Define the terms precision and mantissa?
precision(p) = number of members in mantissa.
For example:
1.001 * 10^2
mantissa is 1.001, precision is 4 and base is 10.
If we have mantissa 1.00 and 1.25 with base = 2, upper limit = 2 and lower limit = 0 of the exponent, what numbers can be stored? What is the precision?
1.00 * 2^0, 1.00 * 2^1, 1.00 * 2^2
1.25 * 2^0, 1.25 * 2^1, 1.25*2^2
precision = 3
What is the difference between binary numbers and decimal numbers?
binary has base 2
decimal has base 10
How can you convert a binary number to a decimal number?
Ex binary number 1.001
decimal = 12^0 + 02^-1 + 02^-2 + 12^-3
decimal = 1.125
Take the number in the mantissa multiplied by base with exponent like the index position of number.
How do you calculate absolute error and relative error? Do they stay the same across the entire computer number axis?
abs = a - b where a is the true value
rel = a - b / a where a is the true value
abs error get bigger with higher numbers but relative error is the same across the entire axis = machine epsilon.
What is machine epsilon?
The magnitude of the relative round off error and it is usually 10^-15.
How can we check if two values can be considered equal?
Look to see if the relative difference between the stored value and the truth is smaller than machine epsilon.
What is machine epsilon?
The magnitude of the relative roundoff error. 10^-16
What are ODEs?
Models used to describe change, often in time. The model is written as derivatives.
The solution y(t) is unknown and cannot be found. But we can use numerical methods like Euler’s and Heun’s to simulate the changes of y with time and get an approximation of y(t) by using y’(t) which is the rate of change.
When using an ODE to get an approximation of y(t), what is h?
h is the distance between the time steps.
Using smaller h will improve accuracy but will give more computations since we have to calculate the solution at every timepoint.
Because of this we want to use numerical methods that do more calculations per step which gives higher accuracy without having to lower h.
How do we use ODEs to solve for y(t) in python?
The derivative is given as a function which is then used in a solver as input. If you have more than one derivative and solution then give the derivatives as a vector.
Def (t, y, constants)
X, S = y
constants = a, b, c
yt = [x’(t), s’(t)]
return yt
solver is solve_ivp.
What will be the output of solve_ivp?
A time vector with the timepoints.
A matrix with the solutions from the different derivatives as rows and time points where the solutions were stored as columns.
If you have 9 derivatives you will get 9 rows in the matrix.
What is the difference between Euler’s and Heun’s method?
To get the y(t) in Euler’s method we calculate the tangent in only f(yi, ti).
- gives one calculation per timestep.
To get y(t) in Heun’s we find the tanget in both f(yi, ti) and f(yi+1 , ti+1) and take the average of them.
- gives double the amount of calculations per time step but giver higher accuracy and we can use bigger h to keep error tolerance.
What is discretization and discretization error?
Since we can’t compute infinity our approximations need to represent infinity. This gives many approximations.
Discretization is approximations in discrete points.
Discretization error is the error that occur because of the discretization and is dependent on h. If we use a smaller h the error decreases.