Matlab: Storing user input into arrays for calculating repeatedly

Viewed 142

I'm writing a MATLAB program for solving the positive z and negative z traveling waves in a multiple region problem. The user chooses the number of layers to be N, and assume the characteristic parameters to be ei ; , µi , and oi in the ith region. The frequency f and the thicknesses di of the N layers is also part of the input data to the program.

I'm having trouble storing 4 variable inputs from the user e, u, o, and d for each individual array (region), store them into N (3 to 5, min. to max.) arrays, then calculate N times from the variable inputs.

c=3*10^8;
u0 = 4*pi*10^-7;
e0 = (36*pi)^-1*10^-9;

% Source parameters --------------------------
prompt = 'Enter number of regions (from 3 to 5)';
N = input(prompt)
prompt = 'Enter the frequency (in Hz) of the incident electric field';
f = input(prompt)
w = 2*pi*f;

% Region example parameters. Region starts from 1 to N -----------------

prompt = 'Enter region %d dielectric epsilon', i; 
e = input(prompt)*e0 % region i epsilon dielectric

prompt = 'Enter region %d permeability mew', i;
u = input(prompt)*u0 % region i permeability mew

prompt = 'Enter region %d conductivity sigma', i;
o = input(prompt) % region i conductivity sigma

B = w*sqrt(u*e); % region i beta propagation constant
L = 2*pi/B; % region i lambda

prompt = 'Enter region %d thickness d (between 0 & 1/2)', i;
d = L*input(prompt) % region i thickness of dielectric material d

a = (w*sqrt(u*e)/sqrt(2))*sqrt(sqrt(1+(o/(w*e))^2)-1); % region i alpha
N = sqrt(u/(e-j*(o/(w) ) )) % region i intrinsic Impedance
Y = a+j*B; % region i gamma

% ---------------------------------------------------------------------

I am stuck figuring out to repeat the user input prompts for the ith region N times, then each variable input is stored into an array. I also believe I need a condition loop to repeat the input prompts up to N times starting from 1. Any help would be appreciated and if some clarification is needed, please ask.

1 Answers

If I've uderstand your problem, you can use:

  • a while loop to acquire the first input N and to check for its validity (that is: keep asking if the input is not in the expcted range)
  • a for loop to iterate over the N regions

To implement the second loop, you can simply transform the scalar varaible you use to get the other input values and to compute ne intermediate values into arrays.

This also allows you recording all values (input, interim variables, output).

At the end of the loop, you can add a third for loop to print the results.

A possible implementation could be:

c=3*10^8;
u0 = 4*pi*10^-7;
e0 = (36*pi)^-1*10^-9;

% Source parameters --------------------------
%
% Check for input validity
%
go_ahead=0;
%
% Continue asking if not valid
%
while(~go_ahead)
   prompt = 'Enter number of regions (from 3 to 5)';
   %N_reg = input(prompt)
   N_reg = input(prompt)
   if(N_reg>=3 && N_reg<=5)
      go_ahead=1;
   end
end

%
% Loop over the number of regioins
%
for reg_idx=1:N_reg
   fprintf('*****************\nProcessing N_reg= %d\n*****************\n',reg_idx)
   prompt = 'Enter the frequency (in Hz) of the incident electric field';
   f(reg_idx) = input(prompt)
   w(reg_idx) = 2*pi*f(reg_idx);

   % Region example parameters. Region starts from 1 to N -----------------

   prompt = 'Enter region %d dielectric epsilon', i; 
   e(reg_idx) = input(prompt)*e0 % region i epsilon dielectric

   prompt = 'Enter region %d permeability mew', i;
   u(reg_idx) = input(prompt)*u0 % region i permeability mew

   prompt = 'Enter region %d conductivity sigma', i;
   o(reg_idx) = input(prompt) % region i conductivity sigma

   B(reg_idx) = w(reg_idx)*sqrt(u(reg_idx)*e(reg_idx)); % region i beta propagation constant
   L(reg_idx) = 2*pi/B(reg_idx); % region i lambda

   prompt = 'Enter region %d thickness d (between 0 & 1/2)', i;
   d(reg_idx) = L(reg_idx)*input(prompt) % region i thickness of dielectric material d

   a(N_reg) = (w(reg_idx)*sqrt(u(reg_idx)*e(reg_idx))/sqrt(2))*sqrt(sqrt(1+(o(reg_idx)/(w(reg_idx)*e(reg_idx)))^2)-1); % region i alpha
   N(reg_idx) = sqrt(u(reg_idx)/(e(reg_idx)-j*(o(reg_idx)/(w(reg_idx)) ) )); % region i intrinsic Impedance
   Y(reg_idx) = a(reg_idx)+j*B(reg_idx); % region i gamma
end

%
% Print output
%
for reg_idx=1:N_reg
   fprintf('\n*************\nRegion #%d\n*************\n',reg_idx)
   fprintf('Frequency=%f\ndielectric epsilon=%f+%fi\npermeability mew=%f+%fi\nconductivity sigma=%f+%fi\nthickness d=%f+%fi\n',real(f(reg_idx)),imag(f(reg_idx)),real(e(reg_idx)),imag(e(reg_idx)),real(u(reg_idx)),imag(u(reg_idx)),real(o(reg_idx)),imag(o(reg_idx)),real(d(reg_idx)),imag(d(reg_idx)))
   fprintf('\n==> a=%f+%fi\n==> N=%f+%fi\n==> Y=%f+%fi\n',real(a(reg_idx)),imag(a(reg_idx)),real(N(reg_idx)),imag(N(reg_idx)),real(Y(reg_idx)),imag(Y(reg_idx)))
end
Related