# How to write function with multiple input and output (Function for Optimization based on Experimental data)

6 views (last 30 days)
Jafar Nur Arafah on 3 Feb 2020
Commented: Jafar Nur Arafah on 10 Feb 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.
Regards,
Nur Arafah
function y = myFunction (x)
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

Walter Roberson on 3 Feb 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);

#### 1 Comment

Jafar Nur Arafah on 4 Feb 2020
I tried to use your suggested command "a(i) = alpha(i,1)".
No error came out. The value of a came out to be 1x2241 double but the value along the row is zero except for the colum 2241, which is -3.3730 (the last value of from file alpha (:,1).
What command should I use so to represent all the value in the first row, alpha (:,1)?
Thank you.
Regards,
Nur Arafah

Jakob B. Nielsen on 3 Feb 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.

#### 1 Comment

Jafar Nur Arafah on 4 Feb 2020
Yes, my intention put alpha(:,1) in the command is to take every value in the first column of alpha. However, its look like my command is not correct thus the error appear.
The reason I put a(i) at the front is because every value of alpha in the column should match the each frequency value following the sequence.
Could you recommend what is the correct command that I should use for "alpha"?
Thank you.
Regards,
Nur Arafah

Jafar Nur Arafah on 4 Feb 2020
Thanks for your comment and suggestion.
FYI, I've changed the command of alpha to a(i) = alpha(i,1) and I able to called the alpha value that I want (every value in the first column of alpha).
However, when I tried to test the function using command:
x(1)=700e-6; x(2)=1000e-6 ;x(3)=0.00615;
y = myFunction(x).
This error came out:
Error using alpha
Too many output arguments.
Error in myFunction (line 12)
a(i) = alpha(i,1);
It should be correct right? Because alpha value is changed with frequency, thus the output also should be changed with frequency. Why this error came out? How can I correct it?
Thank you.
Regards,
Nur Arafah.

Jafar Nur Arafah on 8 Feb 2020
I have updated my code (as per below).
Where I state 'i' as,
f = 500:1.5625:4000;
for i = 1:length(f)
I'm using the same for 'i' command for every loop. The reason I use the same command is because, I want each loop to use the same frequency range, f = 500:1.5625:4000.
function y = myFunction (x)
c = 345.2;
n = 1.83e-5;
den = 1.189;
f = 500:1.5625:4000;
%x(1)=700e-6
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
%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
%x(1)=800e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,3);
E(i) = xi(i,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(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
%x(1)=850e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,4);
E(i) = xi(i,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(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
%x(1)=900e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,5);
E(i) = xi(i,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(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
%x(1)=950e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,6);
E(i) = xi(i,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(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
%x(1)=1000e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,7);
E(i) = xi(i,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(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
%x(2)=700e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,8);
E(i) = xi(i,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(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
%x(2)=800e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,9);
E(i) = xi(i,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(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
%x(2)=900e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,10);
E(i) = xi(i,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(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
%x(2)=1000e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,11);
E(i) = xi(i,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(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
%x(2)=1100e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,12);
E(i) = xi(i,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(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
%x(2)=1200e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,13);
E(i) = xi(i,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(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
%x(2)=1300e-6
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,14);
E(i) = xi(i,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(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
%x(3)=0.004
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,15);
E(i) = xi(i,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(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
%x(3)=0.005
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,16);
E(i) = xi(i,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(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
%x(3)=0.006
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,17);
E(i) = xi(i,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(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
%x(3)=0.007
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,18);
E(i) = xi(i,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(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
%x(3)=0.008
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,19);
E(i) = xi(i,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(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
%x(3)=0.009
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,20);
E(i) = xi(i,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(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
%x(3)=0.01
for i = 1:length(f)
w(i) = 2.*pi.*f(i);
a(i) = alpha(i,21);
E(i) = xi(i,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(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
plot (f, y(i))
end
Walter Roberson on 9 Feb 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.
Jafar Nur Arafah on 10 Feb 2020
1. 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. 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.
Correct, each value relied with i (f value).
-For a() and xi(), its changed directly with i, but different for each loop.
-w(i) and Rs(i), changed with i, but same for each loop.
-k(i), r1(i) and m1(i), change for each loop as it is depends on x's value. In additional, r1(i) and m1(i) are depends on the a() and xi().
-For the rest parameter, z, Red, Ref_re, Ref_im and y, it will changed for each loop as it is calculated based on r1 and m1. Thus, the output y (as a function of i) is different for each loop.
2. 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.
-As per my answer above, there are some parameter changed for each loop, but its true that same logic flow holds for every loop.
-When you mentioned that 'nothing is calculated in the first loop', does it because of the x value, that are not determine in the loop? For example, in first loop x(1) is not determine.
%x(1)=700e-6
3. 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.
-From my understanding for command is used in a loop to repeat 'something' (where in my case it will be i). But as the value of other parameters are different, so I keep repeat the same function for each loop. Where, I expect to have a total of 21 y(i) values presented at the end of function.
-What command should I use so that the calculation in first loop will not be ignored and overwritten when second loop command is run?
4. 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
-Actually when I first want to write a function for 'optimization', I take a loook at tutorial available. But I could not find any optimization based on experimental value. In my case, I have few known inputs ( x(1), x(2) and x(3) ) with known output (y as function of f), but basically the mathematical equation is fixed to determine the output for different inputs.
-In most of tutorial available, they did not determine the inputs (x's) and output (y) and just written the equation in their function. They will only determine the boundary condition for inputs in the main function of their optimization code. Where, at the end they will get the optimize value of inputs (x's).
-Thus, I'm not sure whether I should list the input, x's value in my function or not. Therefore, instead of list all the x's value in each loop, I decide to list the x's value that are fixed only (which is divided into 3 different set as per shown below).
-That is why, in the first 7 loop I only determine the value of x(2) and x(3), as it is fixed. Then changed to x(1) and x(3) in the next 7 loop and so on.
-Should I just lis all the x's value in each loop or put it in mat.file or text.file like a() and xi()?
Thank you.
Regards,
Nur Arafah