Azzera filtri
Azzera filtri

Scripts works as main code but not as a function

3 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

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

Accedi per commentare.

Più risposte (1)

Tanmay Das
Tanmay Das il 15 Lug 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 Commento
Mizo Amreya
Mizo Amreya il 15 Lug 2020
Thank you so much for explaning your logic, I totally understand now.
The code is working.

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by