I need some help answering this question : How do I find the complexity of the matrix B and f knowing that I have this code :
d = 123456;
randn('state',d); rand('state',d);
n = 1000; A = diag(1+rand(1,n));
k = 5500; r = randperm(n*n,k);
A(r) = A(r) + rand(1,k);
L = tril(A); U = A-L;
x = rand(n,1);
b = A*x;
B = -L\U; f = L\b;
xit = zeros(n,1);
nit = 100;
res = zeros(nit,1);
incr = zeros(nit,1);
err = zeros(nit,1);
for it = 1:nit,
xit0 = xit;
xit = B*xit+f;
res(it) = norm(b-A*xit);
incr(it) = norm(xit-xit0);
err(it) = norm(xit-x);
end
Thanks in advance
Related
function Test()
a = 2;
b = 1;
c = 0.5;
q = 0.001;
r = 10;
function F = Useful(x) %calculates existing values for x with size 11
eq1 = (1*(0.903*x(2))^(-1))-(0.903*x(1));
eq2 = (1*(0.665*x(3))*(0.903*x(2))^(-1))-0.903*x(4);
eq3 = (1*(0.399*x(5))*(0.903*x(2)))-0.665*x(6);
eq4 = (1*(0.399*x(5))*(0.903*x(2))^2)-0.903*x(7);
eq5 = (1*(0.399*x(5))*(0.903*x(2))^3)-1*x(8);
eq6 = (1*(0.665*x(3))*(0.399*x(5))*(0.903*x(2)))-1*x(9);
eq7 = (1*(0.665*x(3))*(0.399*x(5))*(0.903*x(2))^2)-0.903*x(10);
eq8 = (1*(0.665*x(3))*(0.399*x(5)))-0.903*x(11);
eq9 = x(3)+x(4)+x(9)+x(10)+x(11)-a;
eq10 = x(5)+x(6)+x(7)+x(8)+x(9)+x(10)+x(11)-b;
eq11 = x(2)+x(6)+2*x(7)+3*x(8)+x(9)+2*x(10)-x(1)-x(4)-c;
F = [eq1;eq2;eq3;eq4;eq5;eq6;eq7;eq8; eq9; eq10; eq11];
end
Value(1,1) = 0;
for d = 2:100
x = fsolve(#Useful,x0,options); %Produces the x(1) to x(11) values
Value(1,d) = (x(3)+x(5))*d+Value(1,d-1); %Gives a new value after each iteration
a = a-x(3);
b = b-x(5);
c = c-x(2);
end
function Zdot = rhs(t,z) %z = (e1,e2,e3,e4,e5)
Zdot=zeros(5,1);
Zdot(1) = -1*z(1);
Zdot(2) = 1*z(1);
Zdot(3) = 1*z(1) - 1*z(2)*z(3);
Zdot(4) = 1*1*z(1) - Value(1,100)*H(z(3))*z(4)*z(4);
Zdot(5) = Value(1,100)*H(z(3))*(z(4));
end
function hill = H(x)
hill = q/(q+x^r);
end
[T,Y] = ode15s(#rhs, [0, 120], [1, 0, 1, 0, 0]); %Solve second function with values giving z(1) to z(5)
plot(T,Y(:,5))
end
I'm wondering, is it possible to pass on each Value obtained (Value (1), Value (2)... so on), into "function Zdot" or is only the final value possible to pass on? Essentially is this possible to implement:
function Zdot = rhs(t,z) %z = (e1,e2,e3,e4,e5)
Zdot=zeros(5,1);
Zdot(1) = -1*z(1);
Zdot(2) = 1*z(1);
Zdot(3) = 1*z(1) - 1*z(2)*z(3);
Zdot(4) = 1*1*z(1) - Value(1,d)*H(z(3))*z(4)*z(4);
Zdot(5) = Value(1,d)*H(z(3))*(z(4));
end
Any insights would be much appreciated and I would be extremely grateful. Thank you in advance!
%% Gram—Schmidt as Triangular Orthogonalization
clear
M = [1,1,1; 1,1,0; 1,1,9]
[m,n] = size(M);
Q = M;
Rinv = eye(n);
for i = 1:n
Ri = eye(n);
Ri(i,i) = 1 / norm(Q(:,i));
for j = i+1:n
Ri(i,j) = -Q(:,i)'*Q(:,j) / norm(Q(:,i));
end
Q = Q*Ri;
Rinv = Rinv*Ri;
end
Q
R = inv(Rinv)
The question: Q*R gives M butQ is not orthagonal.
This should return the correct result:
clear();
M = [1,1,1; 1,1,0; 1,1,9];
[m,n] = size(M);
Q = zeros(m,n);
R = zeros(n,n);
for j = 1:n
v = M(:,j);
for i = 1:j-1
R(i,j) = Q(:,i).' * M(:,j);
v = v - (R(i,j) * Q(:,i));
end
R(j,j) = norm(v);
Q(:,j) = v / R(j,j);
end
Alternatively, you could give a try to the following implementation I found on Matlab File Exchange (it contains many know variants of the algorithm): https://it.mathworks.com/matlabcentral/fileexchange/55881-gram-schmidt-orthogonalization
I have a while loop and I've been unable to determine how to store the values successfully. Any help would be greatly appreciated.
n = 0; a = 21; b = 0; c = 0; y = 37.6991; T = 18.5; z = 0.1591; d = 0; f = 15.3049; q = 2.2391e4; e = 5; x = 60;
while n < x
a = a + b
c = y*(T-a)/z;
d = f*c;
b = d/q;
n = n + e;
end
The value I'm trying to store is a, I can tell the values are correct inside the loop but just can't seem to store the values correctly.
Another approach would be to recognise that it is a relatively simple recurrence relation:
n = 0; a = 21; y = 37.6991; T = 18.5; z = 0.1591; f = 15.3049; q = 2.2391e4; e = 5; x = 60;
while n < x
a(end+1) = a(end) + f * y * (T - a(end)) / (q * z);
n = n + e;
end
This calculation can also be vectorised, but if you want exactly the same output you need to be a little bit careful:
n = 5:5:55; y = 37.6991; T = 18.5; z = 0.1591; f = 15.3049; q = 2.2391e4; a0 = 21;
alpha = f * y * T / (q * z);
beta = (1 - f * y / (q * z)).^(0:length(n))
a = a0 * beta + alpha * [0 cumsum(beta(1:end-1))];
The code seems to lose clarity (to me) when vectorised, so I would probably prefer the loop in this scenario.
Try this :
counter = 1;
n = 0; a = 21; b = 0; c = 0; y = 37.6991; T = 18.5; z = 0.1591; d = 0; f = 15.3049; q = 2.2391e4; e = 5; x = 60;
var = zeros(1,12);
while n < x
a = a + b;
c = y*(T-a)/z;
d = f*c;
b = d/q;
n = n + e;
var(counter) = a;
counter = counter+1;
end
I added a variable called var which is a vector that stores the values of a. In order t osave runtime i initialized it to the expected size of the variable var = zeros(1,12); (This is not strictly required but is recommended.
I'm stuck on K2 as it brought up this error:
In an assignment A(I) = B, the number of elements in B and
I must be the same.
I ran the debugger and I found out that J4 alone was a vector while other variables were all scalar.
How can I resolve this error to have the plot?
Here is the code that I ran.
h1 = 1*10^-6;
h2 = (10:10:1500)*10^-6;
a = 62.5*10^-6;
b = a+h1;
c = b+h2;
alpha_1 = 0.55*10^-6;
alpha_2 = 17.2*10^-6;
alpha_3 = 14.2*10^-6;
zeta = 6.3*10^-6;
P11 = 0.121;
P12 = 0.27;
neff = 1.456;
U1 = 0.17;
U2 = 0.32;
U3 = 0.31;
E1 = 0.74*10^11;
E2 = 1.08*10^11;
E3 = 1.96*10^11;
n = 1;
while(n<=150)
J1(n) = E2*(b^2-a^2)*(1-U1)-E1*a^2*(1-U2)-E1*b^2*(1+U2);
J2(n) = 2*E1*b^2;
J3(n) = E1*E2*(b^2-a^2)*(alpha_2 - alpha_1);
J4(n) = 2*E3*(c(n)^2-b^2)*a^2;
J5(n) = E2*(b^2-a^2)*(1-U3)*b^2+E2*(b^2-a^2)*(1+U3)*c(n)^2-E3*(c(n)^2-b^2)*(1+U2)*a^2-E3*(c(n)^2-b^2)*(1-U2)*b^2;
J6(n) = E2*E3*(c(n)^2 - b^2)*(b^2-a^2)*(alpha_2-alpha_3);
K1(n) = ((alpha_3-alpha_1)*E3*(c(n)^2-b^2)+(alpha_2-alpha_1)*E2*(b^2-a^2))/(E1*a^2+E2*(b^2-a^2)+E3*(c(n)^2-b^2));
K2(n) = (J2*J6-J3*J5)/(J2*J4-J1*J5);
Sr(n) = (neff^2/2)*(P11+P12)*(((1-U1)*K2/E1)-U1*K1);
Sz(n) = (1+P12)*(K1-(2*U2*K2/E1));
St(n) = alpha_1+zeta;
Km(n) = St+Sz+Sr;
n=n+1;
end
plot(h2,Km)
To recap what was already said in one answer, here's how I would modify the code:
h1 = 1e-6;
h2 = (10:10:1500)*1e-6;
a = 62.5*1e-6;
b = a+h1;
c = b+h2;
alpha_1 = 0.55*1e-6;
alpha_2 = 17.2*1e-6;
alpha_3 = 14.2*1e-6;
zeta = 6.3*1e-6;
P11 = 0.121;
P12 = 0.27;
neff = 1.456;
U1 = 0.17;
U2 = 0.32;
U3 = 0.31;
E1 = 0.74*1e11;
E2 = 1.08*1e11;
E3 = 1.96*1e11;
% pre-allocate variables
J1 = zeros(size(h2));
J2 = zeros(size(h2));
J3 = zeros(size(h2));
J4 = zeros(size(h2));
J5 = zeros(size(h2));
J6 = zeros(size(h2));
K1 = zeros(size(h2));
K2 = zeros(size(h2));
Sr = zeros(size(h2));
Sz = zeros(size(h2));
for n=1:length(h2)
J1(n) = E2*(b^2-a^2)*(1-U1)-E1*a^2*(1-U2)-E1*b^2*(1+U2);
J2(n) = 2*E1*b^2;
J3(n) = E1*E2*(b^2-a^2)*(alpha_2 - alpha_1);
J4(n) = 2*E3*(c(n)^2-b^2)*a^2;
J5(n) = E2*(b^2-a^2)*(1-U3)*b^2+E2*(b^2-a^2)*(1+U3)*c(n)^2-E3*(c(n)^2-b^2)*(1+U2)*a^2-E3*(c(n)^2-b^2)*(1-U2)*b^2;
J6(n) = E2*E3*(c(n)^2 - b^2)*(b^2-a^2)*(alpha_2-alpha_3);
K1(n) = ((alpha_3-alpha_1)*E3*(c(n)^2-b^2)+(alpha_2-alpha_1)*E2*(b^2-a^2))/(E1*a^2+E2*(b^2-a^2)+E3*(c(n)^2-b^2));
K2(n) = (J2(n)*J6(n)-J3(n)*J5(n))/(J2(n)*J4(n)-J1(n)*J5(n));
Sr(n) = (neff^2/2)*(P11+P12)*(((1-U1)*K2(n)/E1)-U1*K1(n));
Sz(n) = (1+P12)*(K1(n)-(2*U2*K2(n)/E1));
end
St = alpha_1+zeta;
Km = Sz+Sr+St;
plot(h2,Km)
Notes:
I have used a for loop to ensure the vector lengths are consistent with h2
I have pre-allocated the variables for speed
I have added various (n) to K1, K2, J1, J2, etc... in the equations to have only scalar operations
I have moved stuff out of the for loop that didn't need to be there
This gives the following plot (in Octave)
I ran your code and I found out that the dimensions of the vectors used to compute K2 are not compatible, e.g. each of J2 and J6 is a 1-row by 2-columns vector and you cannot multiply those. This also applies for the other multiplications. Depending on what you want to compute, you should transpose either one of them in each multiplication.
FYI: J.' is the transposed version of J in MATLAB.
I have written the following code. I would like the function to return the matrices D & S but at the moment it simply returns the matrix 'ans' which is equal to S. Any suggestions would be appreciated. Thanks.
function [D,S] = sdQRS(QRS_cell)
%scales & dilates QRS complexes
m = length(QRS_cell{1}(:,1));
n = length(QRS_cell);
d1 = linspace(0.5,1.0,10);
d2 = linspace(1.0,2.0,21);
d = vertcat(d1',(d2(2:21))');
s1 = linspace(0.6,1.0,13);
s2 = linspace(1.0,1.5,18);
s = vertcat(s1',(s2(2:18))');
DIL = cell(n,1);
SCAL = cell(n,1);
for i = 1:n
DIL{i} = zeros(m,length(d));
SCAL{i} = zeros(m,length(d));
for j = 1:length(d)
DIL{i}(:,j) = interp1(QRS_cell{i}(:,1),QRS_cell{i}(:,2),QRS_cell{i}(:,1)/d(j),'linear','extrap');
SCAL{i}(:,j) = s(j)*QRS_cell{i}(:,2);
end
end
D = zeros(n);
S = zeros(n);
for k = 1:n
for l = 1:n
e = [];
t = [];
for a = 1:length(d)
e(a) = euc_dilQ(QRS_cell{k},QRS_cell{l},d(a));
t(a) = euc_scalQ(QRS_cell{k},QRS_cell{l},s(a));
end
[M,E] = min(e);
[M,T] = min(t);
D(k,l) = d(E);
S(k,l) = s(T);
end
end
You can specify a Matlab function to return any number of output values. In your case the function signature would look something like
function [D,S] = sdQRS(QRS_cell) {
d1 = linspace(0.5,1.0,10);
...
}
Now you can call that function by entering
[D,S] = sdQRS(QRS_cell);