How to modify an anonymous function with another function

13 Ansichten (letzte 30 Tage)
Max Rothstein
Max Rothstein am 21 Sep. 2020
Kommentiert: Walter Roberson am 22 Sep. 2020
I have the following function
function [y1sig,y2sig] = lab1sim(A,B,L,sig)
Cs = 333.333;
fs = 1e6;
t = -5:(1/fs):5;
d1 = pdist([0,2*A;B,L],'euclidean')/Cs;
d2 = pdist([0,A;B,L],'euclidean')/Cs;
y1sig = sig(t - d1);
y2sig = sig(t - d2);
end
which is supposed to take a function of t (sig) and convert it to be t minus some delay. So for example (t)^2 should become (t-delay)^2. My function as it stands just returns sig exactly as it was.
sig = @(t) 1000*cos(880*pi*t).*heaviside(t);
  2 Kommentare
Jan
Jan am 21 Sep. 2020
What does not work? Which problem do you try to solve?
Max Rothstein
Max Rothstein am 21 Sep. 2020
y1sig and y2sig are returned unmodified, they should have t replaced with (t-d1) or (t-d2)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 21 Sep. 2020
As written, y1sig and y2sig aren't function handles. You could make them function handles like this:
f1 = @(d) sind(d);
f2 = @(x) f1(x+90); % f2 is a shifted version of f1
theta = 0:360;
axis([0 360 -1 1])
hold on
plot(theta, f1(theta), 'r-', theta, f2(theta), 'k--')
To anticipate your next question no, there is no way to have f2 displayed as @(x) sind(x+90) or @(d) sind(d+90). At least not without converting f1 into text with func2str, manipulating the text, and converting to a function handle with str2func. If you were to go that route, be careful: @(d+90) sind+90(d+90) is not a valid function handle. Don't make the clbuttic mistake.
  3 Kommentare
Steven Lord
Steven Lord am 22 Sep. 2020
You didn't show us A, B, L, or t. But when I tried running with arbitrary values of those four variables it seemed to work.
sig = @(t) 1000*cos(880*pi*t).*heaviside(t);
A = 2; B = 4; L = 8;
Cs = 333.333;
fs = 1e6;
t = -5:(1/fs):5;
d1 = pdist([0,2*A;B,L],'euclidean')/Cs;
d2 = pdist([0,A;B,L],'euclidean')/Cs;
y1sig = @(n) sig(n-d1);
y2sig = @(n) sig(n-d2);
Q = y1sig(1:10);
Can you show us the specific values of A, B, L, and t that you're using? If you can't show the values, at least showing us the class and size of each could be useful.
whos A B L t
Walter Roberson
Walter Roberson am 22 Sep. 2020
At least not without converting f1 into text with func2str, manipulating the text, and converting to a function handle with str2func.
Or by using the Symbolic Toolbox.
The route of converting to string has some risk. When an anonymous function mentions a variable that is not a parameter, the function handle generated has a copy of the variable as of the time the handle was defined, and it is that copy of the value that is used every time the function executes, no matter what changes you might make to the variable that was mentioned. But when you convert the function handle to text, then there is no way for the text to say "use my local copy of the variable phase", so when you convert to text and back to function handle, it would be the current value of that variable that would get captured, not the value that was associated with the anonymous function.
This is not an issue if you went the symbolic toolbox route: when you created the symbolic evaluation of the function handle, the value of the saved variable would be copied in (and the name of the variable would drop out.)

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by