Clear Filters
Clear Filters

Why does my function handle change to a vector when I pass it to another function?

2 views (last 30 days)
I'm new to function handles and am running into an issue. I have a function f_pe(z) that describes the plasma frequency in the ionosphere as a function of height above the earth's surface, which looks like this:
fplot(f_pe, [50e3 400e3])
But, when I pass that function into another function like this:
result = @(z) PhysLib.coldPlasmaNonMagGroupIOR(f_pe(z),3e6)
f_pe turns into a 1x150 double vector, and when I plot it, it looks like this:
Why is it being converted to a 1x150 double, and why doesn't it at least have the same smooth shape as the function passed in?

Accepted Answer

Steven Lord
Steven Lord on 30 Sep 2021
f_pe turns into a 1x150 double vector,
No it doesn't. If it did I'm guessing your result function handle would throw an error when you tried to evaluate it.
why doesn't it at least have the same smooth shape as the function passed in?
What exactly are you plotting in that second picture? I'm guessing you're not plotting f_pe but you're instead plotting result. If that's the case, that plot could look different from the first depending on what PhysLib.coldPlasmaNonMagGroupIOR is doing to the results returned by evaluating f_pe at the value you pass into result as input.
To use a food metaphor, the outputs of b and t below are not likely to be the same unless your toast() function fails to account for the toaster being unplugged.
b = @(x) bread(x); % Call as b('rye') to make rye bread
t = @(x) toast(b(x)); % t('rye') makes rye bread then toasts it
  2 Comments
Jordan Wiker
Jordan Wiker on 30 Sep 2021
Edited: Jordan Wiker on 30 Sep 2021
In both images, I'm plotting f_pe.
Here're the steps I took to lead me to think f_pe turns into a 1x150 vector:
  1. Set a breakpoint immediately before f_pe gets passed into PhysLib.coldPlasmaNonMagGroupIOR here: result = @(z) PhysLib.coldPlasmaNonMagGroupIOR(f_pe(z),3e6)
  2. Plot f_pe at this point via: fplot(f_pe, [50e3 400e3]) to produce this plot
  3. Step forward one step so that I'm just inside PhysLib.coldPlasmaNonMagGroupIOR. Now, before anything has happened in PhysLib.coldPlasmaNonMagGroupIOR, the passed in f_pe variable is shown in the Workspace as a 1x150 double
  4. Plot f_pe again, this time via plot(f_pe) to produce this plot:
Steven Lord
Steven Lord on 30 Sep 2021
The f_pe in the workspace from which you called result has not changed. The first input argument to PhysLib.coldPlasmaNonMagGroupIOR is not the function handle f_pe. That first input argument is the output of evaluating f_pe at the vector with which you called the result function handle.
I think one potential point of confusion is if the name of the variable inside PhysLib.coldPlasmaNonMagGroupIOR to which that first input argument was assigned was f_pe. Two variables in different contexts are not necessarily the same variable even if they share a name, much like Steve Eddins and I are not the same person even though we're both named Steve.
Or to show it another way:
f = @sin; % function handle
plot(f(0:pi/4:2*pi)) % double vector resulting from evaluating function handle
figure
y = f(0:pi/4:2*pi);
whos y
Name Size Bytes Class Attributes y 1x9 72 double
plot(y) % Same result as the first plot call
The plot function does not receive the function handle as input. MATLAB evaluates that function handle (which gives a double vector) and passes that double vector into plot.
When you call fplot on the function handle, MATLAB will decide at which points it needs to evaluate the function handle to get a "nice" plot. This could mean MATLAB evaluates the function at more points than you did manually when you called result.
figure
h = fplot(f);
You can check that fplot evaluated f at many more points than I did manually to create the first two plots above.
size(h.XData)
ans = 1×2
1 192
Plotting 192 points along the sine curve makes for a smoother plot than plotting just 9 points.

Sign in to comment.

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!