Request for MATLAB Code Review and Correction
Show older comments
The image contains the Transmission Line (TRL) equation. I am trying to develop a MATLAB code that matches this equation accurately. However, I need to verify that the implementation is consistent with the mathematical expression shown in the image.
Could you please review it and help ensure that the MATLAB code correctly follows the TRL equation?
Here this matlab code
clc;
clear;
% ===== Constants =====
c = 3e8; % Speed of light (m/s)
d = 2.62e-3; % Thickness in meters (example: 2.62 mm)
% ------------------- Ask user to select Excel file -------------------
[filename, pathname] = uigetfile('*.xlsx', 'Select your S-parameter Excel file');
if isequal(filename,0)
error('No file selected. Exiting...');
end
filepath = fullfile(pathname, filename);
% ===== Read data from Excel =====
% Make sure your Excel file has columns: frequency(Hz), e', e'', u', u''
data = readmatrix(filepath); % Reads all numeric data
f = data(:,1); % Frequency in Hz
ep_real = data(:,2); % e'
ep_imag = data(:,3); % e''
mu_real = data(:,4); % u'
mu_imag = data(:,5); % u''
% ===== Complex parameters =====
eps_r = ep_real - 1i*ep_imag;
mu_r = mu_real - 1i*mu_imag;
% ===== Input impedance calculation =====
Zin = sqrt(mu_r ./ eps_r) .* tanh(-1i .* (2*pi.*f.*d./c) .* sqrt(mu_r .* eps_r));
% ===== Reflection Loss =====
RL = 20*log10(abs((Zin - 1) ./ (Zin + 1)));
% ===== Reflection coefficient =====
Gamma = abs((Zin - 1) ./ (Zin + 1));
% ===== Absorption percentage =====
Absorption = (1 - Gamma.^2) * 100;
% ===== Display results =====
Result = table(f, RL, Absorption)
% ===== Plot RL =====
figure
plot(f/1e9, RL,'LineWidth',2)
xlabel('Frequency (GHz)')
ylabel('Reflection Loss (dB)')
title('Reflection Loss vs Frequency')
grid on
7 Comments
Torsten
ongeveer 4 uur ago
If log() means the decadic logarithm (logarithm to the base 10), your lines
% ===== Input impedance calculation =====
Zin = sqrt(mu_r ./ eps_r) .* tanh(-1i .* (2*pi.*f.*d./c) .* sqrt(mu_r .* eps_r));
% ===== Reflection Loss =====
RL = 20*log10(abs((Zin - 1) ./ (Zin + 1)));
look correct.
In mathematics, log() usually denotes the natural logarithm (logarithm to the base e).
dpb
ongeveer 3 uur ago
RL = 20*log10(abs((Zin - 1) ./ (Zin + 1)));
I don't know the application otomh, but 20log() would be a common expression for a quantity in dB units in which case base 10 log would be expected. Not conclusive, but makes me think the log10 is the correct one here.
Just commenting on code style and MATLAB usage...
...
% Make sure your Excel file has columns: frequency(Hz), e', e'', u', u''
data = readmatrix(filepath); % Reads all numeric data
tData=readtable(filepath); % all numeric data in a table Var1 ... Var5
tData.Var2=complex(tData.Var2,-tData.Var3); % combine real, imag for eps
tData.Var3=complex(tData.Var4,-tData.Var5); % combine real, imag for mu
tData=removevars(tData,{'Var4','Var5'}); % remove unneeded real, imag
tData.Properties.VariableNames={'f','eps','mu'}; % name variables
f = data(:,1); % Frequency in Hz
% ===== Input impedance calculation =====
Zin = sqrt(tData.mu./tData.eps).*tanh(-1i*(2*pi*f.*d/c).*sqrt(tData.mu.*tData.eps));
% ===== Reflection coefficient =====
Gamma = abs((Zin-1) ./ (Zin+1));
% ===== Reflection Loss =====
RL = 20*log10(Gamma);
% ===== Absorption percentage =====
Absorption = (1 - Gamma.^2) * 100;
% ===== Display results =====
Result = table(f, RL, Absorption)
...
Just a use of a table for the input data and then using that data in place instead of making temporary copies of the data array for the calculations.
dpb
ongeveer 2 uur ago
Not necessarily "should" but "could" ... <grin>
Is a little more memory efficient and done just to illustrate.
One thing I did notice -- in the building of the complex values the imaginary part was negated when adding -- is that correct, the values read are the negative of the actual values wanted? Note the negative sign in the complex() argument; that replicates the "-" in the numeric expression you had.
Mr.DDWW
ongeveer een uur ago
Oh. The data file has a header row (or rows) then. The original code using readmatrix, I assumed there wasn't a header so the 'VarN' variable names are the default.
You can use the actual names if want (means changing the names to match later of course) or tell readtable to not read the variable names. If you do choose to use the 'preserve' option for variable names and use them, then the syntax to use them will be slightly different; regular "dot" addressing won't work; you will have to enclose the variable name in parentheses after the period as a quoted string. See table and <Summary Accessing Table Data>
The first row of the summary table illustrates the pertinent forms to use here in that case to address individual variables by name as I did above or one can always revert to the column numbers for less typing at the expense of internal code documentation.
One additional generic comment -- the use of clear is rarely needed since variables used are all going to be defined explicitly and it is a club that can cause a need to regenerate other prior calculations in the workspace that may want again. Not necessarily wrong if is really intended, just note that it isn't necessary although many instructors have blanket instructions to do so on homework leading to an adoption of the practice.
A more robust and cleaner way would be to encapsulate the calculations in a function instead of using a script; then the temporary constants can be local and not add clutter to the base workspace; just return the desired result tables needed/wanted and everything else goes away automagically when the function returns; it will clear its own local memory for you.
Answers (0)
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!