How to get a single value of an array through a function inside a script?

조회 수: 2 (최근 30일)
Hello,
I have two questions, how can we use a variable defined in a script to be used later inside a function in the same script?
2nd, I am having trouble in trying to get a single value of an array which is processed inside a function inside a script. The function should return the u'th value of the array gE by storing it in the Eg(function return) but I get an error of
Index exceeds matrix dimensions.
Error in Slab_002>Esrc (line 187)
Eg = gE(u);
How can we fix this? All answers are welcome.
script something something
a = something;
gE = zeros(1, something);
dt = something;
STEPS = something;
function Eg = Esrc(u)
a = dt*u; %% I want to use the a that is defined above in the code.
for i = 1:STEPS
gE(i) = exp(-((t-t0-a)/tau).^2);
end
Eg = gE(u);
end

채택된 답변

Kevin Phung
Kevin Phung 2019년 5월 8일
function Eg = Esrc(u,a)
a = dt*u; %% I want to use the a that is defined above in the code.
for i = 1:STEPS
gE(i) = exp(-((t-t0-a)/tau).^2);
end
Eg = gE(u);
end
  댓글 수: 2
Stephen23
Stephen23 2019년 5월 9일
편집: Stephen23 2019년 5월 9일
This does not make much sense:
function Eg = Esrc(u,a)
a = dt*u;
...
end
You added the inut argument a, but totally ignore that input argument inside the function by reallocating that variable name. What is the point of that?
Zahid Saleem
Zahid Saleem 2019년 5월 9일
Well, I am trying to simulate maxwells equations using FDTD(Finite Difference Time domain), ome side of equation is temporal whereas the other side is spatial. I am fairly new to MATLAB so this was one of the easiest(inefficient) way to manage those equations differentials.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by