How does LU decomposition with partial pivoting work? - numerical-methods

I am using the method in which initially the elements on the main diagonal of L are set to ones (think that is Doolittleā€™s method, but not sure because I have seen it named differently). I know there tons of documentations, papers and books but I could not find simple examples that were not using Gauss elimination for finding L.

Partial Pivoting, as compared to full pivoting, uses row interchanging only as compared to full pivoting which also pivots columns. The primary purpose of partial pivoting as shown below in the picture and the code is to swap the rows to find the maximum u there as to avoid dividing by a very small one in that for loop which would cause a large condition number.
If you try a naive implementation of the LU decomposition and some ill-conditioned matrix like some arbitrary diagonally dominant matrix it should explode.
function [L,U,P] = my_lu_piv(A)
n = size(A,1);
I = eye(n);
O = zeros(n);
L = I;
U = O;
P = I;
function change_rows(k,p)
x = P(k,:); P(k,:) = P(p,:); P(p,:) = x;
x = A(k,:); A(k,:) = A(p,:); A(p,:) = x;
x = v(k); v(k) = v(p); v(p) = x;
end
function change_L(k,p)
x = L(k,1:k-1); L(k,1:k-1) = L(p,1:k-1);
L(p,1:k-1) = x;
end
for k = 1:n
if k == 1, v(k:n) = A(k:n,k);
else
z = L(1:k-1,1:k -1)\ A(1:k-1,k);
U(1:k-1,k) = z;
v(k:n) = A(k:n,k)-L(k:n,1:k-1)*z;
end
if k<n
x = v(k:n); p = (k-1)+find(abs(x) == max(abs(x))); % find index p
change_rows(k,p);
L(k+1:n,k) = v(k+1:n)/v(k);
if k > 1, change_L(k,p); end
end
U(k,k) = v(k);
end
end

Related

Using folve to solve implicit function V.S. Desmos

I have an implicit equation, like this:
(a1 X + b1 Y + m)*(a2 X + b2 Y + m)*(a3 X + b3 Y + m) - c = 0,
a1,a2,b1,b2,a3,b3 are certain value, c is a variant. According different c, I need to solve it to get a set of (x,y), which I will use to integrate.
The listed function I have in practice is much more complex than this, so I am confused as to why when I put this equation into the website desmos to draw this implicit function, I can get the solutions that satisfies this function and I would like to know why this is so fast for desmos and then if there is a better way to find these solutions
I using polar coordinate to solve this problem
c_range = np.linspace(0,c_max, 1000)
theta_range = np.linspace(0,pi, 1000)
for i in range(1000):
if i ==0:
c = c_range[i]
for j in range(1000):
theta = theta_range[j]
r = fsolve(#func, r0, args=(c, theta))
radius[i][j] = r
r0 = r
else:
c = c_range[i]
r0 = radius[i-1]
r = fsolve(#func, r0, args=(c, theta_range))
radius[i]= r

Problem with Octave. Can't recognize a matrix

I try to build a script on Octave and I receive this message:
error: script2: =: nonconformant arguments (op1 is 1x1, op2 is 1x10)
error: called from
script2 at line 5 column 1
My script is:
l = 20:29;
m = 30;
for i = 0:9
a(i + 1) = l / m;
end
Can someone help me fix this?
Octave allows you to assign to a non-existent name by making a scalar. You can then append to it by assigning to an index that is one past the length.
When you assign to a(1), a is created as a scalar (or 1x1 array). l / m is 1x10. That is what your error message is telling you.
There are a couple of workarounds. If you want to just accumulate the rows of a matrix, add a second dimension:
a(i + 1, :) = l / m;
If you want columns:
a(:, i + 1) = l / m;
The problem with this approach is that it reallocates the matrix at every iteration. The recommended approcach is to pre-allocate the matrix a and fill it in:
l = 20:29;
m = 30;
a = zeros(10);
for i = 1:10
a(i + 1, :) = l / m;
end
Since Octave is capable of doing matrix operations, you don't need the for loop in the first place.
I would rather write:
l = 20:29;
m = 30;
a = l / m;
This is much more efficient.

How can I save output arguments' function generated by a loop in a matrix using scilab?

(Scilab)
I'm using a function in a loop. I'd like to save every output argument in a matrix. I can display it but I want to save it.
for x = 0:loopduration:(Endtrial-120);
y = x + 120;
Deb = x;
Fin = y;
Moy = Data_Moy(Data, Deb, Fin);
disp(Moy);
end;
If Data_Moy yields a matrix with fixed sized among iterations I would use a tensor/3D matrix like this:
k = 1;
for x = 0:loopduration:(Endtrial-120);
y = x + 120;
Deb = x;
Fin = y;
Moy(:,:,k) = Data_Moy(Data, Deb, Fin);
k = k+1;
end;
then you can later display or do anything you want with the submatrices e.g
disp(Moy(:,:,1))

What's the time complexity of the following code?

What's the time complexity of the following code?
a = 2;
while (a <= n)
{
for (k=1; k <= n; k++)
{
b = n;
while (b > 1)
b = b / 2;
}
a = a * a * a;
}
I'm struggling with the outer while loop, which is loglogn, I can't understand why. How would the time complexity change if the last line was a = a * a * a * a;?
the for loop is O(n), and inner one is O(logn).
So in total, O(n*logn*loglogn)
a values would be:
a = 2 2^3 2^9 2^27 2^81 ...
and so on.
Now let's assume that the last value of a is 2^(3^k)
Where k is the number of iterations of the outer while loop.
For simplicity let's assume that a = n^3, so 2^(3^k) = n^3
So 3^k = 3*log_2(n) => k = log_3(3log_2(n)) = š¯›©(loglogn)
If the last line was a = a * a * a * a the time-complexity would remain š¯›©(loglogn) because k = log_4(4log_2(n)) = š¯›©(loglogn).
the loop is running n times and the inner loop has time complexity is log n so total time complexity is O(n log n)

Fourier coefficients using matlab numerical integration

I have been trying to display the an and bn fourier coefficients in matlab but no success, I was able to display the a0 because that is not part of the iteration.
I will highly appreciate your help, below is my code
syms an;
syms n;
syms t;
y = sym(0);
L = 0.0005;
inc = 0.00001; % equally sample space of 100 points
an = int(3*t^2*cos(n*pi*t/L),t,-L,L)*(1/L);
bn = int(3*t^2*sin(n*pi*t/L),t,-L,L)*(1/L);
a0 = int(3*t^2,t,-L,L)*(1/L);
a0 = .5 *a0;
a0=a0
for i=1:5
y = subs(an, n, i)*cos(i*pi*t/0.0005)
z = subs(bn, n, i)*sin(i*pi*t/0.0005)
end
If everything you stated within your question is correct, I would tend to solve it this way:
clc, clear all,close all
L = 0.0005;
n = 5;
an = zeros(1,n);
bn = zeros(1,n);
for i = 1:5
f1 = #(t) 3.*(t.^2).*cos(i.*pi.*t./L);
f2 = #(t) 3.*(t.^2).*sin(i.*pi.*t./L);
an(i) = quad(f1,-L,L).*(1./L);
bn(i) = quad(f2,-L,L).*(1./L);
a0 = .5.*quad(#(t) 3.*t.^2,-L,L).*(1./L);
end
I hope this helps.