Error: In assignment A(I) = B, the number of elements in B and I must be the same - matlab

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.

Related

Swapping fragments of matrices Matlab

I wrote a program implementing Gaussian Elimination with Complete Pivoting:
function x = gecp(A,b)
x = b;
n = length(A);
p = 1:n;
l = b;
for k = 1:n
[i,j] = find(A(k:n,k:n)==max(abs(A(k:n,k:n)),[],'all'));
i = i+k-1;
j = j+k-1;
[A(k,:),A(i,:)] = deal(A(i,:),A(k,:));
[A(:,j),A(:,k)] = deal(A(:,k),A(:,j));
[b(i),b(k)] = deal(b(k),b(i));
[p(k),p(j)] = deal(p(j),p(k));
temp = (k+1):n;
l(temp) = A(temp,k)/A(k,k);
b(temp) = b(temp)-l(temp).*b(k);
A(temp,temp) = A(temp,temp)-l(temp).*A(k,temp);
end
x(n) = b(n)/A(n,n);
for k = (n-1):-1:1
s = 0;
for h = (k+1):n
s = s+A(k,h)*x(h);
end
x(k) = (b(k)-s)/A(k,k);
end
x(p) = x;
And it is called like this:
N = 5; A = randn(N); b = randn(N,1); x = gecp(A,b)
Unfortunately all lines containing deal function (used for swapping rows of columns of matrices), give me following (or similar) error: "Unable to perform assignment because the size of the left side is 1-by-5 and the size of the right side is 0-by-5."
Unfortunately I have no idead why would the width of these vectors be changed to 0 as I wrote excatly the same thing on both sides.

Creating a table from a variable inside a for loop

