Cant create a function that return array correctly
Afficher commentaires plus anciens
Hello,
I'm learning how to use Matlab for some computational physics simulations and i'm having the following problem.
I define a certain quantity
x = [0:dx:L];
y(1,:) = cos(x).*exp(-(x-1*L/2).^2/(2*20));
where the first index of the array represents time and the second (x) represents space.
I want to advance this quantity in time with a simple relation lets suppose, so i create a loop
for n = 1:Nmax
for i = 1:Imax
x(n+1,i) = x(n,i) - 0.5;
end
end
this work just fine..
But now imagine if i want to create a function that advances this quantity x in another .m file and i want to call it here as so
for n = 1:Nmax
x = advancex(n,Imax,x);
end
where the advance function (independent .m file)
function z =advancex(n,Imax,x)
for i = 1:Imax
z(n+1,i) = x-0.5;
end
This way work poorly because only final array is zero in all position in time except for the final position i.e. only z(Nmax,:) is correctly calculated.
How do I do this breakdown correctly in such a way as to end up with an array which has the values of z at every time step n in all positions?
Open to suggestions, thank you so much :)
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!