(NOOB) Dealing with large numbers? Any advice on existing code appreciated!

6 views (last 30 days)
Hello, i'm a research intern working with antenna arrays
I've been given the task of finding the ratio of path length to array-element distance required to allow scanning between to two frequency limits.
Here is my current code, this is my first MATLAB function so it's probably a mess!
&# function that computes the ratio of d/l needed to scan between two frequency limits and also %#computes d and l seperately function [l,d] = meander(f1,f2,eff)
%this value is dependant on the source signal, will figure out later
n =
%#speed of light in m/s
c = 299792458;
%#frequency limits
f1 = input('Enter lower frequency');
f2 = input('Enter upper frequency');
%#effective permitivitty
eff= input('Enter εeff');
%#central frequency
fc = 2*f1*f2/(f1+f2);
%#wave number
k = 2*pi*fc;
% calculations
x = ((f2-f1)*sqrt(eff))/((f2+f1)*c);
l = ((f2+f1)*1.8*n)/(k*sqrt(eff));
d = l*x;
fprintf('d/l = % g \n', x )
fprintf('l = % g \n', l )
fprintf('d = % g \n', d )
Since there are large numbers involved(the frequencies will be in giga-hertz) how to I manage them? also please point out any mistakes in the code.
Cheers Gavin

Answers (1)

Chad Greene
Chad Greene on 8 Jul 2014
Your function seems to do a few things twice. For example, the inputs and outputs. The user will interact with the function in some form similar to this:
[l,d] = meander(5e+9,6e+9,.2)
The output will be l and d, so the fprintf lines seem unnecessary. They're certainly fine, but they output what is already being output in the form of variables in the workspace. Similarly for the inputs, the input lines are unnecessary if the user enters inputs manually as above. The syntax above says that f1 equals 5 gigahertz, f2 equals 6 gigahertz, and efficiency is 0.2.
If you keep the input lines, I'd change the text to be very clear about what the user is supposed to enter. If the frequency needs to be in hertz, let the message say 'Enter lower frequency in Hz:'. If the user needs to enter the lower frequency in gigahertz, say 'Enter lower frequency in GHz:'
If dealing with all the zeros of gigahertz is an issue, you can use the syntax shown above. 5e+9 is the same thing as 5 * 10^9. Or, you can tell the user to enter frequencies in gigahertz, then do the conversion in the function by starting with this:
f1 = f1*1e+9;
That changes f1 from the user-friendly gigahertz form to the math-friendly hertz form. If you want to be extra clear about it, you could change the names of all your frequency variables so anyone reading your function can immediately identify what units every variable is associated with. Like this:
function [l,d] = meander(f1_GHz,f2_GHz,eff)
f1_Hz = f1_GHz*1e+9;
f2_Hz = f2_GHz*1e+9;
fc_Hz = 2*f1_Hz*f2_Hz/(f1_Hz+f2_Hz);
and and so on.
A final point is to get in the habit of writing function headers with clearly-written syntax, description, and examples. Good documentation should take about twice as long to write as it took you to write the code. It forces you to come up with many possible uses of your function, and test each case. It forces you to understand exactly what your code is doing, and it will always help you find bugs. Even if it's a function that only you will ever use, you'll never regret making clear notes to yourself on how to use the function and what each step does.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!