I am writing a for loop to calculate the value of four different variables. The first variable is M. M increases from 10^2 to 10^5,
M = [10^2,10^3,10^4,10^5];
The other three variables needed for the table are shown in the code below.
confmc
confcv
confmcSize/confcvSize
I first create a for loop to iterate through the four different values of M. I then create the table outside of the for loop.
How could I adjust the implementation so that the table displays all four values of M?
randn('state',100)
%%%%%% Problem and method parameters %%%%%%%%%
S = 5; E = 6; sigma = 0.3; r = 0.05; T = 1;
Dt = 1e-2; N = T/Dt; M = [10^2,10^3,10^4,10^5];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:numel(M)
%%%%%%%%% Geom Asian exact mean %%%%%%%%%%%%
sigsqT= sigma^2*T*(N+1)*(2*N+1)/(6*N*N);
muT = 0.5*sigsqT + (r - 0.5*sigma^2)*T*(N+1)/(2*N);
d1 = (log(S/E) + (muT + 0.5*sigsqT))/(sqrt(sigsqT));
d2 = d1 - sqrt(sigsqT);
N1 = 0.5*(1+erf(d1/sqrt(2)));
N2 = 0.5*(1+erf(d2/sqrt(2)));
geo = exp(-r*T)*( S*exp(muT)*N1 - E*N2 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Spath = S*cumprod(exp((r-0.5*sigma^2)*Dt+sigma*sqrt(Dt)*randn(M(k),N)),2);
% Standard Monte Carlo
arithave = mean(Spath,2);
Parith = exp(-r*T)*max(arithave-E,0); % payoffs
Pmean = mean(Parith);
Pstd = std(Parith);
confmc = [Pmean-1.96*Pstd/sqrt(M(k)), Pmean+1.96*Pstd/sqrt(M(k))];
confmcSize = [(Pmean+1.96*Pstd/sqrt(M(k)))-(Pmean-1.96*Pstd/sqrt(M(k)))];
% Control Variate
geoave = exp((1/N)*sum(log(Spath),2));
Pgeo = exp(-r*T)*max(geoave-E,0); % geo payoffs
Z = Parith + geo - Pgeo; % control variate version
Zmean = mean(Z);
Zstd = std(Z);
confcv = [Zmean-1.96*Zstd/sqrt(M(k)), Zmean+1.96*Zstd/sqrt(M(k))];
confcvSize = [(Zmean+1.96*Zstd/sqrt(M(k)))-(Zmean-1.96*Zstd/sqrt(M(k)))];
end
T = table(M,confmc,confcv,confmcSize/confcvSize)
The current code returns
T =
1×4 table
M confmc confcv Var4
_____ ____________________ ____________________ ______
1e+05 0.096756 0.1007 0.097306 0.097789 8.1622
How could I change my implementation so that all four values of M are computed?
I just modified few things.Take a look at the following code.
randn('state',100)
%%%%%% Problem and method parameters %%%%%%%%%
S = 5; E = 6; sigma = 0.3; r = 0.05; T = 1;
Dt = 1e-2; N = T/Dt; M = [10^2,10^3,10^4,10^5];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
confmc = zeros(numel(M), 2);
confcv = zeros(numel(M), 2);
confmcSize = zeros(numel(M), 1);
confcvSize = zeros(numel(M), 1);
for k=1:numel(M)
%%%%%%%%% Geom Asian exact mean %%%%%%%%%%%%
sigsqT= sigma^2*T*(N+1)*(2*N+1)/(6*N*N);
muT = 0.5*sigsqT + (r - 0.5*sigma^2)*T*(N+1)/(2*N);
d1 = (log(S/E) + (muT + 0.5*sigsqT))/(sqrt(sigsqT));
d2 = d1 - sqrt(sigsqT);
N1 = 0.5*(1+erf(d1/sqrt(2)));
N2 = 0.5*(1+erf(d2/sqrt(2)));
geo = exp(-r*T)*( S*exp(muT)*N1 - E*N2 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Spath = S*cumprod(exp((r-0.5*sigma^2)*Dt+sigma*sqrt(Dt)*randn(M(k),N)),2);
% Standard Monte Carlo
arithave = mean(Spath,2);
Parith = exp(-r*T)*max(arithave-E,0); % payoffs
Pmean = mean(Parith);
Pstd = std(Parith);
confmc(k,:) = [Pmean-1.96*Pstd/sqrt(M(k)), Pmean+1.96*Pstd/sqrt(M(k))];
confmcSize(k,1) = [(Pmean+1.96*Pstd/sqrt(M(k)))-(Pmean-1.96*Pstd/sqrt(M(k)))];
% Control Variate
geoave = exp((1/N)*sum(log(Spath),2));
Pgeo = exp(-r*T)*max(geoave-E,0); % geo payoffs
Z = Parith + geo - Pgeo; % control variate version
Zmean = mean(Z);
Zstd = std(Z);
confcv(k,:) = [Zmean-1.96*Zstd/sqrt(M(k)), Zmean+1.96*Zstd/sqrt(M(k))];
confcvSize(k,1) = [(Zmean+1.96*Zstd/sqrt(M(k)))-(Zmean-1.96*Zstd/sqrt(M(k)))];
end
T = table(M',confmc,confcv,confmcSize./confcvSize)
In short, I just used a matrix instead of a vector or scalar as the members of the table. In your code, the variables (confmc, confcv, confmcSize, confcvSize) were getting overwritten.

My approximate entropy script for MATLAB isn't working

This is my Approximate entropy Calculator in MATLAB. https://en.wikipedia.org/wiki/Approximate_entropy
I'm not sure why it isn't working. It's returning a negative value.Can anyone help me with this? R1 being the data.
FindSize = size(R1);
N = FindSize(1);
% N = input ('insert number of data values');
%if you want to put your own N in, take away the % from the line above
and
%insert the % before the N = FindSize(1)
%m = input ('insert m: integer representing length of data, embedding
dimension ');
m = 2;
%r = input ('insert r: positive real number for filtering, threshold
');
r = 0.2*std(R1);
for x1= R1(1:N-m+1,1)
D1 = pdist2(x1,x1);
C11 = (D1 <= r)/(N-m+1);
c1 = C11(1);
end
for i1 = 1:N-m+1
s1 = sum(log(c1));
end
phi1 = (s1/(N-m+1));
for x2= R1(1:N-m+2,1)
D2 = pdist2(x2,x2);
C21 = (D2 <= r)/(N-m+2);
c2 = C21(1);
end
for i2 = 1:N-m+2
s2 = sum(log(c2));
end
phi2 = (s2/(N-m+2));
Ap = phi1 - phi2;
Apen = Ap(1)
Following the documentation provided by the Wikipedia article, I developed this small function that calculates the approximate entropy:
function res = approximate_entropy(U,m,r)
N = numel(U);
res = zeros(1,2);
for i = [1 2]
off = m + i - 1;
off_N = N - off;
off_N1 = off_N + 1;
x = zeros(off_N1,off);
for j = 1:off
x(:,j) = U(j:off_N+j);
end
C = zeros(off_N1,1);
for j = 1:off_N1
dist = abs(x - repmat(x(j,:),off_N1,1));
C(j) = sum(~any((dist > r),2)) / off_N1;
end
res(i) = sum(log(C)) / off_N1;
end
res = res(1) - res(2);
end
I first tried to replicate the computation shown the article, and the result I obtain matches the result shown in the example:
U = repmat([85 80 89],1,17);
approximate_entropy(U,2,3)
ans =
-1.09965411068114e-05
Then I created another example that shows a case in which approximate entropy produces a meaningful result (the entropy of the first sample is always less than the entropy of the second one):
% starting variables...
s1 = repmat([10 20],1,10);
s1_m = mean(s1);
s1_s = std(s1);
s2_m = 0;
s2_s = 0;
% datasample will not always return a perfect M and S match
% so let's repeat this until equality is achieved...
while ((s1_m ~= s2_m) && (s1_s ~= s2_s))
s2 = datasample([10 20],20,'Replace',true,'Weights',[0.5 0.5]);
s2_m = mean(s2);
s2_s = std(s2);
end
m = 2;
r = 3;
ae1 = approximate_entropy(s1,m,r)
ae2 = approximate_entropy(s2,m,r)
ae1 =
0.00138568170752751
ae2 =
0.680090884817465
Finally, I tried with your sample data:
fid = fopen('O1.txt','r');
U = cell2mat(textscan(fid,'%f'));
fclose(fid);
m = 2;
r = 0.2 * std(U);
approximate_entropy(U,m,r)
ans =
1.08567461184858

Numerical Integration by Simpsons method

I am trying to solve this integration by simpsons method and plot the result in a figure.The figure is taking only the value of P0= -6 from the for loop. When I set I(K,P) it gives the error:
Attempted to access P0(0); index must be a positive integer or logical
How can I solve it?
alpha = 45;
beta = 185;
gamma_e = 116;
% Gain values
G_ei = -18.96;
G_ee = 18.52;
G_sr = -0.26;
G_rs = 16.92;
G_es = 2.55;
G_re = 4.67;
G_se = 0.73;
G_sn = 2.78;
G_esre = G_es*G_sr*G_re;
G_srs = G_sr*G_rs;
G_ese = G_es*G_se;
G_esn = G_es*G_sn;
t_0 = 0.085; % corticothalamic loop delay in second
r_e = 0.086; % Excitatory axon range in metre
f = linspace(-40,40,500); % f = frequency in Hz
w = 2*pi*f; % angular frequency in radian per second
delt_P = 0.5;
L=zeros(1,500);
Q=repmat(L,1);
P=repmat(L,1);
%%%%%%%%%%%%% integration %%%%%%%%%%%%
a = -80*pi;
b = 80*pi;
n=500;
I=repmat(L,1);
P_initial = repmat(L,1);
P_shift = repmat(L,1);
p = repmat(L,1);
for k = 1:length(w)
for P0 = [6 -6]
L_initial = #(w1) (1-((1i*w1)/alpha))^(-1)*(1-((1i*w1)/beta))^(-1);
Q_initial = #(w1) (1/(r_e^2))*((1-((1i*w1)/gamma_e))^(2) - (1/(1-G_ei*L_initial(w1)))*....
(L_initial(w1)*G_ee + (exp(1i*w1*t_0)*(L_initial(w1)^2*G_ese +L_initial(w1)^3*G_esre))/(1-L_initial(w1)^2*G_srs)));
P_initial = #(w1) (pi/r_e^4)* (abs((L_initial(w1)^2*G_esn)/((1-L_initial(w1)^2*G_srs)*....
(1-G_ei*L_initial(w1)))))^2 * abs((atan2((imag(Q_initial(w1))),(real(Q_initial(w1)))))/imag(Q_initial(w1)));
G = 150*exp(- (f - P0).^2./(2*(delt_P).^2));
P2 = #(w1) G(k) + P_initial(w1);
L_shift = #(w1) (1-((1i*(w(k)-w1))/alpha))^(-1)* (1-((1i*(w(k)-w1))/beta))^(-1);
Q_shift = #(w1) (1/(r_e^2))*((1-((1i*(w(k)-w1))/gamma_e))^(2) - (1/(1-G_ei*L_shift(w1)))*...
(L_shift(w1)*G_ee + (exp(1i*(w(k)-w1)*t_0)*(L_shift(w1)^2*G_ese +L_shift(w1)^3*G_esre))/(1-L_shift(w1)^2*G_srs)));
P_shift = #(w1) (pi/r_e^4)* (abs((L_shift(w1)^2*G_esn)/((1-L_shift(w1)^2*G_srs)*(1-G_ei*L_shift(w1)))))^2 *....
abs((atan2((imag(Q_shift(w1))),(real(Q_shift(w1)))))/imag(Q_shift(w1)));
p = #(w1) P2(w1)*P_shift(w1); % Power spectrum formula for P(w1)*p(w-w1)
I(k) = simprl(p,a,b,n);
end
end
figure(1)
plot(f,I,'r--')
figure(2)
plot(f,G,'k')
At the moment you only use the results for P0 = -6 as you save them in I(k). First you save the result for P0 = 6 later you overwrite it and save the other. The results of P0 = 6are neither used nor saved. If you write your code as follows this will be clarifyied.
for k = 1:length(w)
L_shift = #(w1) (1-((1i*(w(k)-w1))/alpha))^(-1)* (1-((1i*(w(k)-w1))/beta))^(-1);
Q_shift = #(w1) (1/(r_e^2))*((1-((1i*(w(k)-w1))/gamma_e))^(2) - (1/(1-G_ei*L_shift(w1)))*...
(L_shift(w1)*G_ee + (exp(1i*(w(k)-w1)*t_0)*(L_shift(w1)^2*G_ese +L_shift(w1)^3*G_esre))/(1-L_shift(w1)^2*G_srs)));
P_shift = #(w1) (pi/r_e^4)* (abs((L_shift(w1)^2*G_esn)/((1-L_shift(w1)^2*G_srs)*(1-G_ei*L_shift(w1)))))^2 *....
abs((atan2((imag(Q_shift(w1))),(real(Q_shift(w1)))))/imag(Q_shift(w1)));
for P0 = [6 -6]
G = 150*exp(- (f - P0).^2./(2*(delt_P).^2));
P2 = #(w1) G(k) + P_initial(w1);
p = #(w1) P2(w1)*P_shift(w1);
I(k) = simprl(p,a,b,n);
end
end
You can't access I(k,P) as I is an vector not an matrix. However this will give you Index exceeds matrix dimensions. You could save the results for P0 = -6 in one variable and P0 = 6 in the other variable as the results in your code do not depent on each other.

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.