How to write function with multiple input and output (Function for Optimization based on Experimental data)
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Jafar Nur Arafah
el 3 de Feb. de 2020
Comentada: Jafar Nur Arafah
el 10 de Feb. de 2020
Hi All,
I have 3 set of experimental data and I would like to do an optimization based on those results. Below are the set of data:
*SET 1:
inputs : 0.7 < x(1) < 1.0
x(2) = 1.0
x(3) = 0.615
a(f)
E(f)
outputs: y(f)
*SET 2:
inputs : x(1) = 0.7
0.7 < x(2) < 1.3
x(3) = 0.615
a(f)
E(f)
outputs: y(f)
*SET 3:
inputs : x(1) = 0.7
x(2) = 1.0
0.4 < x(3) <1.0
a(f)
E(f)
outputs: y(f)
In total, there are 21 samples (7 samples for each set mentioned above). Inputs a(f) and E(f) are taken from experimental data and this values are varied with frequency and it is different for every samples. I tried to include all the data available in 1 function (as per code attached). At the end I plan to find an optimize inputs x(1), x(2) and x(3) for ouput y(f) equal to 1 along the frequency range.
After save the function. I tried to test the function by write:
x(1)=700e-6; x(2)=1000e-6 ;x(3)=0.00615;
y = myFunction(x)
However, error below appear.
Subscripted assignment dimension mismatch.
Error in myFunction (line 12)
a(i) = alpha(:,1);
I'm not sure where is the problem. Really appreciate if someone could give any advice or suggestion as I am new to Matlab.
Thank you in advance.
Regards,
Nur Arafah
function y = myFunction (x)
load alpha.txt;
load xi.txt;
c = 345.2;
n = 1.83e-5;
den = 1.189;
freq = 500:1.5625:4000;
f = freq'
%x(1)=700e-6
for i = length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,1);
E(i) = xi(:,1);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a.*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E;
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(1)=750e-6
for i = 2:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,2);
E(i) = xi(:,2);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(1)=800e-6
for i = 3:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,3);
E(i) = xi(:,3);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(1)=850e-6
for i = 4:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,4);
E(i) = xi(:,4);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(1)=900e-6
for i = 5:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,5);
E(i) = xi(:,5);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(1)=950e-6
for i = 6:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,6);
E(i) = xi(:,6);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(1)=1000e-6
for i = 7:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,7);
E(i) = xi(:,7);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(2)=700e-6
for i = 8:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,8);
E(i) = xi(:,8);
x(1)= 700e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(2)=800e-6
for i = 9:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,9);
E(i) = xi(:,9);
x(1)= 700e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(2)=900e-6
for i = 10:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,10);
E(i) = xi(:,10);
x(1)= 700e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(2)=1000e-6
for i = 11:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,11);
E(i) = xi(:,11);
x(1)= 700e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(2)=1100e-6
for i = 12:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,12);
E(i) = xi(:,12);
x(1)= 700e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(2)=1200e-6
for i = 13:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,13);
E(i) = xi(:,13);
x(1)= 700e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(2)=1300e-6
for i = 14:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,14);
E(i) = xi(:,14);
x(1)= 700e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(3)=0.004
for i = 15:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,15);
E(i) = xi(:,15);
x(1)= 700e-6
x(2)= 1000e-6
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(3)=0.005
for i = 16:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,16);
E(i) = xi(:,16);
x(1)= 700e-6
x(2)= 1000e-6
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(3)=0.006
for i = 17:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,17);
E(i) = xi(:,17);
x(1)= 700e-6
x(2)= 1000e-6
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(3)=0.007
for i = 18:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,18);
E(i) = xi(:,18);
x(1)= 700e-6
x(2)= 1000e-6
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(3)=0.008
for i = 19:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,19);
E(i) = xi(:,19);
x(1)= 700e-6
x(2)= 1000e-6
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(3)=0.009
for i = 20:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,20);
E(i) = xi(:,20);
x(1)= 700e-6
x(2)= 1000e-6
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
%x(3)=0.01
for i = 21:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(:,21);
E(i) = xi(:,21);
x(1)= 700e-6
x(2)= 1000e-6
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref);
Ref_im(i) = imag(Ref);
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
end
0 comentarios
Respuesta aceptada
Walter Roberson
el 3 de Feb. de 2020
a(i) = alpha(:,1);
alpha has multiple rows. alpha(:, 1) is a vector. The right hand side is a vector. The left hand side is big enough to store a scalar.
a(i) = alpha(i,1);
Más respuestas (2)
Jakob B. Nielsen
el 3 de Feb. de 2020
a(i) wants to assign a single value into a vector, a, on the ith index. But alpha(:,1) refers to every value iin the first column of alpha. That is why you get the dimension mismatch error. Depending on what your alpha looks like and what you want to do, just as a troubleshooting you can try writing alpha(1,1) instead - this is a single value, and that will at least fix the error so you can look at the rest of your script.
Jafar Nur Arafah
el 4 de Feb. de 2020
9 comentarios
Walter Roberson
el 9 de Feb. de 2020
Let's look at this more closely:
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,1);
E(i) = xi(i,1);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref(i));
Ref_im(i) = imag(Ref(i));
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
Variables changed for each value of i: a E k m1 Ref Ref_re Ref_im RF Rs r1 w y z. Also, x(1), and x(2) relied upon.
The assignment
w(i) = 2.*pi.*f(i);
relies on f, which is not changing in the loop, so if you were to repeat the loop with the same i value you would get the same result. Likewise a(i) and E(i) are repeatable as they depend only on values that do not change in the loop.
The assignments
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
rely upon w(i) which is set earlier in the loop to a value that depends upon f(i) that is not changing in the loop. With w(i) always getting the same value for any given i, the k and Rs values are repeatable -- if you were to run the entire loop with the same i value, you would get the same w(i) and so the same k(i) and Rs(i) values. The m1(i) depends upon w(i) and k(i) and E(i), which we showed earlier are repeatable, depending only on the i value and not upon previous iterations. x(1) does not change in the loop and x(2) and x(3) are assigned constants in the loop. So if you run the loop from the beginning, m1(i) will be exactly repeatable for the same i value.
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref(i));
Ref_im(i) = imag(Ref(i));
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
All of those depend only on values calculated earlier in the loop, with the calculations repeatable if you run the same loop with the same i value.
Now we look at the second loop
%x(1)=750e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,2);
E(i) = xi(i,2);
x(2)= 1000e-6
x(3)= 0.00615
k(i) = x(1).*sqrt((w(i).*den)./(4.*n));
Rs(i) = (1./2).*sqrt(2.*n.*den.*w(i));
r1(i) = (((32.*n.*x(2))./(x(3).*den.*c.*(x(1).^2))).*sqrt(1+((k(i).^2)./32)))+((2.*a(i).*Rs(i))./(x(3).*den.*c));
m1(i) = (((w(i).*x(2))./(x(3).*c)).*(1+(1./sqrt(9+((k(i).^2)./2)))))+(((w(i).^2).*(x(1).^2))./(8.*x(3).*(c.^2)))+((8.*x(1).*w(i))./(3.*pi.*x(3).*c)).*E(i);
z(i) = r1(i)+(j.*m1(i));
Ref(i) = (z(i)-1)./(z(i)+1);
Ref_re(i) = real(Ref(i));
Ref_im(i) = imag(Ref(i));
RF(i) = sqrt((Ref_re(i).^2)+(Ref_im(i).^2));
y(i) = 1 - abs(RF(i)).^2;
end
Different alpha() and xi() are used that for the first loop, but the same logic flow holds: all of the same variables are completely overwritten, using only external constant values and the loop iteration counter, and nothing calculated in the first loop. For any given i value, y(i) calculated in the first loop will be completely ignored in the second loop but y(i) will be overwritten.
The same logic holds through the %x(1)=1000e-6 loop with alpha(i,7) . You might as well not have executed any of the previous values because you are overwriting all of the previous variables.
Then you have a change in the 8th loop: the first 7 loops assign to x(2) and x(3) but the 8th loop has
x(1)= 700e-6
x(3)= 0.00615
without assigning to x(2), but still uses x(2) in its calculations. Here it matters what value of x(2) was inherited from the 7'th loop -- and if at that point you were to for some reason go back and re-do the first 7 loops you would get different answers because x(1) would have changed. The first 7 loops have comments about the x(1) values that imply that x(1) is different for them, but they do not do anything to change x(1), so x(1) will be whatever value it was when it was loaded (or 0 by default if x was not in the .mat file that was loaded.)
%x(2)=700e-6
No, at that point x(2) is inherited from the 7th loop and is x(2)= 1000e-6 .
Anyhow, then the 9th loop overwrites all of the changes that the 8th loop made, so they are all a waste of time until loop 14. Then loop 15 does
x(1)= 700e-6
x(2)= 1000e-6
which inherits the x(3) from loop 14.
And then loop 16 overwrites everything loop 15 did...
And so on.
At the end, loop 21 overwrites all of those variables as well, but the value that it produces depends upon the current x(3) that was assigned in loop 14.
So... you could get exactly the same output by just doing the same assignment to x(3) that you do in loop 14, and then execute the code in loop 21.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!