Effacer les filtres
Effacer les filtres

Scripts works as main code but not as a function

12 vues (au cours des 30 derniers jours)
Mizo Amreya
Mizo Amreya le 15 Juil 2020
Commenté : Mizo Amreya le 15 Juil 2020
Hello All,
I've written a piece of script in my main code and it's working fine.
I'm trying to move it into a function to make my code more efficient, but it stops working and spits out the following error.
Error: Index exceeds the number of array elements (1).
Error in noflow_boundary (line 27)
lamdaWG(i) = lamdaO(i+Imax);
Here is my script within my main code:
for i=1:Imax*Jmax
if i <= Imax
lamdaWC(i) = 0;
else
lamdaWC(i) = lamdaO(i-Imax);
end
if mod(i,Imax) == 1
lamdaWD(i) = 0;
else
lamdaWD(i) = lamdaO(i-1);
end
if mod(i,Imax) == 0
lamdaWF(i) = 0;
else
lamdaWF(i) = lamdaO(i);
end
if i > (Imax*Jmax)-Imax
lamdaWG(i) = 0;
else
lamdaWG(i) = lamdaO(i+Imax);
end
end
And here is how I'm using it in function format:
function [lamdaWC,lamdaWD,lamdaWF,lamdaWG] = noflow_boundary(lamdaO,Imax,Jmax)
% No Flow Boundary Conditions
for i=1:Imax*Jmax
if i <= Imax
lamdaWC(i) = 0;
else
lamdaWC(i) = lamdaO(i-Imax);
end
if mod(i,Imax) == 1
lamdaWD(i) = 0;
else
lamdaWD(i) = lamdaO(i-1);
end
if mod(i,Imax) == 0
lamdaWF(i) = 0;
else
lamdaWF(i) = lamdaO(i);
end
if i > (Imax*Jmax)-Imax
lamdaWG(i) = 0;
else
lamdaWG(i) = lamdaO(i+Imax); % this is the line with the error occurs
end
end
end
And here's how I'm calling the function in my main code:
[lamdaWC(i),lamdaWD(i),lamdaWF(i),lamdaWG(i)] = noflow_boundary(lamdaO(i),Imax,Jmax);
Anyone has any idea why the error is popping out in the function and not main code? and how I can resolve it?
Thanking you in advance.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 15 Juil 2020
call it this way
[lamdaWC,lamdaWD,lamdaWF,lamdaWG] = noflow_boundary(lamdaO,Imax,Jmax);
  1 commentaire
Mizo Amreya
Mizo Amreya le 15 Juil 2020
Thank you, it worked.
What's the logic behind dropping the counter (i)?

Connectez-vous pour commenter.

Plus de réponses (1)

Tanmay Das
Tanmay Das le 15 Juil 2020
Hi,
I have the understanding that you are passing a single element i.e. lamdaO(i) as an argument but in order to execute your function, you may need to pass the whole array i.e. lamdaO. Also the return types are arrays so you may need to assign the return values of your function to arrays instead. The following code may be helpful for your understanding:
% function definition
function [lamdaWC,lamdaWD,lamdaWF,lamdaWG] = noflow_boundary(lamdaO,Imax,Jmax)
% No Flow Boundary Conditions
for i=1:Imax*Jmax
if i <= Imax
lamdaWC(i) = 0;
else
lamdaWC(i) = lamdaO(i-Imax);
end
if mod(i,Imax) == 1
lamdaWD(i) = 0;
else
lamdaWD(i) = lamdaO(i-1);
end
if mod(i,Imax) == 0
lamdaWF(i) = 0;
else
lamdaWF(i) = lamdaO(i);
end
if i > (Imax*Jmax)-Imax
lamdaWG(i) = 0;
else
lamdaWG(i) = lamdaO(i+Imax); % this is the line with the error occurs
end
end
end
% function call
[lamdaWC,lamdaWD,lamdaWF,lamdaWG] = noflow_boundary(lamdaO,Imax,Jmax);
Hope that helps!
  1 commentaire
Mizo Amreya
Mizo Amreya le 15 Juil 2020
Thank you so much for explaning your logic, I totally understand now.
The code is working.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by