Solve a system of nonlinear equations that contain Lambert-W function - matlab

I'm trying to solve a non-linear system of equations using Newton-Raphson method. The problem is in section 2.1 from this paper. It goes only up to the second iteration, and I got incorrect answer.
I used these values for iN and VN (N=1,2,3,4,5):
V1 = 1.00400400e+00;
V2 = 1.02598500e+00;
V3 = 1.05325500e+00;
V4 = 1.08812300e+00;
V5 = 1.13388700e+00;
i1 = -2.40036700e-02;
i2 = -3.59849700e-02;
i3 = -5.32552100e-02;
i4 = -7.81225600e-02;
i5 = -1.13887200e-01;
I used:
a = Iph,
b = I0,
f = n,
c = Rs,
d = Rsh,
Vth = 0.0258
Here's my code:
clear all; clc;
N = 20;
tErr = 1e-6;
syms a b c d f
V1 = 1.00400400e+00; V2 = 1.02598500e+00; V3 = 1.05325500e+00; V4 = 1.08812300e+00; V5 = 1.13388700e+00; i1 = -2.40036700e-02; i2 = -3.59849700e-02; i3 = -5.32552100e-02; i4 = -7.81225600e-02 ; i5 = -1.13887200e-01;
m1 = d*(-i1 + a + b)/(0.0258*f); m2 = d*(-i2 + a + b)/(0.0258*f); m3 = d*(-i3 + a + b)/(0.0258*f); m4 = d*(-i4 + a + b)/(0.0258*f);
m5 = d*(-i5 + a + b)/(0.0258*f); n = .0258*f; k = b*d/n;
f1 = -i1*c-i1*d+a*d-n*lambertw(b*d*exp(m1)/n)-V1;
f2 = -i2*c-i2*d+a*d-n*lambertw(b*d*exp(m2)/n)-V2;
f3 = -i3*c-i3*d+a*d-n*lambertw(b*d*exp(m3)/n)-V3;
f4 = -i4*c-i4*d+a*d-n*lambertw(b*d*exp(m4)/n)-V4;
f5 = -i5*c-i5*d+a*d-n*lambertw(b*d*exp(m5)/n)-V5;
fs = [f1; f2; f3; f4; f5];
xs = [a; b; f; c; d];
jac = [d/lambertw(b*d*exp(m1)/n), -(n*lambertw(b*d*exp(m1)/n)-b*d)/(b*(1+lambertw(b*d*exp(m1)/n))), lambertw(b*d*exp(m1)/n)*(-n*lambertw(b*d*exp(m1)/n)-i1*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m1)/n))), -i1, -(n*lambertw(b*d*exp(m1)/n)+i1*d-a*d-b*d)/((1+lambertw(b*d*exp(m1)/n))*d);
d/lambertw(b*d*exp(m2)/n), -(n*lambertw(b*d*exp(m2)/n)-b*d)/(b*(1+lambertw(b*d*exp(m2)/n))), lambertw(b*d*exp(m2)/n)*(-n*lambertw(b*d*exp(m2)/n)-i2*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m2)/n))), -i2, -(n*lambertw(b*d*exp(m2)/n)+i2*d-a*d-b*d)/((1+lambertw(b*d*exp(m2)/n))*d);
d/lambertw(b*d*exp(m3)/n), -(n*lambertw(b*d*exp(m3)/n)-b*d)/(b*(1+lambertw(b*d*exp(m3)/n))), lambertw(b*d*exp(m3)/n)*(-n*lambertw(b*d*exp(m3)/n)-i3*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m3)/n))), -i3, -(n*lambertw(b*d*exp(m3)/n)+i3*d-a*d-b*d)/((1+lambertw(b*d*exp(m3)/n))*d);
d/lambertw(b*d*exp(m4)/n), -(n*lambertw(b*d*exp(m4)/n)-b*d)/(b*(1+lambertw(b*d*exp(m4)/n))), lambertw(b*d*exp(m4)/n)*(-n*lambertw(b*d*exp(m4)/n)-i4*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m4)/n))), -i4, -(n*lambertw(b*d*exp(m4)/n)+i4*d-a*d-b*d)/((1+lambertw(b*d*exp(m4)/n))*d);
d/lambertw(b*d*exp(m5)/n), -(n*lambertw(b*d*exp(m5)/n)-b*d)/(b*(1+lambertw(b*d*exp(m5)/n))), lambertw(b*d*exp(m5)/n)*(-n*lambertw(b*d*exp(m5)/n)-i5*d+a*d+b*d)/(f*(1+lambertw(b*d*exp(m5)/n))), -i5, -(n*lambertw(b*d*exp(m5)/n)+i5*d-a*d-b*d)/((1+lambertw(b*d*exp(m5)/n))*d)];
x = [1.2; 0.001; 1.5; 1; 15];
for i=1:N
f = double(subs(fs, xs, x));
j = double(subs(jac, xs, x));
x = x - inv(j)*f;
err = abs(inv(j)*f);
if(err<tErr)
break;
end
end
display(x);

