How to write function with multiple input and output (Function for Optimization based on Experimental data)
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (2)
Jakob B. Nielsen
le 3 Fév 2020
0 votes
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 commentaire
Jafar Nur Arafah
le 4 Fév 2020
Jafar Nur Arafah
le 4 Fév 2020
0 votes
9 commentaires
Walter Roberson
le 4 Fév 2020
alpha happens to be the name of a function that accepts one input and can never have an output.
When you do the
load alpha.txt
then you are "poofing" alpha into existence: MATLAB looks at the name of the file being loaded in order to figure out which variable to write into, and there is no explicit assignment to alpha in the code. Under the circumstances, current versions of MATLAB are permitted to believe that in the entire rest of the code, references to alpha are to the function named alpha, not to a variable named alpha.
The correction for this is,
alpha = load('alpha.txt');
Jafar Nur Arafah
le 4 Fév 2020
Walter Roberson
le 4 Fév 2020
a(i) = alpha(i,1);
So a is a vector.
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));
That uses all of a not just one element of a
Jafar Nur Arafah
le 5 Fév 2020
Walter Roberson
le 6 Fév 2020
I suspect that you will find that you have not assigned anything to i at that point, and so that i will be its default value which is sqrt(-1)
Walter Roberson
le 6 Fév 2020
Your code is pretty strange, and I would be surprised if it is correct. You start with
for i = length(f)
which would cause i to take on only the value length(f) and not any other value -- the loop will be executed once as if you had written
i = length(f);
Then you follow that with
for i = 2:length(f)
That will cause i to be set to 2 for the first iteration, 3 for the second iteration, and so on until i=length(f) is reached. In isolation that looks plausible, but would look more plausible if the first loop had been for i = 1 instead of for i = length(f) . You write to all (or almost all) of a number of arrays in the loop.
Then you follow with
for i = 3:length(f)
and have almost exactly the same instructions, including a bunch of calculation that do not rely on anything that was caclulated in the for i = 2:length(f) loop but overwrite the same locations. It does look like at least one of the for i = 3:length(f) calculations does depend upon what you calculated in for i = 2:length(f) but I have to wonder if that was an accident .
By the time you get to for i = 21:length(f) you will have overwritten positions 21 to length(f) of some of the arrays 19 times before that (and for length(f), one additional time.) This is really questionable.
Jafar Nur Arafah
le 8 Fév 2020
Walter Roberson
le 9 Fév 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
le 10 Fév 2020
Catégories
En savoir plus sur Analysis of Lifetime Data dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
