Info
This question is closed. Reopen it to edit or answer.
functions with for loop
6 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
dpb
on 19 Jun 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.
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!