Can I store a function including multiple variable?

1 visualización (últimos 30 días)
chia ching lin
chia ching lin el 18 de Oct. de 2020
Comentada: Walter Roberson el 24 de Oct. de 2020
Hi,
Im new to matlab, is it able to store function including multiple variable which still can be used? Something like this
x=linspace(1,10,100);
t=linspace(1,5,50);
f=@(x,t,theta) sin(x+t(a)+theta)
for i=1:10
state{i}=f(:,:,i);
end
where i expect that (but it is not)
state={sin(x+t(a)+1) sin(x+t(a)+2) sin(x+t(a)+3) ... sin(x+t(a)+10)}
so i can used a for loop on t to make a annimation like this
for a=1:length(t)
for b=1:length(state)
plot(x,state{b}(a));
end
end
Is it possible to do something like above or Im using the wrong function?
  3 comentarios
chia ching lin
chia ching lin el 22 de Oct. de 2020
I'm trying to make a wave propagating and will change it's phase at the source by time, also consider the retarded potential. The equation suppose to be
E=E0*exp(w(t-x/c)+theta)*u(t-x/c);
where the E0 = amplitude, w = angular frequency, t = time, x = propagate direction, c = 3*10^8, theta = phase, u = step function.
The problem is that if I change the phase at the souce, the whole wave will change. If it only change phase at source, the wave wouldn't look like a a sine or cosine wave when time ends.
The length of state is how much times the phase change. I set the phase change with the time ,so if the time length is 50, the length of the state will be 50. It need to be a variable.
The a is the length of the time , I want all the function in state can run in one for loop changing by time.

Iniciar sesión para comentar.

Respuestas (2)

Steven Lord
Steven Lord el 22 de Oct. de 2020
You can't call a function handle using :. If you want the anonymous function to "remember" and use the values of x and t that you've defined in the workspace, you can do that.
x = 1:10;
y = 2;
f = @(theta) y*sin(x+theta);
z1 = f(7)
% check
z2 = 2*sin(8:17)
  1 comentario
chia ching lin
chia ching lin el 22 de Oct. de 2020
Tks for answering, but this isn't quite I want. I know its able to used multiple variable in function handle, but it is only 'one' function for me to used. Is it able to have the same result as below?
%example
f=sin(x*t+theta); % function I used
x=[1 2 3 4 5 6 7 8 9 10]; % spactial distribution % will remain unchange in for loop
t=[1 2 3 4 5]; % time
theta=[1 2 3 4 5]; % phase change by time (will be same length as time)
first, I would like to do something to this function to made it :
F=[ sin(x*t+1) sin(x*t+2) sin(x*t+3) sin(x*t+4) sin(x*t+5)];
so I can call the function for example: F{1}=sin(x*t+1)
next, the function in F need to move together by time :
for b=1:length(theta) % F{1},F{2},F{3},F{4},F{5}
for a=1:length(t) % time change
A=F{b}(a); % (a) is suppose to be the time change, but I don't know how to do with it
end
end
The wave function will times a step function in my code at last, so I cant change the order of my code, it need to be step by step like that.
The length of t and theta will be greater then 1000, so I can't type the function one by one.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 22 de Oct. de 2020
for i=1:10
state{i}=@(x,t)f(x,t,i);
end
Note that this will be a cell array of function handles and if you wanted an array of results you would need to cellfun
cellfun(@(F) F(x, y), state)
  2 comentarios
chia ching lin
chia ching lin el 24 de Oct. de 2020
What is the y in the cellfun? And what is F stands for in the code?
How can I fix my code? This is what I get now. I'm not quite sure the usage of the cellfun, need more help.
x=linspace(0,10,100);
t=linspace(0,5,50);
phase=0:pi/3:2*pi;
for i=1:5
state{i}=@(x,t) sin(x.*t+phase(i));
end
C=cellfun(@(F) F(x,t),state);
Thanks.
Walter Roberson
Walter Roberson el 24 de Oct. de 2020
cellfun(@(F) F(x, t), state)

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by