Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

functions with for loop

6 visualizaciones (últimos 30 días)
Mazin
Mazin el 19 de Jun. de 2017
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hi all, I have a function for some heat transfer model and it works just fine. The out put of the function is an nxm matrix. The next step for me is to run that function in for loop and get multiple nxm matrices. The code is long but I'll put a short example here for illustration.
function [T] = heat(x,y) % xa and y are nxm matrices
T = X+T0;
end
now what if i want to run this code 10 times by changing the value of T0 to the previous value of T as follows
function [T] = heat(x,y)
for i=1:10
T = X+T0;
T0=T;
end
end
how can get the 10 matrices of T as an output? because running this code doesn't work

Respuestas (1)

dpb
dpb el 19 de Jun. de 2017
Your heat transfer model has to be dependent upon something else to cause it to return different values; you show arguments (x,y) which one presumes are the geometry. What is the variable you want to change in these three runs; it can't be the output temperature itself, it has to be something in the model.
You need to move whatever is/are that/those parameter/s into the argument list so you can set its/their values when you call it in the loop. Then the loop could look something like
VarToChange=V0; dV=dV0; % initial values of the variable(s) and the change/increment
N=10; % the number of loops
T=zeros(n,m,N); % preallocate the output for nxm x nLoops
for i=1:N
T(:,:,i)=heat(x,y,VarToChange); % call the model with the other parameter(s)
VarToChange=VarToChange+dV; % increment the variable
end
You'll end up w/ 3D array with each plane one of the solutions for the particular VarToChange level.
You could, alternatively, use cell arrays and write the return values as
T{i} = heat(x,y,VarToChange);
and each would then be an nxm cell array.

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by