Related

the script works normally for few data but if it accepts a matrix with a lot of data it takes too long time

That script(matlab) works normally and fast for few data but if it accepts a matrix with a lot of data it takes too long time >1hr it run with no error but very very slow. What is the reason and what i can do for that (what are the main factors that make a program slow)
clear;clc;close all;
pref=input('Please enter file prefix: ','s');
fn=[pref '.mhk'];
fr=fopen(fn,'r');
k=0;
error = false;
t = fgetl(fr);
XTH = [];
Yed = [];
while ~feof(fr)
t = fgetl(fr);
[temp,~]=sscanf(t,'%f');
if ~error
k = k + 1;
XTH(k) = temp(1);
Yed(k) = temp(2);
end
end
x = [XTH(1)];
y = [Yed(1)];
XTH=[x XTH];
Yed = transpose([y Yed]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n = length(XTH);
l = 0;
k = 0;
Vmin = 100000000;
r=0;
for u1 = 1:XTH(end)
l = l + 1;
if ismember(u1,XTH)==1
u1 = u1 + 0.1;
end
if u1>=XTH(1)+1 && u1< XTH(end)-1
for u2 = u1:XTH(end)
k = k + 1;
if u2 == u1
u2 = u2 + 1;
end
if ismember(u2,XTH)
u2 = u2 + 1;
end
XTH = [(XTH) u1 u2];
XTH = sort(XTH);
num1 = find(XTH==u1);
num2 = find(XTH==u2);
XTH = XTH(XTH~=u1);
XTH = XTH(XTH~=u2);
a11 = XTH(1:num1);
a12 = repelem(u1,length(XTH)-num1);
a1 = transpose([a11, a12]);
a2 = ones(n,1);
a31 = transpose(zeros(num1,1));
a32 = XTH(num1+1:num2-1)-u1;
a33 = repelem(u2-u1,length(XTH(num2:end)));
a3 = transpose([a31 a32 a33]);
a41 = transpose(zeros(num2-1,1));
a42 = XTH(num2:end)-u2;
a4 = transpose([a41 a42]);
A = [a1 a2 a3 a4];
AtA = transpose(A)*A;
B = Yed;
AtB = transpose(A)*B;
if det(AtA)==0
continue
end
if det(AtA)<1e-3
continue
end
solution = linsolve(AtA, AtB);
alpha1 = solution(1,1);
beta1 = solution(2,1);
alpha2 = solution(3,1);
alpha3 = solution(4,1);
beta2 = (alpha1*u1) + beta1 - (alpha2*u1);
beta3 = (alpha1*u1) + beta1 + alpha2*(u2-u1) - (alpha3*u2);
dy1 = 0;
dy2 = 0;
dy3 = 0;
Dx = XTH(end)-XTH(1);
width = 10;
for j = 1:num1
dy1 = dy1 + abs((Yed(j)-(alpha1*XTH(j)+beta1)));
end
for j = num1:num2-1
dy2 = dy2 + abs((Yed(j) - (alpha2*XTH(j)+beta2)));
end
for j = num2:n
dy3 = dy3 + abs((Yed(j) - (alpha2*XTH(j)+beta2)));
end
V(l+k) = Dx*width*(1/length(Yed))*(dy1+dy2+dy3);
if V(l+k) < Vmin && V(l+k)~=0
aa1 = alpha1;
aa2 = alpha2;
aa3 = alpha3;
bb1 = beta1;
bb2 = beta2;
bb3 = beta3;
U1 = u1;
U2 = u2;
Vmin = V(l+k);
end
end
end
end
plot(transpose(XTH), Yed, 'k')
daspect([10,1,1]);
% ax = gca;
% ax.XLim = [XTH(1) XTH(end)];
hold on;
grid on;
x11 = XTH(1);
x21 = U1;
y11 = aa1*x11 + bb1;
y21 = aa1*x21 + bb1;
plot([x11,x21],[y11,y21], 'r')
x21 = U1;
x22 = U2;
y21 = aa2*x21 + bb2;
y22 = aa2*x22 + bb2;
plot([x21,x22],[y21,y22], 'r')
x31 = U2;
x32 = XTH(end);
y31 = aa3*x31 + bb3;
y32 = aa3*x32 + bb3;
plot([x31,x32],[y31,y32], 'r')
%line(U1,aa1*U1 + bb1)
disp('Line equation 1:')
line1 = ['y = ', num2str(aa1),'*x + ',num2str(bb1)];
disp(line1)
disp('Line equation 2:')
line2 = ['y = ', num2str(aa2),'*x + ',num2str(bb2)];
disp(line2)
line3 = ['y = ', num2str(aa3),'*x + ',num2str(bb3)];
disp(line3);
vol = [num2str(Vmin),' m^3'];
disp('Minimum volume achieved:')
disp(vol)

QR factorization,matlab

%% 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

Code not working - getting a figure with no plot

Why is this simple code not working properly? I have dozens of other more complex codes based on the same template as this one, and they run faster as well, but it's just not working. I have spent hours trying to fix this.
I should have size(y) = 1001 x 11 and a graph, not just a point. Also, the warning
Failure at t=1.194435e+02. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (2.273737e-13) at time t
doesn't shed light on this.
Here is the script AB.m:
global Q q l v1 v2 v3 v4
v3 = 0.00086;
v4 = 0.000086;
D0 = 0.01;
Q = 0.01;
q = 0.001;
l = 0.001;
v1 = 0.999;
v2 = 0.999;
K0 = 1;
P0 = 1;
S10 = 100;
y0 = [S10 zeros(1,7) K0 P0 D0];
tf = 1e10;
tvec = 0:tf/1e3:tf;
options = odeset('RelTol',1e-4);
[t,y] = ode15s(#ABEqns,tvec,y0,options);
S1 = y(:,1);
S2 = y(:,2);
KS1 = y(:,3);
PS2 = y(:,4);
DS1 = y(:,5);
DS2 = y(:,6);
DKS1 = y(:,7);
DPS2 = y(:,8);
K = y(:,9);
P = y(:,10);
D = y(:,11);
Stot = S1+S2+KS1+PS2+DS1+DS2+DKS1+DPS2;
plot(t,Stot,'k','LineSmoothing','on')
axis([0 tf 0 1.1*max(Stot)])
xlabel('Time (s)')
hold on
Here is the function ABEqns.m:
function ydot = ABEqns(t,y)
global Q q l v1 v2 v3 v4
S1 = y(1);
S2 = y(2);
KS1 = y(3);
PS2 = y(4);
DS1 = y(5);
DS2 = y(6);
DKS1 = y(7);
DPS2 = y(8);
K = y(9);
P = y(10);
D = y(11);
ydot = zeros(11,1);
ydot(1) = Q+l*(DS1+KS1)-q*S1*(D+K)+v2*PS2;
ydot(2) = l*(DS2+PS2)-q*S2*(D+P)+v1*KS1;
ydot(3) = l*(DKS1-KS1)+q*(K*S1-D*KS1);
ydot(4) = l*(DPS2-PS2)+q*(P*S2-D*PS2);
ydot(5) = q*D*S1-(l+v3)*DS1;
ydot(6) = q*D*S2-(l+v4)*DS2;
ydot(7) = q*D*KS1-(l+v3)*DKS1;
ydot(8) = q*D*PS2-(l+v4)*DPS2;
ydot(9) = (l+v1)*KS1-q*K*S1+v3*DKS1;
ydot(10) = (l+v2)*PS2-q*P*S2+v4*DPS2;
ydot(11) = (l+v3)*(DS1+DKS1)+(l+v4)*(DS2+DPS2)-q*D*(S1+S2+KS1+PS2);
end
Thanks for any help.

Plot of two variables varied simultaneously, on x-axis

My following code generates a graph with a looping variable for the x-axis. Specifically, eta_22 varies from 0 to 1, with loop iteration size of 0.01.
The code below the line is the source function file.
My question is: How can I generate a graph with eta_1 varying from 0 to 1, with loop iteration size of 0.01 as well? (I want a plot of AA on the y-axis, and eta_1, eta_2 varying from 0 to 1.)
My attempts: I have tried to create nested "for" loops, but the plot itself is looping. I have tried to put the plot line outside of the "for" loops as well, but that did not work.
Thanks for any help.
global Lambda mu mu_A mu_T beta tau eta_1 eta_2 lambda_T rho_1 rho_2 gamma
alpha = 100;
TIME = 365;
eta_22 = zeros(1,alpha);
AA = zeros(1,alpha);
for m = 1:1:alpha
eta_2 = m./alpha;
eta_22(m) = m./alpha;
Lambda = 531062;
mu = (1/70)/365;
mu_A = 0.25/365;
mu_T = 0.2/365;
beta = 0.187/365;
tau = 4/365;
lambda_T = 0.1;
rho_1 = 1/60;
rho_2 = (rho_1)./(270.*rho_1-1);
gamma = 1e-3;
eta_1 = 0;
S0 = 191564208;
T0 = 131533276;
H0 = 2405659;
C0 = 1805024;
C10 = 1000000;
C20 = 1000000;
CT10 = 500000;
CT20 = 500000;
y0 = [S0, T0, H0, C0, C10, C20, CT10, CT20];
[t,y] = ode45('SimplifiedEqns',[0:1:TIME],y0);
S = y(:,1);
T = y(:,2);
H = y(:,3);
C = y(:,4);
C1 = y(:,5);
C2 = y(:,6);
CT1 = y(:,7);
CT2 = y(:,8);
N = S + T + H + C + C1 + C2 + CT1 + CT2;
HIVinf1=[0:1:TIME];
HIVinf2=[beta.*(S+T).*(C1+C2)./N];
HIVinf=trapz(HIVinf1,HIVinf2);
AA(m) = HIVinf;
end
plot(100.*eta_22,AA./1000)
_____________________________________________________________________________________________________
function ydot = SimplifiedEqns(t,y)
global Lambda mu mu_A mu_T beta tau eta_1 eta_2 lambda_T rho_1 rho_2 gamma
S = y(1);
T = y(2);
H = y(3);
C = y(4);
C1 = y(5);
C2 = y(6);
CT1 = y(7);
CT2 = y(8);
N = S + T + H + C + C1 + C2 + CT1 + CT2;
ydot = zeros(8,1);
ydot(1)=Lambda-mu.*S-beta.*(H+C+C1+C2).*(S./N)-tau.*(T+C).*(S./N);
ydot(2)=tau.*(T+C).*(S./N)-beta.*(H+C+C1+C2).*(T./N)-(mu+mu_T).*T;
ydot(3)=beta.*(H+C+C1+C2).*(S./N)-tau.*(T+C).*(H./N)-(mu+mu_A).*H;
ydot(4)=beta.*(H+C+C1+C2).*(T./N)+tau.*(T+C).*(H./N)-(mu+mu_A+mu_T+lambda_T).*C;
ydot(5)=lambda_T.*C-(mu+mu_A+rho_1+eta_1).*C1;
ydot(6)=rho_1.*C1-(mu+mu_A+rho_2+eta_2).*C2;
ydot(7)=eta_1.*C1-(mu+rho_1+gamma).*CT1;
ydot(8)=eta_2.*C2-(mu+rho_2+gamma.*(rho_1)./(rho_1+rho_2)).*CT2+(rho_1).*CT1;
end
The simplest way:
eta_1=0:1/alpha:1;
eta_2=0:1/alpha:1;
lsize=length(eta_1) % I assume eta_1 and eta_2 are of the same size
for i=1:lsize
%Update your AA(i) here
end
plot(eta_1,AA,eta_2,AA)

Perform step-by-step integral

I have this piece of code:
time = 614.4;
Uhub = 11;
HubHt = 90;
TI = 'A';
N1 = 4096;
N2 = 32;
N3 = 32;
L1 = Uhub*time;
L2 = 150;
L3 = 220;
V = L1*L2*L3;
gamma = 3.9;
c = 1.476;
b = 5.6;
if HubHt < 60
lambda1 = 0.7*HubHt;
else
lambda1 = 42;
end
L = 0.8*lambda1;
if isequal(TI,'A')
Iref = 0.16;
sigma1 = Iref*(0.75*Uhub + b);
elseif isequal(TI,'B')
Iref = 0.14;
sigma1 = Iref*(0.75*Uhub + b);
elseif isequal(TI,'C')
Iref = 0.12;
sigma1 = Iref*(0.75*Uhub + b);
else
sigma1 = str2num(TI)*Uhub/100;
end
sigma_iso = 0.55*sigma1;
%% Wave number vectors
ik1 = cat(2,(-N1/2:-1/2),(1/2:N1/2));
ik2 = -N2/2:N2/2-1;
ik3 = -N3/2:N3/2-1;
[x y z] = ndgrid(ik1,ik2,ik3);
k1 = reshape((2*pi*L/L1)*x,N1*N2*N3,1);
k2 = reshape((2*pi*L/L2)*y,N1*N2*N3,1);
k3 = reshape((2*pi*L/L3)*z,N1*N2*N3,1);
k = sqrt(k1.^2 + k2.^2 + k3.^2);
Now I should calculate
where
The procedure to calculate the integral is
At the moment I'm using this loop
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,N1*N2*N3);
E_int(1) = 1.5;
for i = 2:(N1*N2*N3)
E_int(i) = E_int(i) + quad(E,i-1,i);
end
neglecting for the k>400 approximation. I believe that my loop is not right.
How would you suggest to calculate the integral?
I thank you in advance.
WKR,
Francesco
This is a list of correction from the more obvious to the possibly more subtle. (Indeed I start from what you wrote in the final part going upwards).
From what you write:
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,N1*N2*N3);
E_int(1) = 1.5;
for i = 2:(N1*N2*N3)
%//No point in doing this:
%//E_int(i) = E_int(i) + quad(E,i-1,i);
%//According to what you write, it should be:
E_int(i) = E_int(i-1) + quad(E,i-1,i);
end
You could speed the whole thing up by doing
%//Independent integration on segments
Local_int = arrayfun(#(i)quad(E,i-1,i), 2:(N1*N2*N3));
Local_int = [1.5 Local_int];
%//integral additivity
E_int = cumsum(Local_int);
Moreover, if the known condition (point 2.) really is "... ( = 1.5 if k' = 0)", then the whole implementation should really be more like
%//Independent integration on segments
Local_int = arrayfun(#(i)quad(E,i-1,i), 2:(N1*N2*N3));
%//integral additivity + cumulative removal of queues
E_int = 1.5 - [0 fliplr(cumsum(fliplr(Local_int)))]; %//To remove queues